| 1 | #!/usr/bin/env perl | ||
| 2 | # Benchmark: DMS::Parser (pure Perl) vs DMS::XS::Parser (C-backed). | ||
| 3 | # | ||
| 4 | # Reads a handful of representative fixtures from the conformance corpus | ||
| 5 | # and runs each through both parsers under Benchmark::cmpthese. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use FindBin; | ||
| 10 | use lib "$FindBin::Bin/../"; # pure-Perl DMS::Parser lives in ../ | ||
| 11 | use lib "$FindBin::Bin/blib/lib"; | ||
| 12 | use lib "$FindBin::Bin/blib/arch"; | ||
| 13 | use DMS::Parser; | ||
| 14 | use DMS::XS::Parser; | ||
| 15 | use Benchmark qw(cmpthese timethese); | ||
| 16 | |||
| 17 | my $ROOT = "$FindBin::Bin/../../.."; | ||
| 18 | |||
| 19 | # Representative fixtures covering small / medium / large / tier-1 features. | ||
| 20 | my @fixtures = ( | ||
| 21 | ["tiny int", "$ROOT/tests/valid/int-dec/v0000.dms"], | ||
| 22 | ["small table", "$ROOT/tests/valid/combo/quad-000.dms"], | ||
| 23 | ["flat-500 (big)", "$ROOT/tests/valid/stress/flat-500.dms"], | ||
| 24 | ["flow-array-1000", "$ROOT/tests/valid/stress/flow-array-1000.dms"], | ||
| 25 | ["depth-50", "$ROOT/tests/valid/stress/depth-50.dms"], | ||
| 26 | ["frontmatter keys-200","$ROOT/tests/valid/frontmatter-stress/keys-200.dms"], | ||
| 27 | ["tier1 pure mod", "$ROOT/tests/valid/tier1-user-mod/chain-upper-then-trim.dms"], | ||
| 28 | ); | ||
| 29 | |||
| 30 | # Filter to what's actually present. | ||
| 31 | @fixtures = grep { -f $_->[1] } @fixtures; | ||
| 32 | |||
| 33 | # Build shared config with pure modifiers so tier-1 fixtures work on both. | ||
| 34 | my $pure_cfg = DMS::ParserConfig->new; | ||
| 35 | $pure_cfg->register_pure('my_upper', \&mod_my_upper); | ||
| 36 | $pure_cfg->register_pure('my_repeat', \&mod_my_repeat); | ||
| 37 | |||
| 38 | my $xs_cfg = DMS::XS::ParserConfig->new; | ||
| 39 | $xs_cfg->register_pure('my_upper', \&mod_my_upper); | ||
| 40 | $xs_cfg->register_pure('my_repeat', \&mod_my_repeat); | ||
| 41 | |||
| 42 | for my $f (@fixtures) { | ||
| 43 | my ($label, $path) = @$f; | ||
| 44 | open my $fh, '<', $path or do { warn "skip $path: $!\n"; next }; | ||
| 45 | local $/; | ||
| 46 | my $src = <$fh>; | ||
| 47 | close $fh; | ||
| 48 | my $bytes = length $src; | ||
| 49 | |||
| 50 | print "=" x 60, "\n"; | ||
| 51 | printf "%s (%d bytes)\n", $label, $bytes; | ||
| 52 | print "=" x 60, "\n"; | ||
| 53 | |||
| 54 | # Validate both parse successfully before benchmarking. | ||
| 55 | my $ok = 1; | ||
| 56 | for my $pair ( | ||
| 57 | ['pure', sub { DMS::Parser::parse_document_with_config($src, $pure_cfg) }], | ||
| 58 | ['xs', sub { DMS::XS::Parser::parse_document_with_config($src, $xs_cfg) }], | ||
| 59 | ) { | ||
| 60 | eval { $pair->[1]->() }; | ||
| 61 | if ($@) { warn " $pair->[0] failed to parse: $@"; $ok = 0 } | ||
| 62 | } | ||
| 63 | unless ($ok) { print " (skipped: parse failure)\n\n"; next } | ||
| 64 | |||
| 65 | # Auto-tune iteration count: more iterations for faster fixtures. | ||
| 66 | cmpthese(-2, { | ||
| 67 | pure => sub { DMS::Parser::parse_document_with_config($src, $pure_cfg) }, | ||
| 68 | xs => sub { DMS::XS::Parser::parse_document_with_config($src, $xs_cfg) }, | ||
| 69 | }); | ||
| 70 | print "\n"; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub mod_my_upper { | ||
| 74 | my ($input, $params) = @_; | ||
| 75 | die "my_upper takes no arguments" | ||
| 76 | if defined($params) && ref($params) eq 'ARRAY' && @$params; | ||
| 77 | die "my_upper expects a string value" if ref($input) ne ''; | ||
| 78 | return uc($input); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub mod_my_repeat { | ||
| 82 | my ($input, $params) = @_; | ||
| 83 | die "my_repeat expects one non-negative integer argument" | ||
| 84 | unless defined($params) && ref($params) eq 'ARRAY' && @$params == 1; | ||
| 85 | my $arg = $params->[0]; | ||
| 86 | die "my_repeat expects one non-negative integer argument" | ||
| 87 | unless ref($arg) eq 'DMS::Integer'; | ||
| 88 | my $bn = $arg->value; | ||
| 89 | die "my_repeat expects one non-negative integer argument" | ||
| 90 | if $bn->is_neg; | ||
| 91 | my $n = int($bn->bstr); | ||
| 92 | die "my_repeat expects a string value" if ref($input) ne ''; | ||
| 93 | return $input x $n; | ||
| 94 | } |