File Coverage

File:bin/benchmark-generator
Coverage:84.8%

linestmtbrancondsubtimecode
1#!/usr/bin/env perl
2
3
8
8
8
13188
6
98
use strict;
4
8
8
8
8
5
119
use warnings;
5
8
8
8
1097
48781
13
use autodie qw(:all);
6
7
8
8
8
59236
7
128
use App::Test::Generator::BenchmarkGenerator;
8
8
8
8
2159
26796
10
use Getopt::Long qw(GetOptions);
9
8
8
8
1880
149609
366
use Pod::Usage;
10
8
8
8
1089
7492
916
use YAML::XS qw(LoadFile);
11
12 - 59
=head1 NAME

benchmark-generator - Generate Benchmark harnesses from ATG schema files

=head1 SYNOPSIS

  benchmark-generator -i schemas/my_func.yml
  benchmark-generator -i schemas/abs.yml -o benchmarks/abs.pl
  benchmark-generator -i schemas/abs.yml -r

=head1 DESCRIPTION

Reads an ATG YAML schema (the same format used by C<fuzz-harness-generator>)
and emits a self-contained Perl benchmark script using L<Benchmark/cmpthese>.

Each transform in the schema becomes one variant in the C<cmpthese> call.
When no transforms are defined a single C<'default'> variant is generated.
Representative input values are derived from each parameter's type and
range constraints.

=head1 OPTIONS

=over 4

=item B<--input|-i> FILE

The schema YAML file to read (required unless positional argument given).

=item B<--output|-o> FILE

Output file for the generated benchmark.  Defaults to STDOUT.

=item B<--run|-r>

Execute the generated benchmark with C<perl> immediately after generating it.
Implies a temporary output file when B<--output> is not given.

=item B<--version|-V>

Print the L<App::Test::Generator> version and exit.

=item B<--help|-h>

Show this help message and exit.

=back

=cut
60
61
8
429314
my ($infile, $outfile, $run, $help, $version);
62
63
8
24
Getopt::Long::Configure('bundling');
64
8
150
GetOptions(
65        'input|i=s'   => \$infile,
66        'output|o=s'  => \$outfile,
67        'run|r'       => \$run,
68        'help|h'      => \$help,
69        'version|V'   => \$version,
70) or pod2usage(2);
71
72
8
2747
pod2usage(-exitval => 0, -verbose => 1) if $help;
73
74
7
8
if($version) {
75
1
4
        print $App::Test::Generator::BenchmarkGenerator::VERSION, "\n";
76
1
0
        exit 0;
77}
78
79
6
10
$infile //= shift @ARGV;
80
6
5
pod2usage(-message => 'ERROR: no input schema given', -exitval => 2)
81        unless defined $infile;
82
83
6
13
my $schema = LoadFile($infile);
84
85
5
445
my $bg     = App::Test::Generator::BenchmarkGenerator->new(schema => $schema);
86
5
8
my $source = $bg->generate();
87
88
5
9
if($outfile) {
89
2
4
        open my $fh, '>', $outfile;
90
2
1226
        print $fh $source;
91
2
4
        close $fh;
92
2
0
        print "Wrote benchmark to $outfile\n";
93} elsif($run) {
94
0
0
        require File::Temp;
95
0
0
        my $tmp = File::Temp->new(SUFFIX => '.pl', UNLINK => 1);
96
0
0
        print $tmp $source;
97
0
0
        close $tmp;
98
0
0
        system($^X, "$tmp");
99} else {
100
3
0
        print $source;
101}