perlancar

NAME

bencher - A benchmark framework (CLI)

VERSION

This document describes version 1.028 of bencher (from Perl distribution Bencher), released on 2017-01-25.

SYNOPSIS

List all scenario modules (Bencher::Scenario::*) installed locally on your system:

 % bencher --list-scenario-modules
 % bencher -L

Run benchmark described by a scenario module:

 % bencher -m Example

Run benchmark described by a scenario file:

 % bencher -f scenario.pl

Add participants from the command-line instead of (or in addition to) those specified in a scenario file/module:

 % bencher -p '{"fcall_template":"Bar::func(<arg>)"}'

Run module startup overhead benchmark instead of the normal benchmark:

 % bencher -m Example --module-startup

Show/dump scenario instead of running benchmark:

 % bencher -m Example --show-scenario

List participants instead of running benchmark:

 % bencher ... --list-participants
 % bencher ... --list-participants -l ;# show detail

List participating Perl modules (modules mentioned by all the participants):

 % bencher ... --list-participant-modules
 % bencher ... --list-participant-modules -l ;# show detail

List datasets instead of running benchmark:

 % bencher ... --list-datasets
 % bencher ... --list-datasets -l ;# show detail

List items instead of running benchmark:

 % bencher ... --list-items
 % bencher ... --list-items -l ;# show detail

Show items' codes instead of running benchmark:

 % bencher ... --show-items-codes

Show items' results instead of running benchmark:

 % bencher ... --show-items-results

Select (include/exclude) participants before running benchmark (you can also select datasets/modules/items):

 % bencher ... --include-participant-pattern 'Tiny|Lite' --exclude-participant 'HTTP::Tiny'

Benchmarking against multiple perls

You need to install App::perlbrew first and then install some perls. Also, install at least Bencher::Backend to each perl you want to run the benchmark on.

To list available perls (same as perlbrew list, but also shows whether a perl has Bencher):

 % bencher --list-perls
 % bencher --list-perls -l

To run a scenario against all perls which have Bencher:

 % bencher -m ScenarioModule --multiperl ...

To run a scenario against some perls:

 % bencher -m ScenarioModule --multiperl --include-perl perl-5.20.3 --include-perl perl-5.22.1 ...

Benchmarking multiple versions of a module

For example, if version 0.02 of a module is installed and you want to benchmark against version 0.01 (in /my/home/lib):

 % bencher -m ScenarioModule --multimodver Module::Name -I /my/home/lib ...

Note that Module::Name must be among the modules that are being benchmarked (according to the scenario).

DESCRIPTION

Bencher is a benchmark framework. You specify a scenario (either in a Bencher::Scenario::* Perl module, or a Perl script, or over the command-line) containing list of participants and datasets. Participants are codes or commands to run, and datasets are arguments for the codes/commands. Bencher will permute the participants and datasets into benchmark items, ready to run.

You can choose to include only some participants, datasets, or items. And there are options to view your scenario's participants/datasets/items/mentioned modules, run benchmark against multiple perls and module versions, and so on. Bencher comes as a CLI script as well as Perl module. See the Bencher::Backend documentation for more information.

OPTIONS

* marks required options.

Main options

--capture-stderr

Trap output to stderr.

--capture-stdout

Trap output to stdout.

--dataset=s

Add a dataset.

See --datasets.

--datasets-json=s

Add datasets (JSON-encoded).

See --datasets.

--datasets=s

Add datasets.

--detail, -l
--env-hash=s

Add an environment hash.

See --env-hashes.

--env-hashes-json=s

Add environment hashes (JSON-encoded).

See --env-hashes.

--env-hashes=s

Add environment hashes.

--include-path-json=s, -I

Additional module search paths (JSON-encoded).

See --include-path.

--include-path=s@

Add path to module search path.

Used when searching for scenario module, or when in multimodver mode.

Can be specified multiple times.

--multimodver=s

Benchmark multiple module versions.

If set to a module name, will search for all (instead of the first occurrence) of the module in `@INC`. Then will generate items for each version.

Currently only one module can be multi version.

--multiperl

Benchmark against multiple perls.

Requires <pm:App::perlbrew> to be installed. Will use installed perls from the perlbrew installation. Each installed perl must have <pm:Bencher::Backend> module installed (in addition to having all modules that you want to benchmark, obviously).

By default, only perls having Bencher::Backend will be included. Use `--include-perl` and `--exclude-perl` to include and exclude which perls you want.

Also note that due to the way this is currently implemented, benchmark code that contains closures (references to variables outside the code) won't work.

--on-failure=s

What to do when there is a failure.

Valid values:

 ["die","skip"]

For a command participant, failure means non-zero exit code. For a Perl-code participant, failure means Perl code dies or (if expected result is specified) the result is not equal to the expected result.

The default is "die". When set to "skip", will first run the code of each item before benchmarking and trap command failure/Perl exception and if that happens, will "skip" the item.

--on-result-failure=s

What to do when there is a result failure.

Valid values:

 ["die","skip","warn"]

This is like `on_failure` except that it specifically refer to the failure of item's result not being equal to expected result.

There is an extra choice of `warn` for this type of failure, which is to print a warning to STDERR and continue.

--participant=s

Add a participant.

See --participants.

--participants-json=s

Add participants (JSON-encoded).

See --participants.

--participants=s

Add participants.

--precision-limit=f

Set precision limit.

Instead of setting `precision` which forces a single value, you can also set this `precision_limit` setting. If the precision in the scenario is higher (=number is smaller) than this limit, then this limit is used. For example, if the scenario specifies `default_precision` 0.001 and `precision_limit` is set to 0.005 then 0.005 is used.

This setting is useful on slower computers which might not be able to reach the required precision before hitting maximum number of iterations.

--precision=f

Precision.

When benchmarking with the default <pm:Benchmark::Dumb> runner, will pass the precision to it. The value is a fraction, e.g. 0.5 (for 5% precision), 0.01 (for 1% precision), and so on. Or, it can also be a positive integer to speciify minimum number of iterations, usually need to be at least 6 to avoid the "Number of initial runs is very small (<6)" warning. The default precision is 0, which is to let Benchmark::Dumb determine the precision, which is good enough for most cases.

When benchmarking with <pm:Benchmark> runner, will pass this value as the $count argument. Which can be a positive integer to mean the number of iterations to do (e.g. 10, or 100). Or, can also be set to a negative number (e.g. -0.5 or -2) to mean minimum number of CPU seconds. The default is -0.5.

When benchmarking with <pm:Benchmark::Dumb::SimpleTime>, this value is a positive integer which means the number of iterations to perform.

This setting overrides `default_precision` property in the scenario.

--raw

Show "raw" data.

When action=show-items-result, will print result as-is instead of dumping as Perl.

--runner=s

Runner module to use.

Valid values:

 ["Benchmark::Dumb","Benchmark","Benchmark::Dumb::SimpleTime"]

The default is `Benchmark::Dumb` which should be good enough for most cases.

You can use `Benchmark` runner (`Benchmark.pm`) if you are accustomed to it and want to see its output format.

You can use `Benchmark::Dumb::SimpleTime` if your participant code runs for at least a few to many seconds and you want to use very few iterations (like 1 or 2) because you don't want to wait for too long.

--save-result

Whether to save benchmark result to file.

Will also be turned on automatically if `BENCHER_RESULT_DIR` environment variabl is defined.

When this is turned on, will save a JSON file after benchmark, containing the result along with metadata. The directory of the JSON file will be determined from the `results_dir` option, while the filename from the `results_filename` option.

--scenario-file=s, -f

Load a scenario from a Perl file.

Perl file will be do()'ed and the last expression should be a hash containing the scenario specification.

--scenario-module=s, -m

Load a scenario from a Bencher::Scenario:: Perl module.

Will try to load module `Bencher::Scenario::<NAME>` and expect to find a package variable in the module called `$scenario` which should be a hashref containing the scenario specification.

--test

Whether to test participant code once first before benchmarking.

By default, participant code is run once first for testing (e.g. whether it dies or return the correct result) before benchmarking. If your code runs for many seconds, you might want to skip this test and set this to 0.

--with-args-size

Also return memory usage of item's arguments.

Memory size is measured using <pm:Devel::Size>.

--with-process-size

Also return process size information for each item.

This is done by dumping each item's code into a temporary file and running the file with a new perl interpreter process and measuring the process size at the end (so it does not need to load Bencher itself or the other items). Currently only works on Linux because process size information is retrieved from `/proc/PID/smaps`. Not all code can work, e.g. if the code tries to access a closure or outside data or extra modules (modules not specified in the participant or loaded by the code itself). Usually does not make sense to use this on external command participants.

