| 1 | package YAML::Tests::Benchmark; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Storable; | ||
| 6 | use Benchmark; | ||
| 7 | |||
| 8 | sub run { | ||
| 9 | my ($class, $yt_opts, $yaml_mods) = @_; | ||
| 10 | |||
| 11 | my $options = $class->parse_options($yt_opts); | ||
| 12 | |||
| 13 | my $default = 0; | ||
| 14 | unless (@$yaml_mods) { | ||
| 15 | @$yaml_mods = qw( YAML YAML::Syck YAML::LibYAML YAML::Tiny ); | ||
| 16 | $default = 1; | ||
| 17 | } | ||
| 18 | |||
| 19 | my @modules = grep { | ||
| 20 | eval("require $_"); !$@ | ||
| 21 | } @$yaml_mods; | ||
| 22 | |||
| 23 | my $reps = 16; | ||
| 24 | my $n_runs = $options->{r}; | ||
| 25 | |||
| 26 | my %struct = ( | ||
| 27 | argh_hash => { map { $_ => "Argh" } 0 .. $reps }, | ||
| 28 | argh_list => [ map { "Argh" } 0 .. $reps ], | ||
| 29 | argh_scalar => ("Argh" x 512) | ||
| 30 | ); | ||
| 31 | |||
| 32 | my $current_struct = \%struct; | ||
| 33 | my $current_frozen = Storable::nfreeze( $current_struct ); | ||
| 34 | |||
| 35 | my $current_yaml = do { | ||
| 36 | no strict 'refs'; | ||
| 37 | &{"$modules[0]::Dump"}( $current_struct ); | ||
| 38 | }; | ||
| 39 | |||
| 40 | # if ($default || grep {$_ eq 'Storable'} @modules) { | ||
| 41 | my %dump_methods = map { | ||
| 42 | "dump " . $_ => eval("sub { ${_}::Dump( \$current_struct ) }") | ||
| 43 | } @modules; | ||
| 44 | |||
| 45 | my %load_methods = map { | ||
| 46 | "load " . $_ => eval("sub { ${_}::Load( \$current_yaml ) }") | ||
| 47 | } @modules; | ||
| 48 | |||
| 49 | my %rt_methods = map { | ||
| 50 | "rt " . $_ => | ||
| 51 | eval("sub { ${_}::Load( ${_}::Dump( \$current_struct ) ) }") | ||
| 52 | } @modules; | ||
| 53 | |||
| 54 | $dump_methods{'dump storable'} = sub { | ||
| 55 | Storable::nfreeze( $current_struct ); | ||
| 56 | }; | ||
| 57 | |||
| 58 | $load_methods{'load storable'} = sub { | ||
| 59 | Storable::thaw( $current_frozen ); | ||
| 60 | }; | ||
| 61 | |||
| 62 | $rt_methods{'rt storable'} = sub { | ||
| 63 | Storable::thaw( Storable::nfreeze( $current_struct )); | ||
| 64 | }; | ||
| 65 | # } | ||
| 66 | |||
| 67 | Benchmark::cmpthese( $n_runs, $_ ) | ||
| 68 | for ( \%dump_methods, \%load_methods, \%rt_methods ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub parse_options { | ||
| 72 | my ($class, $yt_opts) = @_; | ||
| 73 | my $options = { | ||
| 74 | r => 1024 * 32, | ||
| 75 | }; | ||
| 76 | for (@$yt_opts) { | ||
| 77 | if (/^-r(\d+)$/) { | ||
| 78 | $options->{r} = $1; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | return $options; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |