App-Test-Generator

 view release on metacpan or  search on metacpan

lib/App/Test/Generator/BenchmarkGenerator.pm  view on Meta::CPAN

package App::Test::Generator::BenchmarkGenerator;

use 5.036;
use Carp qw(croak);
use Data::Dumper ();
use Params::Get qw(get_params);
use Readonly;
use Scalar::Util qw(looks_like_number);

our $VERSION = '0.52';

Readonly my %TYPE_DEFAULTS => (
	number  => 42,
	integer => 42,
	float   => 3.14,
	string  => "'hello'",
	boolean => 1,
	arrayref => '[]',
	hashref  => '{}',
);

Readonly my $BAREWORD_RE => qr/^[A-Za-z_]\w*\z/a;

# Narrower than looks_like_number(), which also accepts Inf, NaN and
# surrounding whitespace - none of which are valid unquoted Perl source.
Readonly my $NUMERIC_LITERAL_RE => qr/^-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?\z/a;

=encoding utf8

=head1 NAME

App::Test::Generator::BenchmarkGenerator - Generate Benchmark harnesses from ATG schemas

=head1 VERSION

Version 0.52

=head1 SYNOPSIS

Pattern 1: read a schema file and print the benchmark script.

    use App::Test::Generator::BenchmarkGenerator;
    use YAML::XS qw(LoadFile);

    my $schema = LoadFile('schemas/my_func.yml');
    my $bg     = App::Test::Generator::BenchmarkGenerator->new(schema => $schema);
    print $bg->generate();

Pattern 2: write the schema by hand, for a plain function that takes its
arguments in a fixed order, and save the script to a file.

    my $bg = App::Test::Generator::BenchmarkGenerator->new(
        schema => {
            module   => 'List::Util',
            function => 'max',
            input    => {
                first  => { type => 'integer', position => 0, min => 1, max => 9 },
                second => { type => 'integer', position => 1 },
            },
        },
    );

    open my $fh, '>', 'benchmarks/max.pl' or die $!;
    print $fh $bg->generate();    # the call is: List::Util::max(5, 42)
    close $fh;

Pattern 3: compare several kinds of input for an object method.
Each entry under C<transforms> becomes one line in the timing table.

    my $bg = App::Test::Generator::BenchmarkGenerator->new(
        schema => {
            module     => 'My::Shop',
            function   => 'price',
            new        => { currency => 'GBP' },
            input      => { amount => { type => 'number' } },
            transforms => {
                small => { input => { amount => { type => 'number', max => 10 } } },
                large => { input => { amount => { type => 'number', min => 1000 } } },
            },
        },
    );
    my $script = $bg->generate();
    # The script builds:  my $obj = My::Shop->new(currency => 'GBP');
    # and then times:     $obj->price(amount => 9)

lib/App/Test/Generator/BenchmarkGenerator.pm  view on Meta::CPAN


=item Transforms that omit the C<input> key

If a transform's hashref has no C<input> key, C<generate> falls back to the
base schema C<input> spec for that variant.  This is intentional but may
produce identical argument lists across variants if you forgot to add per-
transform input overrides.

=item A transform C<input> replaces the base C<input>. It is not merged.

If the base C<input> has the parameters C<a> and C<b>, and a transform
C<input> has only C<a>, then that variant is called with C<a> alone.
C<b> is not copied from the base.  Repeat every parameter you need inside
each transform.

=item Only plain data is allowed under C<new>

A value under C<new> can be a number, a string, C<undef>, an arrayref or
a hashref, nested to any depth.  A code reference or an object makes
C<generate> croak.

=item How C<undef> is handled

=over 4

=item * C<schema =E<gt> undef> makes C<new> croak with C<schema is required>.

=item * A C<module> or C<function> that is C<undef> makes C<generate> croak.

=item * A missing or C<undef> C<input> or C<transforms> key means "no
parameters" or "no transforms".  This is safe.

=item * C<new =E<gt> undef> (C<new: ~> in YAML) still counts as "has a
constructor".  The script calls C<< Module->new() >> with no arguments.

=item * An C<undef> value B<inside> C<new> is written as the Perl word C<undef>.

=item * A parameter whose specification is C<undef> is passed as the
Perl word C<undef>.

=back

=item The text C<undef> means "no input"

Some ATG schema files contain C<input: undef>.  YAML reads this as the
five-letter string C<"undef">.  C<generate> treats that string, and any
other C<input> or C<transforms> value that is not a hashref, as "no
parameters".  Inside a transform, C<input: undef> gives a call with no
arguments; it does B<not> fall back to the base C<input>.

=item Names that are not plain words are quoted for you

Transform names, parameter names and the keys under C<new> can contain
any characters.  They are quoted and escaped in the generated script when
needed.  Only C<module> and C<function> must be valid Perl names.

=item Which text is written as a number

A string under C<new> is written without quotes only when it is a plain
decimal number such as C<42>, C<-1.5> or C<2e10>.  Everything else,
including C<'Inf'>, C<'NaN'> and C<' 12'>, is written as a quoted string.
Be careful with a leading zero: C<'007'> is written as C<007>, which Perl
reads as an octal number.

=back

=head1 ENCODING

=over 4

=item * C<module> and C<function>

Use ASCII letters, digits, underscores and C<::> only.  Non-ASCII letters
may pass the name check, but the generated script has no C<use utf8> line,
so Perl will not read such names correctly.

=item * Transform names, parameter names and string values under C<new>

Full UTF-8 text, including non-ASCII letters and emojis, is copied into
the script unchanged.  This is safe only if you take two steps yourself:
write the script through a UTF-8 file handle
(C<< open my $fh, '>:encoding(UTF-8)', $path >>), and remember that the
script has no C<use utf8> line, so the running benchmark sees the text as
bytes, not as characters.  For a benchmark that needs real character
strings, add C<use utf8;> to the generated file by hand.

=item * Sample argument values

These are always ASCII (C<42>, C<3.14>, C<'hello'>, C<[]>, C<{}>).
The generator never invents non-ASCII test data.

=item * The return value of C<generate>

A Perl character string.  It is ASCII unless your schema put non-ASCII
text into it as described above.

=back

=head1 LIMITATIONS

=over 4

=item Representative values are heuristic

C<_representative_value> picks a single value based on type and C<min>/C<max>
constraints.  It does not guarantee the chosen value exercises any particular
code path, and it does not use the schema's C<enum> or C<matches> keys.

=item No round-trip with C<extract-schemas>

The generator reads any conforming ATG YAML schema, but it does not verify
that the schema accurately describes the actual function's signature.  If the
schema is stale, the generated benchmark may pass wrong argument types.

=back

=head1 SEE ALSO

=over 4

=item L<Benchmark>



( run in 0.955 second using v1.01-cache-2.11-cpan-60884a8df76 )