--with-result-size

Also return memory usage of each item code's result (return value).

Memory size is measured using <pm:Devel::Size>.

-d=s

Add a dataset.

See --datasets.

-p=s

Add a participant.

See --participants.

Action options

--action=s, -a

Default value:

 "bench"

Valid values:

 ["list-perls","list-scenario-modules","show-scenario","list-participants","list-participant-modules","list-datasets","list-items","show-items-codes","show-items-results","show-items-results-sizes","show-items-outputs","bench"]
--list-datasets

Shortcut for -a list-datasets.

See --action.

--list-items

Shortcut for -a list-items.

See --action.

--list-participant-modules

Shortcut for -a list-participant-modules.

See --action.

--list-participants

Shortcut for -a list-participants.

See --action.

--list-perls

Shortcut for -a list-perls.

See --action.

--list-permutes

Shortcut for -a list-permutes.

See --action.

--list-scenario-modules

Shortcut for -a list-scenario-modules.

See --action.

--module-startup

Benchmark module startup overhead instead of normal benchmark.

--show-items-codes

Shortcut for -a show-items-codes.

See --action.

--show-items-outputs

Shortcut for -a show-items-outputs.

See --action.

--show-items-results

Shortcut for -a show-items-results.

See --action.

--show-items-results-sizes

Shortcut for -a show-items-results-sizes.

See --action.

--show-scenario

Shortcut for -a show-scenario.

See --action.

-L

Shortcut for -a list-scenario-modules.

See --action.

Configuration options

--config-path=filename

Set path to configuration file.

Can be specified multiple times.

--config-profile=s

Set configuration profile to use.

--no-config

Do not use any configuration file.

Environment options

--no-env

Do not read environment for default options.

Filtering options

--exclude-dataset-names=s

Exclude datasets whose name matches this.

--exclude-dataset-pattern=s

Exclude datasets matching this regex pattern.

--exclude-dataset-seqs=s

Exclude datasets whose sequence number matches this.

--exclude-dataset-tags=s

Exclude datasets whose tag matches this.

You can specify `A & B` to exclude datasets that have _both_ tags A and B.

--exclude-datasets=s

Exclude datasets whose seq/name matches this.

--exclude-function-pattern=s

Exclude function(s) matching this regex pattern.

--exclude-functions=s

Exclude functions specified in this list.

--exclude-item-names=s

Exclude items whose name matches this.

--exclude-item-pattern=s

Exclude items matching this regex pattern.

--exclude-item-seqs=s

Exclude items whose sequence number matches this.

--exclude-items=s

Exclude items whose seq/name matches this.

--exclude-module-pattern=s

Exclude module(s) matching this regex pattern.

--exclude-modules=s

Exclude modules specified in this list.

--exclude-participant-names=s

Exclude participants whose name matches this.

--exclude-participant-pattern=s

Exclude participants matching this regex pattern.

--exclude-participant-seqs=s

Exclude participants whose sequence number matches this.

--exclude-participant-tags=s

Exclude participants whose tag matches this.

You can specify `A & B` to exclude participants that have _both_ tags A and B.

--exclude-participants=s

Exclude participants whose seq/name matches this.

--exclude-perls=s

Exclude some perls.

--exclude-pp-modules, --nopp

Exclude PP (pure-Perl) modules.

--exclude-xs-modules, --noxs

Exclude XS modules.

--include-dataset-names=s

Only include datasets whose name matches this.

--include-dataset-pattern=s

Only include datasets matching this regex pattern.

--include-dataset-seqs=s

Only include datasets whose sequence number matches this.

--include-dataset-tags=s

Only include datasets whose tag matches this.

You can specify `A & B` to include datasets that have _both_ tags A and B.

--include-datasets=s

Only include datasets whose seq/name matches this.

--include-function-pattern=s

Only include functions matching this regex pattern.

--include-functions=s

Only include functions specified in this list.

--include-item-names=s

Only include items whose name matches this.

--include-item-pattern=s

Only include items matching this regex pattern.

--include-item-seqs=s

Only include items whose sequence number matches this.

--include-items=s

Only include items whose seq/name matches this.

