bench: Re-run with more time + JSON::Tiny & XMLWriter benchmarks

This commit is contained in:
Yorhel 2025-02-19 12:57:33 +01:00
parent 9014e2900c
commit 63f524622b
3 changed files with 321 additions and 168 deletions

View file

@ -19,8 +19,12 @@ my %modules = map +($_, eval "require $_; \$${_}::VERSION"), qw/
FU
Cpanel::JSON::XS
JSON::PP
JSON::XS
JSON::SIMD
JSON::Tiny
JSON::XS
TUWF::XML
HTML::Tiny
XML::Writer
/;
my %data; # "id x y" => { id x y rate exists }
@ -52,13 +56,14 @@ sub def($id, $text, $xs, @ys) {
my($y, $m, @sub) = @$ya;
$m ||= $y;
for my($i, $x) (builtin::indexed @$xs) {
next if !$sub[$i];
my $d = "$id $x $y";
$data{$d} ||= { id => $id, x => $x, y => $y };
$d = $data{$d};
$d->{exists} = 1;
delete $d->{rate} if !$oldmodules{$m} || $modules{$m} ne $oldmodules{$m};
if (!exists $d->{rate}) {
my $o = timethis -1, $sub[$i], 0, 'none';
my $o = timethis -5, $sub[$i], 0, 'none';
$d->{rate} = sprintf '%.0f', $o->iters/$o->real;
printf "%-20s%-12s%-20s%10d/s\n", $id, $x, $y, $d->{rate};
}
@ -85,6 +90,7 @@ sub defjson($name, $canon, $text, $val) {
my $enc = json_format $val;
def "json/$name", $text, [ 'Encode', $canon ? 'Canonical' : (), 'Decode' ],
[ 'JSON::PP', undef, sub { $pp->encode($val) }, $canon ? sub { $c_pp->encode($val) } : (), sub { $pp->decode($enc) } ],
[ 'JSON::Tiny', undef, sub { JSON::Tiny::to_json($val) }, $canon ? undef : (), sub { JSON::Tiny::from_json($enc) } ],
[ 'Cpanel::JSON::XS', undef, sub { $cp->encode($val) }, $canon ? sub { $c_cp->encode($val) } : (), sub { $cp->decode($enc) } ],
[ 'JSON::SIMD', undef, sub { $si->encode($val) }, $canon ? sub { $c_si->encode($val) } : (), sub { $si->decode($enc) } ],
[ 'JSON::XS', undef, sub { $xs->encode($val) }, $canon ? sub { $c_xs->encode($val) } : (), sub { $xs->decode($enc) } ],
@ -110,6 +116,86 @@ defjson strel => 0, 'String escaping (many)', [ map "This \" \\ needs \b\x01\x02
package BENCH::TUWFXML {
use TUWF::XML ':html5_', 'xml_string';
sub f($id) {
li_ class => $id % 2 ? 'one' : undef, '+', $id % 5 > 2 ? 'two' : undef, sub {
small_ '--'x($id % 50).' ' if $id % 3;
a_ href => "/$id",
class => $id % 7 > 2 ? 'another-class' : undef,
'+' => $id % 9 < 7 ? 'and-another-one' : undef,
style => "width: ${id}px",
$id;
};
}
sub t { xml_string sub { div_ sub { f $_ for (1..100) } } }
}
package BENCH::XMLWriter {
my $wr;
sub f($id) {
$wr->startTag(li => class => join(' ', $id % 2 ? 'one' : (), $id % 5 > 2 ? 'two' : ()));
$wr->dataElement(small => '--'x($id % 50).' ') if $id % 3;
$wr->dataElement(a => $id, href => "/$id", class => join(' ',
$id % 7 > 2 ? 'another-class' : (),
$id % 9 < 7 ? 'and-another-one' : ()
), style => "width: ${id}px");
$wr->endTag();
}
sub t {
$wr = XML::Writer->new(OUTPUT => \my $str, UNSAFE => 1);
$wr->startTag('div');
f $_ for (1..100);
$wr->endTag();
}
}
package BENCH::HTMLTiny {
my $h;
sub f($id) {
$h->li({ class => join(' ', $id % 2 ? 'one' : (), '+', $id % 5 > 2 ? 'two' : ()) }, [
$id % 3 ? $h->small('--'x($id % 50).' ') : '',
$h->a({
href => "/$id",
class => join (' ',
$id % 7 > 2 ? 'another-class' : (),
$id % 9 < 7 ? 'and-another-one' : (),
),
style => "width: ${id}px"
}, $id),
]);
}
sub t {
$h = HTML::Tiny->new;
$h->div(map f($_), 1..100);
}
}
package BENCH::FUXMLWriter {
use FU::XMLWriter ':html5_', 'fragment';
sub f($id) {
li_ class => $id % 2 ? 'one' : undef, '+', $id % 5 > 2 ? 'two' : undef, sub {
small_ '--'x($id % 50).' ' if $id % 3;
a_ href => "/$id",
class => $id % 7 > 2 ? 'another-class' : undef,
'+' => $id % 9 < 7 ? 'and-another-one' : undef,
style => "width: ${id}px",
$id;
};
}
sub t { fragment { div_ sub { f $_ for (1..100) } } }
}
def 'xml/a', 'HTML fragment', [ 'Rate' ],
[ 'TUWF::XML', undef, \&BENCH::TUWFXML::t ],
[ 'XML::Writer', undef, \&BENCH::XMLWriter::t ],
[ 'HTML::Tiny', undef, \&BENCH::HTMLTiny::t ],
[ 'FU::XMLWriter', 'FU', \&BENCH::FUXMLWriter::t ];
delete @data{ grep !$data{$_}{exists}, keys %data };
sub fmtbench($id, $text, $xs, $ys) {
@ -124,7 +210,10 @@ sub fmtbench($id, $text, $xs, $ys) {
my ($y, $m, @ys) = @$yr;
$m ||= $y;
$r .= sprintf '%18s', $y;
$r .= sprintf '%10d/s', $data{"$id $xs->[$_] $y"}{rate} for (0..$#$xs);
for my $i (0..$#$xs) {
my $d = $data{"$id $xs->[$i] $y"};
$r .= $d ? sprintf '%10d/s', $d->{rate} : sprintf '%12s', '-';
}
$r .= "\n";
}
"$r\n"
@ -190,6 +279,10 @@ SIMD parts are only used for parsing.
:benches ^json
=head2 XML Writing
:benches ^xml
=cut
# Cached data used by bench.PL. Same as the formatted tables above but easier to parse.