DKbench.pm

filter: Statementstmt 33.2% 187 / 562 Branchbran 26.9% 28 / 104 Conditioncond 48.6% 18 / 37 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 57.5% 46 / 80 Podpod 100% 4 / 4 totaltotal 35.6% 288 / 809 CC 110 SCAR 26.9
CC
110
Cov
33%
_bench_matrixreal
56.0
_bench_levenshtein
56.0
_bench_bio_codons
30.0
CRAP 14.7
↑ Summary DKbench/Setup.pm »
1 package Benchmark::DKbench;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use List::Util qw(min max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Astro::Coord::Constellations 'constellation_for_eq';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Astro::Coord::Precession 'precess';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Benchmark::MCE ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Crypt::JWT qw(encode_jwt decode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use CSS::Inliner;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use HTML::FormatText;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Imager::Filter::Mandelbrot;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Image::PHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use Math::DCT ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use Math::MatrixReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use SQL::Abstract::Classic;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use SQL::Inserter;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use Text::Levenshtein::Damerau::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Text::Levenshtein::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶my $mono_clock = $^O !~ /win/i || $Time::HiRes::VERSION >= 1.9764;
Condition33%1/3
!($^O =~ /win/i) || $Time::HiRes::VERSION >= 1.9764
l!l&&r!l&&!r
400
MC/DC0%0/2
!($^O =~ /win/i) || $Time::HiRes::VERSION >= 1.9764
!($^O =~ /win/i) $Time::HiRes::VERSION >= 1.9764
Truth table33%1/3
not(A) || B
A= !($^O =~ /win/i)B= $Time::HiRes::VERSION >= 1.9764
ABresult
1X1
011
000
37  
38 4 our $VERSION = '3.02';
39 4 our @EXPORT = qw(system_identity suite_run calc_scalability suite_calc);
40 4 our $datadir = dist_dir("Benchmark-DKbench");
41  
42 =head1 NAME
43  
44 Benchmark::DKbench - Perl CPU Benchmark Suite
45  
46 =head1 SYNOPSIS
47  
48 # Run the suite single-threaded and then multi-threaded on multi-core systems
49 # Will print scores for the two runs and multi/single thread scalability
50 dkbench
51  
52 # A dual-thread "quick" run (with times instead of scores)
53 dkbench -j 2 -q
54  
55 # If BioPerl is installed, enable the BioPerl benchmarks by downloading GenBank data
56 dkbench --setup
57  
58 # Force install the reference versions of all CPAN modules
59 setup_dkbench --force
60  
61 =head1 DESCRIPTION
62  
63 A Perl benchmark suite for general compute, created to evaluate the comparative
64 performance of systems when running computationally intensive Perl (both pure Perl
65 and C/XS) workloads. It is a good overall indicator for generic CPU performance in
66 real-world scenarios. It runs single and multi-threaded (able to scale to hundreds
67 of CPUs) and can be fully customized to run the benchmarks that better suit your own
68 scenario - even allowing you to add your own custom benchmarks.
69  
70 =head1 RUNNING WITH DOCKER
71  
72 For convenience, there is a ready-to-run Debian-based L<Docker image|https://github.com/dkechag/dkbench-docker>
73 available.
74  
75 With Docker installed, you can run:
76  
77 docker run -it --rm dkechag/dkbench
78  
79 which drops you into a bash shell inside the container. There, the C<dkbench>
80 command is available to run the suite.
81  
82 Docker does add a small overhead and variance to the test suite, so be aware there
83 is still an advantage in manual installation.
84  
85 =head1 MANUAL INSTALLATION
86  
87 See the L</"setup_dkbench"> script below for more on the installation of a couple
88 of optional benchmarks and standardizing your benchmarking environment, otherwise
89 here are some general guidelines for various systems.
90  
91 =head2 Linux / WSL etc
92  
93 The only non-CPAN software required to install/run the suite is a build environment
94 for the C/XS modules (C compiler, make etc.) and Perl. On the most popular Linux
95 package managers you can easily set up such an environment (as root or with sudo):
96  
97 # Debian/Ubuntu etc
98 apt-get update
99 apt-get install build-essential perl cpanminus
100  
101 # CentOS/Red Hat
102 yum update
103 yum install gcc make patch perl perl-App-cpanminus
104  
105 After that, you can use L<App::cpanminus> to install the benchmark suite (as
106 root/sudo is the easiest, will install for all users):
107  
108 cpanm -n Benchmark::DKbench
109  
110 =head2 Solaris
111  
112 You will need to install the Oracle Solaris Studio development package to have a
113 compiler environment, and to add its C<bin> directory to your PATH, before installing
114 the benchmark suite.
115  
116 =head2 Strawberry Perl
117  
118 If you are on Windows, you should be using the Windows Subsystem for Linux (WSL)
119 for running Perl or, if you can't (e.g. old Windows versions), Cygwin instead.
120 The suite should still work on Strawberry Perl, as long as you don't try to run
121 tests when installing (some dependencies will not pass them). The simplest way is
122 with L<App::cpanminus> (most Strawberry Perl versions have it installed):
123  
124 cpanm -n Benchmark::DKbench
125  
126 otherwise with the base CPAN shell:
127  
128 perl -MCPAN -e shell
129  
130 > notest install Benchmark::DKbench
131  
132 and then note that the scripts get the batch extension appended, so C<dkbench.bat>
133 runs the suite (and C<setup_dkbench.bat> can assist with module versions, optional
134 benchmarks etc.).
135  
136 Be aware that Strawberry Perl is slower, on my test system I get almost 50% slower
137 performance than WSL and 30% slower than Cygwin.
138  
139 =head1 SCRIPTS
140  
141 You will most likely only ever need the main script C<dkbench> which launches the
142 suite, although C<setup_dkbench> can help with setup or standardizing/normalizing your
143 benchmarking environment.
144  
145 =head2 C<dkbench>
146  
147 The main script that runs the DKbench benchmark suite. If L<BioPerl> is installed,
148 you may want to start with C<dkbench --setup>. But beyond that, there are many
149 options to control number of threads, iterations, which benchmarks to run etc:
150  
151 dkbench [options]
152  
153 Options:
154 --threads <i>, -j <i> : Number of benchmark threads (default is 1).
155 --multi, -m : Multi-threaded using all your CPU cores/threads.
156 --max_threads <i> : Override the CPU detection to specify max CPU threads.
157 --iter <i>, -i <i> : Number of suite iterations (with min/max/avg at the end).
158 --stdev : Show relative standard deviation (for iter > 1).
159 --include <regex> : Run only benchmarks that match regex.
160 --exclude <regex> : Do not run benchmarks that match regex.
161 --time, -t : Report time (sec) instead of score.
162 --quick, -q : Quick benchmark run (implies -t).
163 --no_mce : Do not run under MCE::Loop (implies -j 1).
164 --scale <i>, -s <i> : Scale the bench workload by x times (incompatible with -q).
165 --skip_bio : Skip BioPerl benchmarks.
166 --skip_prove : Skip Moose prove benchmark.
167 --time_piece : Run optional Time::Piece benchmark (see benchmark details).
168 --bio_codons : Run optional BioPerl Codons benchmark (does not scale well).
169 --sleep <i> : Sleep for <i> secs after each benchmark.
170 --duration <i>, -d <i> : Minimum duration in seconds for suite run.
171 --setup : Download the GenBank data to enable the BioPerl tests.
172 --datapath <path> : Override the path where the expected benchmark data is found.
173 --ver <num> : Skip benchmarks added after the specified version.
174 --help -h : Show basic help and exit.
175  
176 The default run (no options) will run all the benchmarks both single-threaded and
177 multi-threaded (using all detected CPU cores/hyper-threads) and show you scores and
178 multi vs single threaded scalability.
179  
180 The scores are calibrated such that a reference CPU (Intel Xeon Platinum 8481C -
181 Sapphire Rapids) would achieve a score of 1000 in a single-core benchmark run using
182 the default software configuration (Linux/Perl 5.36+ built with multiplicity and
183 threads, with reference CPAN module versions). Perl built without thread support and
184 multi(plicity) will be a bit faster (usually in the order of ~3-4%), while older Perl
185 versions will most likely be slower. Different CPAN module versions will also impact
186 scores, using C<setup_dkbench> is a way to ensure a reference environment for more
187 meaningful hardware comparisons.
188  
189 The multi-thread scalability calculated by the suite should approach 100% if each
190 thread runs on a full core (i.e. no SMT), and the core can maintain the clock speed
191 it had on the single-thread runs. Note that the overall scalability is an average
192 of the benchmarks that drops non-scaling outliers (over 2*stdev less than the mean).
193  
194 If you want to reduce the effects of thermal throttling, which will lower the speed
195 of (mainly multi-threaded) benchmarks as the CPU temperature increases, the C<sleep>
196 option can help by adding cooldown time between each benchmark.
197  
198 The suite will report a Pass/Fail per benchmark. A failure may be caused if you have
199 different CPAN module version installed - this is normal, and you will be warned.
200  
201 L<MCE::Loop> is used to run on the desired number of parallel threads, with minimal
202 overhead. There is an option to disable it, which forces a single-thread run.
203  
204 =head2 C<setup_dkbench>
205  
206 Simple installer to check/get the reference versions of CPAN modules and download
207 the GenBank data file required for the BioPerl benchmarks of the DKbench suite.
208  
209 It assumes that you have some software already installed (see L</"INSTALLATION"> above),
210 try C<setup_dkbench --help> will give you more details.
211  
212 setup_dkbench [--force --sudo --test --data=s --help]
213  
214 Options:
215 --sudo : Will use sudo for cpanm calls.
216 --force : Will install reference CPAN module versions and re-download the genbank data.
217 --test : Will run the test suites for the CPAN module (default behaviour is to skip).
218 --data=s : Data dir path to copy files from. Should not need if you installed DKbench.
219 --help : Print this help text and exit.
220  
221 Running it without any options will fetch the data for the BioPerl tests (similar to
222 C<dkbench --setup>) and use C<cpanm> to install any missing libraries.
223  
224 Using it with C<--force> will install the reference CPAN module versions, including
225 BioPerl which is not a requirement for DKbench, but enables the BioPerl benchmarks.
226  
227 The reference Perl and CPAN versions are suggested if you want a fair comparison
228 between systems and also for the benchmark Pass/Fail results to be reliable.
229  
230 =head1 BENCHMARKS
231  
232 The suite consists of 21 benchmarks, 19 will run by default. However, the
233 C<BioPerl Monomers> requires the optional L<BioPerl> to be installed and GenBank
234 data to be downloaded (C<dkbench --setup> can do the latter), so you will only
235 see 18 benchmarks running just after a standard install. Because the overall score
236 is an average, it is generally unaffected by adding or skipping a benchmark or two.
237  
238 The optional benchmarks are enabled with the C<--time_piece> and C<--bio_codons>
239 options.
240  
241 =over 4
242  
243 =item * C<Astro> : Calculates precession between random epochs and finds the
244 constellation for random equatorial coordinates using L<Astro::Coord::Precession>
245 and L<Astro::Coord::Constellations> respectively.
246  
247 =item * C<BioPerl Codons> : Counts codons on a sample bacterial sequence. Requires
248 L<BioPerl> to be installed.
249 This test does not scale well on multiple threads, so is disabled by default (use
250 C<--bio_codons>) option. Requires data fetched using the C<--setup> option.
251  
252 =item * C<BioPerl Monomers> : Counts monomers on 500 sample bacterial sequences using
253 L<BioPerl> (which needs to be installed). Requires data fetched using the C<--setup>
254 option.
255  
256 =item * C<CSS::Inliner> : Inlines CSS on 2 sample wiki pages using L<CSS::Inliner>.
257  
258 =item * C<Crypt::JWT> : Creates large JSON Web Tokens with RSA and EC crypto keys
259 using L<Crypt::JWT>.
260  
261 =item * C<DateTime> : Creates and manipulates L<DateTime> objects.
262  
263 =item * C<DBI/SQL> : Creates a mock L<DBI> connection (using L<DBD::Mock>) and passes
264 it insert/select statements using L<SQL::Inserter> and L<SQL::Abstract::Classic>.
265 The latter is quite slow at creating the statements, but it is widely used.
266  
267 =item * C<Digest> : Creates MD5, SHA-1 and SHA-512 digests of a large string.
268  
269 =item * C<Encode> : Encodes/decodes large strings from/to UTF-8/16, cp-1252.
270  
271 =item * C<HTML::FormatText> : Converts HTML to text for 2 sample wiki pages using
272 L<HTML::FormatText>.
273  
274 =item * C<Imager> : Loads a sample image and performs edits/manipulations with
275 L<Imager>, including filters like gaussian, unsharp mask, mandelbrot.
276  
277 =item * C<JSON::XS> : Encodes/decodes random data structures to/from JSON using
278 L<JSON::XS>.
279  
280 =item * C<Math::DCT> : Does 8x8, 18x18 and 32x32 DCT transforms with L<Math::DCT>.
281  
282 =item * C<Math::MatrixReal> : Performs various manipulations on L<Math::MatrixReal>
283 matrices.
284  
285 =item * C<Moose> : Creates L<Moose> objects.
286  
287 =item * C<Moose prove> : Runs 110 tests from the Moose 2.2201 test suite. The least
288 CPU-intensive test (which is why there is the option C<--skip_prove> to disable it),
289 most of the time will be spent loading the interpreter and the Moose module for each
290 test, which is behaviour representative of how a Perl test suite runs by default.
291  
292 =item * C<Primes> : Calculates all primes up to 7.5 million. Small number with
293 repeat was chosen to keep low memory (this is a pure Perl function no Math libraries).
294  
295 =item * C<Regex/Subst> : Concatenates 3 wiki pages into a byte string then matches
296 3 typical regexes (for names, emails, URIs), replaces HTML tags with their contents
297 (starting with the innermost) and does calls subst a few times.
298  
299 =item * C<Regex/Subst utf8> : Exactly the same as C<Regex/Subst>, but reads into
300 a utf8 string. Perl version can make a big difference, as Unicode behaviour has
301 changed (old Perl versions are faster but less strict in general).
302  
303 =item * C<Text::Levenshtein> : The edit distance for strings of various lengths (up
304 to 2500) are calculated using L<Text::Levenshtein::XS> and L<Text::Levenshtein::Damerau::XS>.
305  
306 =item * C<Time::Piece> : Creates and manipulates/converts Time::Piece objects. It
307 is disabled by default because it uses the OS time libraries, so it might skew results
308 if you are trying to compare CPUs on different OS platforms. It can be enabled with
309 the C<--time_piece> option. For macOS specifically, it can only be enabled if C<--no_mce>
310 is specified, as it runs extremely slow when forked.
311  
312 =back
313  
314 =head2 C<benchmark_list>
315  
316 my $benchmarks = Benchmark::DKbench::benchmark_list($extra_benchmarks_hashref);
317  
318 Returns a hashref with all the suite's benchmarks listed above (optionally adding
319 the ones you pass as an argument) in the format C<Benchmark::MCE::suite_run> expects
320 as the C<bench> parameter.
321  
322 =head1 EXPORTED FUNCTIONS
323  
324 The exported functions are the same as L<Benchmark::MCE> and are used by the C<dkbench>
325 script. You will normally use these functions from L<Benchmark::MCE> if you want to
326 build your own benchmark suite, except if you want to extend DKbench by adding your
327 own modules, and/or replacing the C<dkbench> script:
328  
329 =head2 C<system_identity>
330  
331 This is now imported directly from L<Benchmark::MCE>, see POD for that module.
332  
333 =head2 C<suite_run>
334  
335 This is now imported from L<Benchmark::MCE>, with the addition of C<extra_bench>,
336 see POD for that module and L<CUSTOM BENCHMARKS> below.
337  
338 =head2 C<calc_scalability>
339  
340 This is now an alias to L<Benchmark::MCE::calc_scalability>, see POD for that module.
341  
342 =head2 C<suite_calc>
343  
344 This is now imported directly from L<Benchmark::MCE>, with the addition of C<extra_bench>,
345 see POD for that module and L<CUSTOM BENCHMARKS> below.
346  
347 =head1 CUSTOM BENCHMARKS
348  
349 Version 2.5 introduced the ability to add custom benchmarks to be run along any
350 of the included ones of the suite. This allows you to create a suite that is more
351 relevant to you, by including the actual code you will be running on the systems
352 you are benchmarking.
353  
354 Here is an example of adding a benchmark to the test suite and running it together
355 with the default benchmarks:
356  
357 use Benchmark::DKbench;
358 use Math::Trig qw/:great_circle :pi/;
359  
360 sub great_circle {
361 my $iter = shift || 1; # Optionally have an argument that scales the workload
362 my $dist = 0;
363 $dist +=
364 great_circle_distance(rand(pi), rand(2 * pi), rand(pi), rand(2 * pi))
365 for 1 .. $iter;
366 return $dist; # Returning something is optional, but is used to Fail bench on no match
367 }
368  
369 my %stats = suite_run({
370 extra_bench => { 'Math::Trig' => # A unique name for the benchmark
371 [
372 \&great_circle, # Reference to bench function
373 '3144042.81433949', # Output for your reference Perl - determines Pass/Fail (optional)
374 5.5, # Seconds to complete in normal mode for score = 1000 (optional)
375 1000000, # Argument to pass for --quick mode (optional)
376 5000000 # Argument to pass for normal mode (optional)
377 ]},
378 }
379 );
380  
381 You can use a prefix for the naming of your custom benchmarks and make use of the
382 C<include> argument to run only the custom benchmarks. Here is an example, where
383 a custom test is defined inline, without any of the optional arguments and specified
384 to run by itself:
385  
386 my %stats = suite_run({
387 include => 'custom',
388 extra_bench => { custom1 => [sub {my @a=split(//, 'x'x$_) for 1..10000}] }
389 }
390 );
391  
392 If you want to do a multi-threaded run as well and then calculate scalability:
393  
394 my %stats_multi = suite_run({
395 threads => system_identity(1),
396 include => 'custom',
397 extra_bench => { custom1 => [sub {my @a=split(//, 'x'x$_) for 1..10000}] }
398 }
399 );
400  
401 my %scal = calc_scalability(\%stats, \%stats_multi);
402  
403 Or, with a single call via the convenience function L<suite_calc>:
404  
405 my ($stats, $stats_multi, $scal) = suite_calc({
406 include => 'custom',
407 extra_bench => { custom1 => [sub {my @a=split(//, 'x'x$_) for 1..10000}] }
408 }
409 );
410  
411 For creating your own benchmark suite from scratch, instead of extending the existing
412 ones, please see L<Benchmark::MCE> instead which is only the harness without benchmarks.
413  
414 =head1 NOTES
415  
416 The benchmark suite was created to compare the performance of various cloud offerings.
417 You can see the L<original perl blog post|http://blogs.perl.org/users/dimitrios_kechagias/2022/03/cloud-provider-performance-comparison-gcp-aws-azure-perl.html>
418 as well as the L<2023 follow-up|https://dev.to/dkechag/cloud-vm-performance-value-comparison-2023-perl-more-1kpp>.
419  
420 The benchmarks for the first version were more tuned to what I would expect to run
421 on the servers I was testing, in order to choose the optimal types for the company
422 I was working for. The second version has expanded a bit over that, and is friendlier
423 to use.
424  
425 Although this benchmark is in general a good indicator of general CPU performance
426 and can be customized to your needs, no benchmark is as good as running your own
427 actual workload (which can be done via the L<CUSTOM BENCHMARKS> functionality).
428  
429 =head2 SCORES
430  
431 Some sample DKbench score results from various systems for comparison (all on
432 reference setup with Perl 5.36.0 thread-multi):
433  
434 CPU Cores/HT Single Multi Scalability
435 Intel i7-4750HQ @ 2.0 (macOS) 4/8 612 2332 46.9%
436 AMD Ryzen 5 PRO 4650U @ 2.1 (WSL) 6/12 905 4444 40.6%
437 Apple M1 Pro @ 3.2 (macOS) 10/10 1283 10026 78.8%
438 Apple M2 Pro @ 3.5 (macOS) 12/12 1415 12394 73.1%
439 Ampere Altra @ 3.0 (Linux) 48/48 708 32718 97.7%
440 Intel Xeon Platinum 8481C @ 2.7 (Linux) 88/176 1000 86055 48.9%
441 AMD EPYC Milan 7B13 @ 2.45 (Linux) 112/224 956 104536 49.3%
442 AMD EPYC Genoa 9B14 @ 2.7 (Linux) 180/360 1197 221622 51.4%
443  
444 =head1 AUTHOR
445  
446 Dimitrios Kechagias, C<< <dkechag at cpan.org> >>
447  
448 =head1 BUGS
449  
450 Please report any bugs or feature requests either on L<GitHub|https://github.com/dkechag/Benchmark-DKbench> (preferred), or on RT (via the email
451 C<bug-Benchmark-DKbench at rt.cpan.org> or L<web interface|https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Benchmark-DKbench>).
452  
453 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
454  
455 =head1 GIT
456  
457 L<https://github.com/dkechag/Benchmark-DKbench>
458  
459 =head1 LICENSE AND COPYRIGHT
460  
461 This software is copyright (c) 2021-2026 by Dimitrios Kechagias.
462  
463 This is free software; you can redistribute it and/or modify it under
464 the same terms as the Perl 5 programming language system itself.
465  
466 =cut
467  
468 4 *calc_scalability = \&Benchmark::MCE::calc_scalability;
469  
470 1 ▶sub system_identity { Benchmark::MCE::system_identity(@_) };
Subroutine system_identity: called CC=1 SCAR=0.0
Pod documented
471  
472 7 ▶sub benchmark_list {
Subroutine benchmark_list: called CC=2 SCAR=6.9
Pod documented
473 7 ▶ my $extra_bench = shift || {};
Condition100%2/2
shift() || {}
l!l
34
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
474 return { # idx : 0 = func, 1 = result, 2 = ref time, 3 = quick test, 4 = normal test, 5 = ver
475 7 'Astro' => [\&_bench_astro, 'e71c7ae08f16fe26aea7cfdb72785873', 5.674, 20000, 80000],
476 'BioPerl Codons' => [\&_bench_bio_codons, '97c443c099886ca60e99f7ab9df689b5', 8.752, 3, 5],
477 'BioPerl Monomers' => [\&_bench_bio_mono, 'd29ed0a5c205c803c112be1338d1f060', 5.241, 6, 20],
478 'Crypt::JWT' => [\&_bench_jwt, 'd41d8cd98f00b204e9800998ecf8427e', 6.451, 250, 900],
479 'CSS::Inliner' => [\&_bench_css, '82c1b6de9ca0500a48f8a8df0998df3c', 4.603, 2, 5],
480 'DBI/SQL' => [\&_bench_dbi, '2b8252daad9568a5b39038c696df4be3', 5.700, 5000, 15000, 2.1],
481 'DateTime' => [\&_bench_datetime, 'b08d2eeb994083b7422f6c9d86fed2c6', 6.198, 5000, 15000],
482 'Digest' => [\&_bench_digest, '4b69f6cf0f53cbf6c3444f2f767dd21d', 4.513, 50, 250],
483 'Encode' => [\&_bench_encode, 'PASS 1025', 5.725, 40, 120],
484 'HTML::FormatText' => [\&_bench_formattext, '8c2589f0a5276252805e11301fc2ab56', 4.756, 4, 10],
485 'Imager' => [\&_bench_imager, '8829cb3703e884054eb025496f336c63', 6.792, 4, 16],
486 'JSON::XS' => [\&_bench_json, 'PASS', 5.388, 600, 2200],
487 'Math::DCT' => [\&_bench_dct, '766e3bfd7a2276f452bb3d1bd21939bc', 7.147, 25000, 100000],
488 'Math::MatrixReal' => [\&_bench_matrixreal, '4606231b1309fb21ae1223fa0043fd76', 4.293, 200, 650],
489 'Moose' => [\&_bench_moose, 'd1cb92c513f6378506dfa11f694cffac', 4.968, 10000, 30000],
490 'Moose prove' => [\&_bench_moose_prv, 'PASS', 7.974, 0.5, 1],
491 'Primes' => [\&_bench_primes_m, '4266f70a7a9efb3484cf5d98eba32244', 3.680, 2, 5],
492 'Regex/Subst' => [\&_bench_regex_asc, '30ce365b25f3d597578b3bdb14aa3f57', 4.652, 8, 24],
493 'Regex/Subst utf8' => [\&_bench_regex_utf8, '857eb4e63a4d174ca4a16fe678f7626f', 5.703, 3, 10],
494 'Text::Levenshtein' => [\&_bench_levenshtein, '2948a300ed9131fa0ce82bb5eabb8ded', 5.539, 7, 25, 2.1],
495 'Time::Piece' => [\&_bench_timepiece, '2d4b149fe7f873a27109fc376d69211b', 5.907, 75000, 275000],
496 %$extra_bench
497 };
498 }
499  
500 93 ▶sub _dkbench_filter {
Subroutine _dkbench_filter: called CC=13 SCAR=30.1
501 93 my ($opt, $bench, $benchmark) = @_;
502 93 ▶ return 0 if $opt->{skip_bio} && $bench =~ /BioPerl/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"skip_bio"} and $bench =~ /BioPerl/ 10 83 93
Condition66%2/3
$opt->{"skip_bio"} and $bench =~ /BioPerl/
!ll&&!rl&&r
08310
MC/DC50%1/2
$opt->{"skip_bio"} and $bench =~ /BioPerl/
$opt->{"skip_bio"} $bench =~ /BioPerl/
Truth table66%2/3
A and B
A= $opt->{"skip_bio"}B= $bench =~ /BioPerl/
ABresult
0X0
100
111
503 83 ▶ return 0 if $opt->{skip_prove} && $bench =~ /prove/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"skip_prove"} and $bench =~ /prove/ 0 83 83
Condition33%1/3
$opt->{"skip_prove"} and $bench =~ /prove/
!ll&&!rl&&r
8300
MC/DC0%0/2
$opt->{"skip_prove"} and $bench =~ /prove/
$opt->{"skip_prove"} $bench =~ /prove/
Truth table33%1/3
A and B
A= $opt->{"skip_prove"}B= $bench =~ /prove/
ABresult
0X0
100
111
504 83 ▶ return 0 if !$opt->{bio_codons} && $bench =~ /Codons/;
Branch: 1 branch
BranchTrueFalseTotal
if not $opt->{"bio_codons"} and $bench =~ /Codons/ 0 83 83
Condition33%1/3
not $opt->{"bio_codons"} and $bench =~ /Codons/
!ll&&!rl&&r
0830
MC/DC0%0/2
not $opt->{"bio_codons"} and $bench =~ /Codons/
not $opt->{"bio_codons"} $bench =~ /Codons/
Truth table33%1/3
not(A) and B
A= not $opt->{"bio_codons"}B= $bench =~ /Codons/
ABresult
0X0
100
111
505 83 ▶ return 0 if !$opt->{time_piece} && $bench =~ /Time::Piece/;
Branch: 1 branch
BranchTrueFalseTotal
if not $opt->{"time_piece"} and $bench =~ /Time::Piece/ 4 79 83
Condition100%3/3
not $opt->{"time_piece"} and $bench =~ /Time::Piece/
!ll&&!rl&&r
3764
MC/DC100%2/2
not $opt->{"time_piece"} and $bench =~ /Time::Piece/
not $opt->{"time_piece"} $bench =~ /Time::Piece/
Truth table100%3/3
not(A) and B
A= not $opt->{"time_piece"}B= $bench =~ /Time::Piece/
ABresult
0X0
100
111
506 79 ▶ return 0 if $opt->{ver} && $benchmark->[5] && $opt->{ver} < $benchmark->[5];
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"ver"} and $benchmark->[5] and $opt->{"ver"} < $benchmark->[5] 0 79 79
Condition50%3/6
$opt->{"ver"} and $benchmark->[5] and $opt->{"ver"} < $benchmark->[5]
!ll&&!rl&&r
7900
$opt->{"ver"} and $benchmark->[5]
!ll&&!rl&&r
7630
MC/DC0%0/3
$opt->{"ver"} and $benchmark->[5] and $opt->{"ver"} < $benchmark->[5]
$opt->{"ver"} $benchmark->[5] $opt->{"ver"} < $benchmark->[5]
Truth table50%2/4
A and B and C
A= $opt->{"ver"}B= $benchmark->[5]C= $opt->{"ver"} < $benchmark->[5]
ABCresult
0XX0
10X0
1100
1111
507 79 ▶ if ($bench =~ /Bio/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bench =~ /Bio/) 0 79 79
508 0 require Bio::SeqIO;
509 0 require Bio::Tools::SeqStats;
510 }
511  
512 79 return 1;
513 }
514  
515 4 ▶sub suite_run {
Subroutine suite_run: called CC=1 SCAR=0.0
Pod documented
516 4 return Benchmark::MCE::suite_run(_init_opt(@_));
517 }
518  
519 1 ▶sub suite_calc {
Subroutine suite_calc: called CC=1 SCAR=0.0
Pod documented
520 1 return Benchmark::MCE::suite_calc(_init_opt(@_));
521 }
522  
523 5 ▶sub _init_opt {
Subroutine _init_opt: called CC=4 SCAR=15.5
524 5 my $opt = shift;
525  
526 $opt->{skip_bio} ||= !(
527 5 ▶ eval {
Condition66%2/3
$opt->{"skip_bio"} ||= !eval { do { require Bio::SeqIO; require Bio::Tools::SeqStats; 1 } }
l!l&&r!l&&!r
140
MC/DC0%0/2
$opt->{"skip_bio"} ||= !eval { do { require Bio::SeqIO; require Bio::Tools::SeqStats; 1 } }
$opt->{"skip_bio"} !eval { do { require Bio::SeqIO; require Bio::Tools::SeqStats; 1 } }
Truth table66%2/3
A ||= not(B)
A= $opt->{"skip_bio"}B= !eval { do { require Bio::SeqIO; require Bio::Tools::SeqStats; 1 } }
ABresult
1X1
011
000
528 4 require Bio::SeqIO;
529 0 require Bio::Tools::SeqStats;
530 0 1;
531 }
532 );
533 5 $opt->{filter} = \&_dkbench_filter;
534 5 ▶ $datadir = $opt->{datapath} if $opt->{datapath};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"datapath"} 0 5 5
535 5 ▶ $opt->{bench} //= benchmark_list($opt->{extra_bench});
Condition33%1/3
$opt->{"bench"} //= benchmark_list($opt->{"extra_bench"})
l!l&&r!l&&!r
050
MC/DC0%0/2
$opt->{"bench"} //= benchmark_list($opt->{"extra_bench"})
$opt->{"bench"} benchmark_list($opt->{"extra_bench"})
Truth table33%1/3
A //= B
A= $opt->{"bench"}B= benchmark_list($opt->{"extra_bench"})
ABresult
1X1
011
000
536 5 return $opt;
537 }
538  
539 1 ▶sub _bench_astro {
Subroutine _bench_astro: called CC=3 SCAR=11.0
540 1 my $iter = shift;
541 1 my $d = Digest->new("MD5");
542 my $precessed = precess([rand(24), rand(180)-90], rand(200)+1900, rand(200)+1900)
543 1 for (1..$iter*10);
544 1 my $constellation_abbrev;
545 $d->add(constellation_for_eq(rand(24), rand(180)-90, rand(200)+1900))
546 1 for (1..$iter);
547 1 return $d->hexdigest;
548 }
549  
550 0 ▶sub _bench_bio_codons {
Subroutine _bench_bio_codons: not called CC=5 SCAR=34.0
551 0 my $skip = shift;
552 0 ▶ my $iter = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
553 0 my $d = Digest->new("MD5");
554 0 my $file = catfile($datadir, "gbbct5.seq");
555 0 foreach (1..$iter) {
556 0 my $in = Bio::SeqIO->new(-file => $file, -format => "genbank");
557 0 $in->next_seq for (1..$skip);
558 0 my $seq = $in->next_seq;
559 0 my $seq_stats = Bio::Tools::SeqStats->new($seq);
560 0 my $codon_ref = $seq_stats->count_codons();
561 0 $d->add($_, $codon_ref->{$_}) for sort keys %$codon_ref;
562 }
563 0 return $d->hexdigest;
564 }
565  
566 0 ▶sub _bench_bio_mono {
Subroutine _bench_bio_mono: not called CC=3 SCAR=24.8
567 0 my $iter = shift;
568 0 my $file = catfile($datadir, "gbbct5.seq");
569 0 my $in = Bio::SeqIO->new(-file => $file, -format => "genbank");
570 0 my $d = Digest->new("MD5");
571 0 my $builder = $in->sequence_builder();
572 0 $builder->want_none();
573 0 $builder->add_wanted_slot('display_id','seq');
574 0 for (1..$iter) {
575 0 my $seq = $in->next_seq;
576 0 my $seq_stats = Bio::Tools::SeqStats->new($seq);
577 0 my $weight = $seq_stats->get_mol_wt();
578 0 $d->add(int($weight->[0]));
579 0 my $monomer_ref = $seq_stats->count_monomers();
580 0 $d->add($_, $monomer_ref->{$_}) for sort keys %$monomer_ref;
581 }
582 0 return $d->hexdigest;
583 }
584  
585 1 ▶sub _bench_css {
Subroutine _bench_css: called CC=2 SCAR=6.9
586 1 my $iter = shift;
587 1 my $d = Digest->new("MD5");
588 1 my $file;
589 my $html;
590 1 for (1..$iter) {
591 2 my $inliner = new CSS::Inliner();
592 2 my $i = $_ % 2 + 1;
593 2 $file = catfile($datadir, "wiki$i.html");
594 2 $inliner->read_file({ filename => $file });
595 2 $html = $inliner->inlinify();
596 2 $d->add(Encode::encode_utf8($html));
597 }
598 1 return $d->hexdigest;
599 }
600  
601 0 ▶sub _bench_datetime {
Subroutine _bench_datetime: not called CC=2 SCAR=17.9
602 0 my $iter = shift;
603 0 my @tz = map {DateTime::TimeZone->new( name => $_ )} qw(UTC Europe/London America/New_York);
604 0 my $d = Digest->new("MD5");
605 0 my $str;
606  
607 0 for (1..$iter) {
608 0 my $dt = DateTime->now();
609 0 my $dt1 = DateTime->from_epoch(
610 epoch => 946684800 + rand(100000000),
611 );
612 0 my $dt2 = DateTime->from_epoch(
613 epoch => 946684800 + rand(100000000),
614 );
615 0 $str = $dt2->strftime('%FT%T')."\n";
616 0 $d->add($str);
617 0 eval {$dt2->set_time_zone($tz[int(rand(3))])};
618 0 my $dur = $dt2->subtract_datetime($dt1);
619 0 eval {$dt2->add_duration($dur)};
620 0 eval {$dt2->subtract(days => int(rand(1000)+1))};
621 0 $dt->week;
622 0 $dt->epoch;
623 0 $d->add($dt2->strftime('%FT%T'));
624 0 eval {$dt2->set( year => int(rand(2030)))};
625 0 $d->add($dt2->ymd('/'));
626 }
627 0 return $d->hexdigest;
628 }
629  
630 0 ▶sub _bench_dbi {
Subroutine _bench_dbi: not called CC=3 SCAR=24.8
631 0 my $iter = shift;
632 0 my $d = Digest->new("MD5");
633 0 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
634 0 my ($data, $cols) = _db_data();
635  
636 0 foreach (1..$iter) {
637 0 my $inserter = SQL::Inserter->new(
638 dbh => $dbh,
639 table => 'table',
640 cols => $cols,
641 buffer => 2
642 );
643 0 $inserter->insert($data->[int(rand(20))]) for 1..2;
644 0 $d->add($dbh->last_insert_id(undef, undef, undef, undef));
645 0 my $sql = SQL::Abstract::Classic->new();
646 0 my ($stmt, @bind) = $sql->insert('table', $data->[int(rand(20))]);
647 0 $d->add($dbh->quote($stmt));
648 0 ($stmt, @bind) = $sql->select('table', $cols->[int(rand(20))], [map {_rand_where()} 1..int(rand(3)+1)]);
649 0 $d->add($dbh->quote($stmt._random_str(5)));
650 0 my $dbh2 = DBI->connect( 'DBI:Mock:', '', '' );
651 }
652 0 return $d->hexdigest;
653 }
654  
655 0 ▶sub _bench_dct {
Subroutine _bench_dct: not called CC=2 SCAR=17.9
656 0 my $iter = shift;
657 0 my $d = Digest->new("MD5");
658 0 $d->add(_bench_dct_sz(@$_)) foreach [$iter, 32], [$iter, 18], [$iter*8,8];
659  
660 0 return $d->hexdigest;
661 }
662  
663 0 ▶sub _bench_dct_sz {
Subroutine _bench_dct_sz: not called CC=4 SCAR=30.0
664 0 my $iter = shift;
665 0 my $sz = shift;
666 0 my $d = Digest->new("MD5");
667 0 my @arrays;
668 0 push @arrays, [map { rand(256) } ( 1..$sz*$sz )] foreach 1..10;
669 0 foreach (1..$iter) {
670 0 my $dct = dct2d($arrays[$iter % 10], $sz);
671 0 ▶ $d->add($dct->[0]) if $_ % 10 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
672 }
673 0 return $d->hexdigest;
674 }
675  
676 0 ▶sub _bench_digest {
Subroutine _bench_digest: not called CC=2 SCAR=17.9
677 0 my $iter = shift;
678 0 my $str = _read_wiki_files();
679 0 my $d = Digest->new("MD5");
680 0 my $hex;
681 0 foreach (1..$iter) {
682 0 my $d2 = Digest->new("MD5");
683 0 $d2->add($str);
684 0 $hex = $d2->hexdigest;
685 0 $d->add($hex);
686 0 $d2 = Digest->new("SHA-512");
687 0 $d2->add($str);
688 0 $hex = $d2->hexdigest;
689 0 $d->add($hex);
690 0 $d2 = Digest->new("SHA-1");
691 0 $d2->add($str);
692 0 $hex = $d2->hexdigest;
693 0 $d->add($hex);
694 }
695 0 return $d->hexdigest;
696 }
697  
698 0 ▶sub _bench_encode {
Subroutine _bench_encode: not called CC=5 SCAR=34.0
699 0 my $iter = shift;
700 0 my $str = _read_wiki_files('utf8');
701 0 my $UTF8 = Encode::find_encoding('UTF-8');
702 0 my $UTF16 = Encode::find_encoding('UTF-16');
703 0 my $res = 'PASS';
704 0 my $unenc = 0;
705 0 our $cp1252 = Encode::find_encoding('cp-1252');
706  
707 0 foreach (1..$iter) {
708 0 my $bytes = encode_utf8($str);
709 0 ▶ $res = 'Fail' unless length($bytes) > length($str);
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes > length $str 0 0 0
710 0 my $cp = decode_utf8($bytes);
711 0 ▶ my $enc = rand(1) > 0.25 ? $UTF8 : $UTF16;
Branch: 1 branch
BranchTrueFalseTotal
rand 1 > 0.25 ? : 0 0 0
712 0 $bytes = $enc->encode($cp);
713 0 $cp = $enc->decode($bytes);
714 0 ▶ $res = 'Fail' unless $cp eq $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $cp eq $str 0 0 0
715 0 my $str2 = $cp1252->encode($cp);
716 0 $enc->encode($cp1252->decode($str2));
717 0 $unenc =()= $str2 =~ /\?/g; # Non-encodable
718 }
719 0 return "$res $unenc";
720 }
721  
722 1 ▶sub _bench_imager {
Subroutine _bench_imager: called CC=4 SCAR=26.2
723 1 my $iter = shift;
724 1 my $d = Digest->new("MD5");
725  
726 1 my $data;
727 1 ▶ open (my $fh, '<:raw', catfile($datadir,'M31.bmp')) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", catfile($Benchmark::DKbench::datadir, "M31.bmp") or die $! 0 1 1
728 0 read($fh, $data, -s $fh);
729 0 close($fh);
730  
731 0 foreach (1..$iter) {
732 0 ▶ my $img = Imager->new(
Branch: 1 branch
BranchTrueFalseTotal
my $img = "Imager"->new("data", $data, "type", "bmp") or die "Imager"->errstr 0 0 0
733 data => $data,
734 type => 'bmp'
735 ) or die Imager->errstr();
736 0 my $thumb = $img->scale(scalefactor => .3);
737 0 my $newimg = $img->scale(scalefactor => 1.15);
738 0 $newimg->filter(type=>'autolevels');
739 0 $newimg->filter(type=>"gaussian", stddev=>0.5);
740 0 $newimg->paste(left=>40,top=>20,img=>$thumb);
741 0 $newimg->rubthrough(src=>$thumb,tx=>30, ty=>50);
742 0 $newimg->compose(src => $thumb, tx => 30, ty => 20, combine => 'color');
743 0 $newimg->flip(dir=>"h");
744 0 $newimg->flip(dir=>"vh");
745 0 $d->add(scalar(Image::PHash->new($newimg)->pHash));
746 0 $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
747 0 $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
748 0 $newimg = $img->copy();
749 0 $newimg->filter(type=>"unsharpmask", stddev=>1, scale=>0.5);
750 0 $newimg = $img->rotate(degrees=>20);
751 0 $newimg->filter(type=>"contrast", intensity=>1.4);
752 0 $newimg = $img->convert(matrix => [[0, 1, 0], [1, 0, 0], [0, 0, 1]]);
753 0 $newimg = $img->convert(preset=>'grey');
754 0 $d->add(scalar(Image::PHash->new($newimg)->pHash));
755 0 $img->filter(type=>'mandelbrot');
756 }
757 0 return $d->hexdigest;
758 }
759  
760 0 ▶sub _bench_json {
Subroutine _bench_json: not called CC=4 SCAR=30.0
761 0 my $iter = shift;
762 0 my $res = 'PASS';
763 0 for (1..$iter) {
764 0 my $len = int(rand(40)) + 1;
765 0 my $obj = _rand_hash($len);
766 0 my $str = encode_json($obj);
767 0 foreach (1..100) {
768 0 $obj = decode_json($str);
769 0 $str = encode_json($obj);
770 }
771 0 my $obj2 = decode_json($str);
772 0 ▶ $res = 'FAIL' unless _compare_obj($obj, $obj2);
Branch: 1 branch
BranchTrueFalseTotal
unless _compare_obj($obj, $obj2) 0 0 0
773 }
774 0 return $res;
775 }
776  
777 1 ▶sub _bench_jwt {
Subroutine _bench_jwt: called CC=4 SCAR=15.0
778 1 my $iter = shift;
779 1 my $d = Digest->new("MD5");
780 1 my $data = _random_str(5000);
781 1 my $rsa ='-----BEGIN PRIVATE KEY-----
782 MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqPfgaTEWEP3S9w0t
783 gsicURfo+nLW09/0KfOPinhYZ4ouzU+3xC4pSlEp8Ut9FgL0AgqNslNaK34Kq+NZ
784 jO9DAQIDAQABAkAgkuLEHLaqkWhLgNKagSajeobLS3rPT0Agm0f7k55FXVt743hw
785 Ngkp98bMNrzy9AQ1mJGbQZGrpr4c8ZAx3aRNAiEAoxK/MgGeeLui385KJ7ZOYktj
786 hLBNAB69fKwTZFsUNh0CIQEJQRpFCcydunv2bENcN/oBTRw39E8GNv2pIcNxZkcb
787 NQIgbYSzn3Py6AasNj6nEtCfB+i1p3F35TK/87DlPSrmAgkCIQDJLhFoj1gbwRbH
788 /bDRPrtlRUDDx44wHoEhSDRdy77eiQIgE6z/k6I+ChN1LLttwX0galITxmAYrOBh
789 BVl433tgTTQ=
790 -----END PRIVATE KEY-----';
791 1 my $key = '-----BEGIN PRIVATE KEY-----
792 MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYirTZSx+5O8Y6tlG
793 cka6W6btJiocdrdolfcukSoTEk+hRANCAAQkvPNu7Pa1GcsWU4v7ptNfqCJVq8Cx
794 zo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7fJDiFqz3
795 -----END PRIVATE KEY-----';
796 1 foreach (1..$iter) {
797 1 my $extra = _random_str(100);
798 1 my $data_in = $data . $extra;
799 1 my $token = encode_jwt(
800 payload => $data_in,
801 alg => 'ES256',
802 key => \$key,
803 );
804  
805 1 my $data_out = _decode_jwt2(token=>$token, key=>\$key);
806 1 ▶ $d->add($token) if $data_in eq $data_out.$extra;
Branch: 1 branch
BranchTrueFalseTotal
if $data_in eq $data_out . $extra 0 1 1
807  
808 1 $token = encode_jwt(
809 payload => $data_in,
810 alg => 'RS256',
811 key => \$rsa,
812 );
813  
814 0 $data_out = _decode_jwt2(token=>$token, key=>\$rsa);
815 0 ▶ $d->add($token) if $data_in eq $data_out.$extra;
Branch: 1 branch
BranchTrueFalseTotal
if $data_in eq $data_out . $extra 0 0 0
816 }
817 0 return $d->hexdigest;
818 }
819  
820 0 ▶sub _bench_formattext {
Subroutine _bench_formattext: not called CC=2 SCAR=17.9
821 0 my $iter = shift;
822 0 my $d = Digest->new("MD5");
823 0 my $file;
824 0 for (0..$iter-1) {
825 0 my $i = $_ % 2;
826 0 $file = catfile($datadir, "wiki$i.html");
827 0 my $tree = HTML::TreeBuilder->new->parse_file($file);
828 0 my $formatter = HTML::FormatText->new();
829 0 my $text = $formatter->format($tree);
830 0 $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 30);
831 0 $d->add(Encode::encode_utf8($formatter->format($tree)));
832 }
833 0 return $d->hexdigest;
834 }
835  
836 0 ▶sub _bench_matrixreal {
Subroutine _bench_matrixreal: not called CC=7 SCAR=40.3
837 0 my $iter = shift;
838 0 my $d = Digest->new("MD5");
839 0 my $smatrix = Math::MatrixReal->new_random(13);
840 0 my $matrix = Math::MatrixReal->new_random(20);
841 0 my $bmatrix = Math::MatrixReal->new_random(72);
842  
843 0 for (1..$iter) {
844 0 my $r = rand(10);
845 0 my $m1 = $r*$bmatrix;
846 0 my $m2 = $bmatrix*$r;
847 0 my $m3 = $bmatrix->multiply_scalar($bmatrix,$r);
848 # Should be zero
849 0 ▶ $d->add($m1->element(1, 1) + $m2->element(1, 1) - 2 * $bmatrix->element(1, 1))
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
850 if $_ % 10 == 1;
851  
852 0 my $m = $matrix->det;
853 0 ▶ $d->add($m) if $_ % 10 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
854 0 $m =$matrix->decompose_LR->det_LR;
855 0 ▶ $d->add($m) if $_ % 10 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
856 0 $m1 = $matrix ** 2;
857 0 $m2 = $matrix * $matrix;
858 #should be zero
859 0 ▶ $d->add($m1->element(1, 1) - $m2->element(1, 1))
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
860 if $_ % 10 == 1;
861 0 $m1 = $smatrix->inverse();
862 0 $m2 = $smatrix ** -1;
863 0 $m3 = $smatrix->decompose_LR->invert_LR;
864 0 ▶ $d->add($m1->element(1, 1), $m2->element(1, 1), $m3->element(1, 1))
Branch: 1 branch
BranchTrueFalseTotal
if $_ % 10 == 1 0 0 0
865 if $_ % 10 == 1;
866 }
867  
868 0 return $d->hexdigest;
869 }
870  
871 0 ▶sub _bench_moose {
Subroutine _bench_moose: not called CC=2 SCAR=17.9
872 0 my $iter = shift;
873 0 my $cnt = 0;
874  
875 0 for (1..$iter) {
876 0 my $p = rand(1000);
877 0 my $root = Benchmark::DKbench::MooseTree->new(node => 'root');
878 0 $root->price($p);
879 0 $root->node;
880 0 $root->_cost;
881 0 my $lchild = $root->left;
882 0 $lchild->node('child');
883 0 $lchild->price($p);
884 0 $lchild->tax;
885 0 my $child = $root->right;
886 0 $child->_cost;
887 0 my $grandchild = $child->left;
888 0 $grandchild->node('grandchild');
889 0 $grandchild->has_parent;
890 0 $grandchild->parent;
891 0 $grandchild->price($p);
892 0 $grandchild->_cost;
893 0 my $ggchild = $grandchild->right;
894 0 $ggchild->_cost;
895 0 $cnt += 5;
896 }
897 0 return md5_hex("$cnt objects");
898 }
899  
900 0 ▶sub _bench_moose_prv {
Subroutine _bench_moose_prv: not called CC=4 SCAR=30.0
901 0 my $iter = shift;
902 0 my $tdir = catfile($datadir, 't');
903 0 my $result;
904 0 ▶ if ($iter < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter < 1) { } 0 0 0
905 0 $tdir = catfile($tdir, 'recipes');
906 0 $result = `prove -rQ $tdir 2>/dev/null`;
907 } else {
908 0 $result = `prove -rQ $tdir 2>/dev/null` for (1..$iter);
909 }
910 0 ▶ if ($result =~ /Result: (\w*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /Result: (\w*)/) { } 0 0 0
911 0 return $1;
912 } else {
913 0 return '?';
914 }
915 }
916  
917 0 ▶sub _bench_primes_m {
Subroutine _bench_primes_m: not called CC=1 SCAR=6.9
918 0 my $iter = shift;
919 0 return _bench_primes($iter, 7_500_000);
920 }
921  
922 0 ▶sub _bench_primes {
Subroutine _bench_primes: not called CC=2 SCAR=17.9
923 0 my $iter = shift;
924 0 my $max = shift;
925 0 my @primes;
926 0 @primes = _get_primes($max) for (1..$iter);
927 0 return md5_hex(scalar(@primes)." primes up to $max");
928 }
929  
930 0 ▶sub _bench_regex_asc {
Subroutine _bench_regex_asc: not called CC=1 SCAR=6.9
931 0 my $iter = shift;
932 0 return _bench_regex_subst($iter, '');
933 }
934  
935 0 ▶sub _bench_regex_utf8 {
Subroutine _bench_regex_utf8: not called CC=1 SCAR=6.9
936 0 my $iter = shift;
937 0 return _bench_regex_subst($iter, 'utf8');
938 }
939  
940 0 ▶sub _bench_regex_subst {
Subroutine _bench_regex_subst: not called CC=1 SCAR=6.9
941 0 my $iter = shift;
942 0 my $enc = shift;
943 0 my $str = _read_wiki_files($enc);
944 0 my $match = _bench_regex($str, $iter);
945 0 my $repl = _bench_subst($str, $iter);
946 0 return md5_hex($match, $repl);
947 }
948  
949 0 ▶sub _bench_regex {
Subroutine _bench_regex: not called CC=2 SCAR=17.9
950 0 my $str = shift;
951 0 my $iter = shift;
952 0 my $count;
953 0 for (1..$iter) {
954 0 $count = 0;
955 0 $count += () = $str =~ /\b[A-Z][a-z]+/g;
956 0 $count += () = $str =~ /([\w\.+-]+)@[\w\.-]+\.([\w\.-]+)/g;
957 0 $count += () = $str =~ m![\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?!g;
958 }
959 0 return "$count Matched";
960 }
961  
962 0 ▶sub _bench_subst {
Subroutine _bench_subst: not called CC=4 SCAR=30.0
963 0 my $str = shift;
964 0 my $iter = shift;
965 0 my $count;
966 0 for (1..$iter) {
967 0 my $copy = $str;
968 0 $count = 0;
969 0 ▶ while (my $s = $copy =~ s#<([^>]+)>([^>]*?)</\1>#$2#g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $copy =~ s[<([^>]+)>([^>]*?)</\1>][$2]g) 0 0 0
970 0 $count += $s;
971 }
972 0 $copy = substr($copy, int(rand(100))+1) for 1..10;
973 }
974 0 return "$count Replaced";
975 }
976  
977 0 ▶sub _bench_levenshtein {
Subroutine _bench_levenshtein: not called CC=7 SCAR=40.3
978 0 my $iter = shift;
979 0 my $d = Digest->new("MD5");
980 0 my $data = _fuzzy_data();
981 0 my $diff;
982 0 foreach (1..$iter) {
983 0 foreach my $sz (qw/10 100 1000 2500/) {
984 0 my $n = scalar @{$data->{$sz}};
985 0 my $i = int(rand($n));
986 $diff = Text::Levenshtein::XS::distance(
987 $data->{$sz}->[$i], $data->{$sz}->[$_]
988 0 ) for 0..$n-1;
989 0 ▶ $d->add($diff || -1);
Condition0%0/2
$diff || -1
l!l
00
MC/DC0%0/1
$diff || -1
$diff
Truth table0%0/2
A
A= $diff
Aresult
11
00
990 0 ▶ next if $sz > 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $sz > 1000 0 0 0
991 $diff = Text::Levenshtein::Damerau::XS::xs_edistance(
992 $data->{$sz}->[$i], $data->{$sz}->[$_]
993 0 ) for 0..$n-1;
994 0 $d->add($diff);
995 }
996 }
997 0 return $d->hexdigest;
998 }
999  
1000 0 ▶sub _bench_timepiece {
Subroutine _bench_timepiece: not called CC=3 SCAR=24.8
1001 0 my $iter = shift;
1002 0 my $t = Time::Piece::localtime(1692119499);
1003 0 my $d = Digest->new("MD5");
1004 0 my $day = 3600*24;
1005 0 local $ENV{TZ} = 'UTC';
1006  
1007 0 for (1..$iter) {
1008 0 $t += int(rand(1000)-500)*$day;
1009 0 ▶ $t += 100000*$day if $t->year < 1970;
Branch: 1 branch
BranchTrueFalseTotal
if $t->year < 1970 0 0 0
1010 0 my $str = $t->strftime("%w, %d %m %Y %H:%M:%S");
1011 0 eval '$t = Time::Piece->strptime($str, "%w, %d %m %Y %H:%M:%S")';
1012 0 my $jd = $t->julian_day;
1013 0 $d->add($str,$jd);
1014 }
1015 0 return $d->hexdigest;
1016 }
1017  
1018 0 ▶sub _read_wiki_files {
Subroutine _read_wiki_files: not called CC=4 SCAR=30.0
1019 0 ▶ my $enc = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1020 0 my $str = "";
1021 0 for (0..2) {
1022 0 ▶ open my $fh, "<:$enc", catfile($datadir,"wiki$_.html") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:$enc", catfile($Benchmark::DKbench::datadir, "wiki$_.html") or die $! 0 0 0
1023 0 $str .= do { local $/; <$fh> };
1024 }
1025 0 return $str;
1026 }
1027  
1028 3 ▶sub _random_str {
Subroutine _random_str: called CC=4 SCAR=13.9
1029 3 ▶ my $length = shift || 1;
Condition100%2/2
shift() || 1
l!l
21
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1030 3 my $abc = shift;
1031 3 my $str = "";
1032 3 ▶ my ($base, $rng) = $abc ? (65, 26) : (32, 95);
Branch: 1 branch
BranchTrueFalseTotal
$abc ? : 0 3 3
1033 3 $str .= chr(int(rand($rng))+$base) for 1..$length;
1034 3 return $str;
1035 }
1036  
1037 0 ▶sub _random_uchar {
Subroutine _random_uchar: not called CC=3 SCAR=24.8
1038 0 my $chr = int(rand(800))+32;
1039 0 ▶ $chr += 128 if $chr > 127; # Skip Latin 1 supplement
Branch: 1 branch
BranchTrueFalseTotal
if $chr > 127 0 0 0
1040 0 ▶ $chr += 288 if $chr > 591; # Skip pre-Greek blocks
Branch: 1 branch
BranchTrueFalseTotal
if $chr > 591 0 0 0
1041 0 return chr($chr);
1042 }
1043  
1044 0 ▶sub _fuzzy_data {
Subroutine _fuzzy_data: not called CC=5 SCAR=34.0
1045 0 my %data;
1046 0 push @{$data{10}}, join('', map {_random_uchar()} 1..(8+int(rand(5))))
1047 0 for 0..99;
1048 0 push @{$data{100}}, $data{10}->[$_]x10 for 0..49;
1049 0 push @{$data{1000}}, _random_str(50,1)x20 for 0..7;
1050 0 push @{$data{2500}}, _random_str(50,1)x50 for 0..3;
1051 0 return \%data;
1052 }
1053  
1054 0 ▶sub _rand_where {
Subroutine _rand_where: not called CC=4 SCAR=30.0
1055 0 my $p = rand();
1056 0 ▶ if ($p > 0.5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p > 0.5) { } 0 0 0
elsif ($p > 0.2) { } 0 0 0
1057 0 return {foo => rand(10)};
1058 } elsif ($p > 0.2) {
1059 0 return {bar => {-in => [int($p*10)..int($p*20)]}};
1060 } else {
1061 0 ▶ my $op = $p > 0.1 ? '-and' : '-or';
Branch: 1 branch
BranchTrueFalseTotal
$p > 0.1 ? : 0 0 0
1062 0 my @cond = map {_rand_where()} 1..int(rand(3)+1);
1063 0 return {$op => [@cond]};
1064 }
1065 }
1066  
1067 0 ▶sub _db_data {
Subroutine _db_data: not called CC=2 SCAR=17.9
1068 0 my (@data, @cols);
1069 0 foreach (1..20) {
1070 my $d = {
1071 id => int(rand(10000000)),
1072 date => \"NOW()",
1073 0 map {"data".$_ => "foo bar" x int(rand(5)+1)} 1..int(rand(20)+1)
1074 };
1075 0 push @data, $d;
1076 0 push @cols, [sort keys %$d];
1077 }
1078 0 return \@data, \@cols;
1079 }
1080  
1081 5 ▶sub _compare_obj {
Subroutine _compare_obj: called CC=4 SCAR=15.2
1082 5 my ($obj1, $obj2) = @_;
1083 5 my $t1 = ref($obj1);
1084 5 my $t2 = ref($obj2);
1085 5 ▶ return 0 if $t1 ne $t2;
Branch: 1 branch
BranchTrueFalseTotal
if $t1 ne $t2 1 4 5
1086 4 ▶ return $obj1 eq $obj2 unless $t1;
Branch: 1 branch
BranchTrueFalseTotal
unless $t1 4 0 4
1087 0 ▶ return $t1 eq 'ARRAY' ? _compare_arr($obj1, $obj2) : _compare_hash($obj1, $obj2);
Branch: 1 branch
BranchTrueFalseTotal
$t1 eq 'ARRAY' ? : 0 0 0
1088 }
1089  
1090 2 ▶sub _compare_arr {
Subroutine _compare_arr: called CC=4 SCAR=13.9
1091 2 my ($arr1, $arr2) = @_;
1092 2 my $sz = scalar @$arr1;
1093 2 ▶ return 0 if $sz != scalar @$arr2;
Branch: 1 branch
BranchTrueFalseTotal
if $sz != scalar @$arr2 1 1 2
1094 1 for (0..$sz-1) {
1095 2 ▶ return 0 unless _compare_obj($arr1->[$_], $arr2->[$_]);
Branch: 1 branch
BranchTrueFalseTotal
unless _compare_obj($arr1->[$_], $arr2->[$_]) 1 1 2
1096 }
1097 0 return 1;
1098 }
1099  
1100 2 ▶sub _compare_hash {
Subroutine _compare_hash: called CC=4 SCAR=13.9
1101 2 my ($h1, $h2) = @_;
1102 2 ▶ return 0 if scalar keys %$h1 != scalar keys %$h2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$h1 != scalar keys %$h2 1 1 2
1103 1 for (keys %$h1) {
1104 2 ▶ return 0 unless _compare_obj($h1->{$_}, $h2->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless _compare_obj($h1->{$_}, $h2->{$_}) 1 1 2
1105 }
1106 0 return 1;
1107 }
1108  
1109 0 ▶sub _rand_arr {
Subroutine _rand_arr: not called CC=4 SCAR=30.0
1110 0 my $sz = shift;
1111 0 my @arr;
1112 0 for (1..$sz) {
1113 0 my $len = int(rand(10)) + 1;
1114 0 ▶ my $item = rand(1) < 0.9 ? _random_uchar()x($len*5) : rand(1) < 0.5 ? _rand_arr($len) : _rand_hash($len);
Branch: 2 branches
BranchTrueFalseTotal
rand 1 < 0.9 ? : 0 0 0
rand 1 < 0.5 ? : 0 0 0
1115 0 push @arr, $item;
1116 }
1117 0 return \@arr;
1118 }
1119  
1120 0 ▶sub _rand_hash {
Subroutine _rand_hash: not called CC=4 SCAR=30.0
1121 0 my $sz = shift;
1122 0 my %hash;
1123 0 for (1..$sz) {
1124 0 my $len = int(rand(10)) + 1;
1125 0 ▶ my $item = rand(1) < 0.9 ? _random_uchar()x($len*5) : rand(1) < 0.5 ? _rand_arr($len) : _rand_hash($len);
Branch: 2 branches
BranchTrueFalseTotal
rand 1 < 0.9 ? : 0 0 0
rand 1 < 0.5 ? : 0 0 0
1126 0 $hash{_random_uchar()x($len*4)} = $item;
1127 }
1128 0 return \%hash;
1129 }
1130  
1131 # modified from https://github.com/famzah/langs-performance/blob/master/primes.pl
1132 0 ▶sub _get_primes {
Subroutine _get_primes: not called CC=5 SCAR=34.0
1133 0 my $n = shift;
1134 0 my @s = ();
1135 0 ▶ for (my $i = 3; $i < $n + 1; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n + 1) 0 0 0
1136 0 push(@s, $i);
1137 }
1138 0 my $mroot = $n**0.5;
1139 0 my $half = scalar @s;
1140 0 my $i = 0;
1141 0 my $m = 3;
1142 0 ▶ while ($m <= $mroot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= $mroot) 0 0 0
1143 0 ▶ if ($s[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s[$i]) 0 0 0
1144 0 ▶ for (my $j = int(($m * $m - 3) / 2); $j < $half; $j += $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $half) 0 0 0
1145 0 $s[$j] = 0;
1146 }
1147 }
1148 0 $i++;
1149 0 $m = 2 * $i + 3;
1150 }
1151  
1152 0 return 2, grep($_, @s);
1153 }
1154  
1155 # Fix for Crypt::JWT that was submitted as a patch. Will remove if it is merged.
1156 2 ▶sub _decode_jwt2 {
Subroutine _decode_jwt2: called CC=4 SCAR=14.0
1157 2 my %args = @_;
1158 2 my ($header, $payload);
1159  
1160 2 ▶ if ($args{token} =~
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"token"} =~ /^([a-zA-Z0-9_-]+)=*\.([a-zA-Z0-9_-]*)=*\.([a-zA-Z0-9_-]*)=*(?:\.([a-zA-Z0-9_-]+)=*\.([a-zA-Z0-9_-]+)=*)?$/) 1 1 2
1161 /^([a-zA-Z0-9_-]+)=*\.([a-zA-Z0-9_-]*)=*\.([a-zA-Z0-9_-]*)=*(?:\.([a-zA-Z0-9_-]+)=*\.([a-zA-Z0-9_-]+)=*)?$/
1162 ) {
1163 1 ▶ if (length($5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $5) { } 0 1 1
1164 # JWE token (5 segments)
1165 0 ($header, $payload) =
1166 Crypt::JWT::_decode_jwe($1, $2, $3, $4, $5, undef, {}, {},
1167 %args);
1168 } else {
1169 # JWS token (3 segments)
1170 1 ($header, $payload) =
1171 Crypt::JWT::_decode_jws($1, $2, $3, {}, %args);
1172 }
1173 }
1174 2 ▶ return ($header, $payload) if $args{decode_header};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"decode_header"} 1 1 2
1175 1 return $payload;
1176 }
1177  
1178 # Helper package for Moose benchmark
1179  
1180 {
1181 package Benchmark::DKbench::MooseTree;
1182  
1183 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
1184  
1185 4 has 'price' => (is => 'rw', default => 10);
1186 4 has 'tax' => (is => 'rw', lazy_build => 1);
1187 4 has 'node' => (is => 'rw', isa => 'Any');
1188 4 has 'parent' => (
1189 is => 'rw',
1190 isa => 'Benchmark::DKbench::MooseTree',
1191 predicate => 'has_parent',
1192 weak_ref => 1,
1193 );
1194 4 has 'left' => (
1195 is => 'rw',
1196 isa => 'Benchmark::DKbench::MooseTree',
1197 predicate => 'has_left',
1198 lazy => 1,
1199 builder => '_build_child_tree',
1200 );
1201 4 has 'right' => (
1202 is => 'rw',
1203 isa => 'Benchmark::DKbench::MooseTree',
1204 predicate => 'has_right',
1205 lazy => 1,
1206 builder => '_build_child_tree',
1207 );
1208 0 ▶ before 'right', 'left' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1209 0 my ($self, $tree) = @_;
1210 0 ▶ $tree->parent($self) if defined $tree;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree 0 0 0
1211 4 };
1212  
1213 0 ▶ sub _build_tax {
Subroutine _build_tax: not called CC=1 SCAR=6.9
1214 0 my $self = shift;
1215 0 $self->price * 0.2;
1216 }
1217  
1218 0 ▶ sub _build_child_tree {
Subroutine _build_child_tree: not called CC=1 SCAR=6.9
1219 0 my $self = shift;
1220 0 return Benchmark::DKbench::MooseTree->new( parent => $self );
1221 }
1222  
1223 0 ▶ sub _cost {
Subroutine _cost: not called CC=1 SCAR=6.9
1224 0 my $self = shift;
1225 0 $self->price + $self->tax;
1226 }
1227 }
1228  
1229 4 1;
↑ Summary DKbench/Setup.pm »