--include-module-pattern=s

Only include modules matching this regex pattern.

--include-modules=s

Only include modules specified in this list.

--include-participant-names=s

Only include participants whose name matches this.

--include-participant-pattern=s

Only include participants matching this regex pattern.

--include-participant-seqs=s

Only include participants whose sequence number matches this.

--include-participant-tags=s

Only include participants whose tag matches this.

You can specify `A & B` to include participants that have _both_ tags A and B.

--include-participants=s

Only include participants whose seq/name matches this.

--include-perls=s

Only include some perls.

Format options

--scientific-notation
--sorts=s

Default value:

 ["-time"]

Logging options

--debug

Set log level to debug (note: you also need to set LOG=1 to enable logging, or use DEBUG=1).

--log-level=s

Set log level (note: you also need to set LOG=1 to enable logging).

--quiet

Set log level to quiet (note: you also need to set LOG=1 to enable logging, or use QUIET=1).

--trace

Set log level to trace (note: you also need to set LOG=1 to enable logging, or use TRACE=1).

--verbose

Set log level to info (note: you also need to set LOG=1 to enable logging, or use VERBOSE=1).

Output options

--format=s

Choose output format, e.g. json, text.

Default value:

 undef
--json

Set output format to json.

--naked-res

When outputing as JSON, strip result envelope.

Default value:

 0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd element) as well as result metadata/extra result (4th element) instead of just the result (3rd element). However, sometimes you want just the result, e.g. when you want to pipe the result for more post-processing. In this case you can use `--naked-res` so you just get:

    [1,2,3]

Result options

--note=s

Put additional note in the result.

--result-dir=dirname

Directory to use when saving benchmark result.

Default is from `BENCHER_RESULT_DIR` environment variable, or the home directory.

--result-filename=filename

Filename to use when saving benchmark result.

Default is:

    <NAME>.<yyyy-dd-dd-"T"HH-MM-SS>.json

or, when running in module startup mode:

    <NAME>.module_startup.<yyyy-dd-dd-"T"HH-MM-SS>.json

