« blib/lib/Test/CPAN/Health/Check/AbandonedDeps.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/CIConfig.pm »
| 1 | package Test::CPAN::Health::Check::Benchmarks; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Carp qw(croak carp); | ||
| 8 | use File::Find::Rule; | ||
| 9 | use File::Spec; | ||
| 10 | use Readonly; | ||
| 11 | use Params::Validate::Strict qw(validate_strict); | ||
| 12 | |||
| 13 | use parent 'Test::CPAN::Health::Check'; | ||
| 14 | |||
| 15 | our $VERSION = '0.1.0'; | ||
| 16 | |||
| 17 | # Conventional directory names for distribution benchmarks. | ||
| 18 | Readonly::Array my @BENCH_DIRS => qw(bench benchmarks benchmark); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Test::CPAN::Health::Check::Benchmarks - Check that benchmark scripts are present | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use Test::CPAN::Health::Check::Benchmarks; | ||
| 27 | |||
| 28 | my $check = Test::CPAN::Health::Check::Benchmarks->new; | ||
| 29 | my $result = $check->run($dist); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Checks for the presence of a C<bench/>, C<benchmarks/>, or C<benchmark/> | ||
| 34 | directory containing at least one file. | ||
| 35 | |||
| 36 | Score matrix: | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item * 100 -- benchmarks directory found with at least one file. | ||
| 41 | |||
| 42 | =item * 50 -- benchmarks directory found but empty. | ||
| 43 | |||
| 44 | =item * 0 -- no benchmarks directory found. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub id { return 'benchmarks' } | ||
| 51 | sub name { return 'Benchmarks' } | ||
| 52 | sub description { return 'Checks that benchmark scripts are present in the distribution' } | ||
| 53 | sub weight { return 1 } | ||
| 54 | sub category { return 'quality' } | ||
| 55 | |||
| 56 | =head2 run | ||
| 57 | |||
| 58 | =head3 PURPOSE | ||
| 59 | |||
| 60 | Locate a benchmarks directory and report whether it contains files. | ||
| 61 | |||
| 62 | =head3 API SPECIFICATION | ||
| 63 | |||
| 64 | =head4 INPUT | ||
| 65 | |||
| 66 | dist Test::CPAN::Health::Distribution required | ||
| 67 | context Hashref optional (unused) | ||
| 68 | |||
| 69 | =head4 OUTPUT | ||
| 70 | |||
| 71 | L<Test::CPAN::Health::Result> with: | ||
| 72 | |||
| 73 | check_id 'benchmarks' | ||
| 74 | status 'pass' | 'warn' | 'fail' | ||
| 75 | score 100 | 50 | 0 | ||
| 76 | summary human-readable verdict | ||
| 77 | |||
| 78 | =head3 MESSAGES | ||
| 79 | |||
| 80 | Code | Severity | Message | Resolution | ||
| 81 | ------+----------+------------------------------------+--------------------- | ||
| 82 | BM001 | FAIL | No benchmarks directory found | Add a bench/ or benchmarks/ directory | ||
| 83 | BM002 | WARN | Benchmarks directory is empty | Add at least one benchmark script | ||
| 84 | BM003 | PASS | Benchmarks directory found | | ||
| 85 | |||
| 86 | =head3 FORMAL SPECIFICATION | ||
| 87 | |||
| 88 | -- Z schema (placeholder) -- | ||
| 89 | BenchmarksOp | ||
| 90 | dist : Distribution | ||
| 91 | score : {0, 50, 100} | ||
| 92 | ------------------------------------------------------- | ||
| 93 | no_dir => score = 0 | ||
| 94 | dir_empty => score = 50 | ||
| 95 | dir_nonempty => score = 100 | ||
| 96 | |||
| 97 | =head3 SIDE EFFECTS | ||
| 98 | |||
| 99 | Reads directory listings from disk. | ||
| 100 | |||
| 101 | =head3 USAGE EXAMPLE | ||
| 102 | |||
| 103 | my $result = Test::CPAN::Health::Check::Benchmarks->new->run($dist); | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub run { | ||
| 108 | my ($self, $dist, $context) = @_; | ||
| 109 | |||
| 110 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 111 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 112 | |||
| 113 | for my $dir_name (@BENCH_DIRS) { | ||
| 114 | next unless $dist->has_dir($dir_name); | ||
| 115 | |||
| 116 | my $dir_path = File::Spec->catdir($dist->path, $dir_name); | ||
| 117 | my @files = File::Find::Rule->file->in($dir_path); | ||
| 118 | |||
| 119 | if (@files) { | ||
| 120 | return $self->_result( | ||
| 121 | status => 'pass', | ||
| 122 | score => 100, | ||
| 123 | summary => sprintf( | ||
| 124 | 'Benchmarks directory found: %s/ (%d file(s))', | ||
| 125 | $dir_name, scalar @files, | ||
| 126 | ), | ||
| 127 | data => { name => $self->name, dir => $dir_name, count => scalar @files }, | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | |||
| 131 | return $self->_result( | ||
| 132 | status => 'warn', | ||
| 133 | score => 50, | ||
| 134 | summary => sprintf('Benchmarks directory %s/ exists but is empty', $dir_name), | ||
| 135 | details => ['Add at least one benchmark script to the directory'], | ||
| 136 | data => { name => $self->name, dir => $dir_name, count => 0 }, | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | return $self->_result( | ||
| 141 | status => 'fail', | ||
| 142 | score => 0, | ||
| 143 | summary => 'No benchmarks directory found (bench/, benchmarks/, or benchmark/)', | ||
| 144 | details => [ | ||
| 145 | 'Create a bench/ or benchmarks/ directory containing Perl scripts that use the Benchmark module', | ||
| 146 | 'Each script should time key operations or compare alternative implementations, e.g.: use Benchmark qw(cmpthese); cmpthese(-1, { approach_a => sub { ... }, approach_b => sub { ... } })', | ||
| 147 | 'Benchmarks help users choose between options and help maintainers catch performance regressions early', | ||
| 148 | ], | ||
| 149 | data => { name => $self->name }, | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head1 AUTHOR | ||
| 154 | |||
| 155 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 156 | |||
| 157 | =head1 LICENSE AND COPYRIGHT | ||
| 158 | |||
| 159 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 160 | |||
| 161 | This program is free software; you can redistribute it and/or modify it | ||
| 162 | under the terms of the GNU General Public License as published by the Free | ||
| 163 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 164 | any later version. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | 1; | ||