App-BencherUtils

 view release on metacpan or  search on metacpan

lib/App/BencherUtils.pm  view on Meta::CPAN

package App::BencherUtils;

use 5.010001;
use strict 'subs', 'vars';
use warnings;
use Log::ger;

use Data::Clean::ForJSON;
use Function::Fallback::CoreOrPP qw(clone);
use Perinci::Object;
use Perinci::Sub::Util qw(err);
use PerlX::Maybe;
use POSIX qw(strftime);

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-08-24'; # DATE
our $DIST = 'App-BencherUtils'; # DIST
our $VERSION = '0.245'; # VERSION

our %SPEC;

$SPEC{':package'} = {
    v => 1.1,
    summary => 'Utilities related to bencher',
};

my %args_common = (
    result_dir => {
        summary => 'Directory to store results files in',
        schema => 'str*',
        req => 1,
    },
);

my %args_common_query = (
    query => {
        schema => ['array*', of=>'str*'],
        pos => 0,
        greedy => 1,
    },
    detail => {
        schema => 'bool',
        cmdline_aliases => {l=>{}},
    },
);

sub _clean {
    state $cleanser = Data::Clean::ForJSON->get_cleanser;
    $cleanser->clone_and_clean($_[0]);
}

sub _json {
    state $json = do {
        require JSON::MaybeXS;
        my $json = JSON::MaybeXS->new;
        $json->convert_blessed(1);
        $json->allow_nonref(1);
        $json->canonical(1);
    };
    $json;
}

sub _encode_json {
    no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
    no warnings 'once';
    local *version::TO_JSON = sub { "$_[0]" };
    _json->encode($_[0]);
}

sub _complete_scenario_module {
    require Complete::Module;
    my %args = @_;
    Complete::Module::complete_module(
        word=>$args{word}, ns_prefix=>'Bencher::Scenario');
}

my $re_filename = qr/\A
                     (\w+(?:-(?:\w+))*)

lib/App/BencherUtils.pm  view on Meta::CPAN

        (precision => $args{precision}) x !!(defined $args{precision}),
    );
    return $res unless $res->[0] == 200;

    my $r = $args{-cmdline_r};
    return $res if !$r || $r->{format} && $r->{format} !~ /text/;

    [200, "OK", Bencher::Backend::format_result($res),
     {'cmdline.skip_format'=>1}];
}

$SPEC{bencher_for} = {
    v => 1.1,
    summary => 'List distributions that benchmarks specified modules',
    description => <<'_',

This utility consults <prog:lcpan> (local indexed CPAN mirror) to check if there
are distributions that benchmarks a specified module. This is done by checking
the presence of a dependency with the relationship `x_benchmarks`.

_
    args => {
        modules => {
            schema => ['array*', of=>'perl::modname*'],
            req => 1,
            pos => 0,
            greedy => 1,
        },
    },

};
sub bencher_for {
    require App::lcpan::Call;

    my %args = @_;

    my $res = App::lcpan::Call::call_lcpan_script(
        argv => ["rdeps", "--phase", "x_benchmarks", @{ $args{modules} }],
    );

    return $res unless $res->[0] == 200;

    return [200, "OK", [map {$_->{dist}} @{ $res->[2] }]];
}

1;
# ABSTRACT: Utilities related to bencher

__END__

=pod

=encoding UTF-8

=head1 NAME

App::BencherUtils - Utilities related to bencher

=head1 VERSION

This document describes version 0.245 of App::BencherUtils (from Perl distribution App-BencherUtils), released on 2022-08-24.

=head1 SYNOPSIS

=head1 DESCRIPTION

This distribution includes several utilities:

=over

=item * L<bencher-code>

=item * L<bencher-for>

=item * L<bencher-module-startup-overhead>

=item * L<chart-bencher-result>

=item * L<cleanup-old-bencher-results>

=item * L<format-bencher-result>

=item * L<gen-bencher-scenario-from-cpanmodules>

=item * L<list-bencher-results>

=item * L<list-bencher-scenario-modules>

=back

=head1 FUNCTIONS


=head2 bencher_code

Usage:

 bencher_code(%args) -> [$status_code, $reason, $payload, \%result_meta]

Accept a list of codes and perform benchmark.

% bencher-code 'code1' 'code2'

is basically a shortcut for creating a scenario like this:

 {
     participants => [
         {code_template=>'code1'},
         {code_template=>'code2'},
     ],
 }

and running that scenario with C<bencher>.

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<codes>* => I<array[str]>



( run in 0.768 second using v1.01-cache-2.11-cpan-39bf76dae61 )