where <NAME> is scenario module name, or `NO_MODULE` if scenario is not from a module. The `::` (double colon in the module name will be replaced with `-` (dash).

--return-meta

Whether to return extra metadata.

When set to true, will return extra metadata such as platform information, CPU information, system load before & after the benchmark, system time, and so on. This is put in result metadata under `func.*` keys.

The default is to true (return extra metadata) unless when run as CLI and format is text (where the extra metadata is not shown).

Other options

--help, -h, -?

Display help message and exit.

--version, -v

Display program's version and exit.

COMPLETION

The script comes with a companion shell completer script (bencher) for this script.

bash

To activate bash completion for this script, put:

 complete -C bencher bencher

in your bash startup (e.g. ~/.bashrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install shcompgen which allows you to activate completion scripts for several kinds of scripts on multiple shells. Some CPAN distributions (those that are built with Dist::Zilla::Plugin::GenShellCompletion) will even automatically enable shell completion for their included scripts (using shcompgen) at installation time, so you can immediately have tab completion.

tcsh

To activate tcsh completion for this script, put:

 complete bencher 'p/*/`bencher`/'

in your tcsh startup (e.g. ~/.tcshrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install shcompgen (see above).

other shells

For fish and zsh, install shcompgen as described above.

CONFIGURATION FILE

This script can read configuration files. Configuration files are in the format of IOD, which is basically INI with some extra features.

By default, these names are searched for configuration filenames (can be changed using --config-path): ~/.config/bencher.conf, ~/bencher.conf, or /etc/bencher.conf.

All found files will be read and merged.

To disable searching for configuration files, pass --no-config.

You can put multiple profiles in a single file by using section names like [profile=SOMENAME] or [SOMESECTION profile=SOMENAME]. Those sections will only be read if you specify the matching --config-profile SOMENAME.

You can also put configuration for multiple programs inside a single file, and use filter program=NAME in section names, e.g. [program=NAME ...] or [SOMESECTION program=NAME]. The section will then only be used when the reading program matches.

Finally, you can filter a section by environment variable using the filter env=CONDITION in section names. For example if you only want a section to be read if a certain environment variable is true: [env=SOMEVAR ...] or [SOMESECTION env=SOMEVAR ...]. If you only want a section to be read when the value of an environment variable has value equals something: [env=HOSTNAME=blink ...] or [SOMESECTION env=HOSTNAME=blink ...]. If you only want a section to be read when the value of an environment variable does not equal something: [env=HOSTNAME!=blink ...] or [SOMESECTION env=HOSTNAME!=blink ...]. If you only want a section to be read when an environment variable contains something: [env=HOSTNAME*=server ...] or [SOMESECTION env=HOSTNAME*=server ...]. Note that currently due to simplistic parsing, there must not be any whitespace in the value being compared because it marks the beginning of a new section filter or section name.

List of available configuration parameters:

 action (see --action)
 capture_stderr (see --capture-stderr)
 capture_stdout (see --capture-stdout)
 datasets (see --datasets)
 detail (see --detail)
 env_hashes (see --env-hashes)
 exclude_dataset_names (see --exclude-dataset-names)
 exclude_dataset_pattern (see --exclude-dataset-pattern)
 exclude_dataset_seqs (see --exclude-dataset-seqs)
 exclude_dataset_tags (see --exclude-dataset-tags)
 exclude_datasets (see --exclude-datasets)
 exclude_function_pattern (see --exclude-function-pattern)
 exclude_functions (see --exclude-functions)
 exclude_item_names (see --exclude-item-names)
 exclude_item_pattern (see --exclude-item-pattern)
 exclude_item_seqs (see --exclude-item-seqs)
 exclude_items (see --exclude-items)
 exclude_module_pattern (see --exclude-module-pattern)
 exclude_modules (see --exclude-modules)
 exclude_participant_names (see --exclude-participant-names)
 exclude_participant_pattern (see --exclude-participant-pattern)
 exclude_participant_seqs (see --exclude-participant-seqs)
 exclude_participant_tags (see --exclude-participant-tags)
 exclude_participants (see --exclude-participants)
 exclude_perls (see --exclude-perls)
 exclude_pp_modules (see --exclude-pp-modules)
 exclude_xs_modules (see --exclude-xs-modules)
 format (see --format)
 include_dataset_names (see --include-dataset-names)
 include_dataset_pattern (see --include-dataset-pattern)
 include_dataset_seqs (see --include-dataset-seqs)
 include_dataset_tags (see --include-dataset-tags)
 include_datasets (see --include-datasets)
 include_function_pattern (see --include-function-pattern)
 include_functions (see --include-functions)
 include_item_names (see --include-item-names)
 include_item_pattern (see --include-item-pattern)
 include_item_seqs (see --include-item-seqs)
 include_items (see --include-items)
 include_module_pattern (see --include-module-pattern)
 include_modules (see --include-modules)
 include_participant_names (see --include-participant-names)
 include_participant_pattern (see --include-participant-pattern)
 include_participant_seqs (see --include-participant-seqs)
 include_participant_tags (see --include-participant-tags)
 include_participants (see --include-participants)
 include_path (see --include-path)
 include_perls (see --include-perls)
 log_level (see --log-level)
 module_startup (see --module-startup)
 multimodver (see --multimodver)
 multiperl (see --multiperl)
 naked_res (see --naked-res)
 note (see --note)
 on_failure (see --on-failure)
 on_result_failure (see --on-result-failure)
 participants (see --participants)
 precision (see --precision)
 precision_limit (see --precision-limit)
 raw (see --raw)
 result_dir (see --result-dir)
 result_filename (see --result-filename)
 return_meta (see --return-meta)
 runner (see --runner)
 save_result (see --save-result)
 scenario_file (see --scenario-file)
 scenario_module (see --scenario-module)
 scientific_notation (see --scientific-notation)
 sorts (see --sorts)
 test (see --test)
 with_args_size (see --with-args-size)
 with_process_size (see --with-process-size)
 with_result_size (see --with-result-size)

ENVIRONMENT

BENCHER_OPT => str

Specify additional command-line options

BENCHER_RESULT_DIR => str

Set default for --results-dir.

FILES

~/.config/bencher.conf

~/bencher.conf

/etc/bencher.conf

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Bencher.

SOURCE

Source repository is at https://github.com/perlancar/perl-Bencher.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Bencher

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

bencher-tiny if you want a simpler CLI with no non-core dependencies.

Bencher

Bencher::Backend

Bencher::Manual::*

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.