1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # Usage: bench json-file |
3 | # Usage: bench json-file |
|
|
4 | |
|
|
5 | # which modules to test (JSON usually excluded because its so slow) |
|
|
6 | my %tst = ( |
|
|
7 | "JSON" => ['objToJson $perl' , 'jsonToObj $json'], |
|
|
8 | "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'], |
|
|
9 | "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'], |
|
|
10 | "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'], |
|
|
11 | "JSON::XS" => ['to_json $perl' , 'from_json $json'], |
|
|
12 | "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'], |
|
|
13 | "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'], |
|
|
14 | "Storable" => ['Storable::nfreeze $perl', 'Storable::thaw $pst'], |
|
|
15 | ); |
4 | |
16 | |
5 | use JSON; |
17 | use JSON; |
6 | use JSON::DWIW; |
18 | use JSON::DWIW; |
7 | use JSON::PC; |
19 | use JSON::PC; |
8 | use JSON::XS qw(to_json from_json); |
20 | use JSON::XS qw(to_json from_json); |
… | |
… | |
37 | # open my $fh2, ">:unix", "faces.json" or die "$!"; |
49 | # open my $fh2, ">:unix", "faces.json" or die "$!"; |
38 | # print $fh2 $json; |
50 | # print $fh2 $json; |
39 | # warn length $json; |
51 | # warn length $json; |
40 | #} |
52 | #} |
41 | |
53 | |
42 | my %tst = ( |
|
|
43 | # "JSON" => ['objToJson $perl' , 'jsonToObj $json'], |
|
|
44 | "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'], |
|
|
45 | "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'], |
|
|
46 | "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'], |
|
|
47 | "JSON::XS" => ['to_json $perl' , 'from_json $json'], |
|
|
48 | "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'], |
|
|
49 | "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'], |
|
|
50 | "Storable" => ['Storable::nfreeze $perl', 'Storable::thaw $pst'], |
|
|
51 | ); |
|
|
52 | |
|
|
53 | sub bench($) { |
54 | sub bench($) { |
54 | my ($code) = @_; |
55 | my ($code) = @_; |
55 | |
56 | |
56 | my $perl = jsonToObj $json; |
57 | my $perl = jsonToObj $json; |
57 | my $pst = Storable::nfreeze $perl; |
58 | my $pst = Storable::nfreeze $perl; |
58 | |
59 | |
59 | my $count = 2; |
60 | my $count = 5; |
60 | my $times = 25; |
61 | my $times = 200; |
61 | |
62 | |
62 | my $cent = eval "sub { " . (join ";", ($code) x $count) . "}"; |
63 | my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }"; |
63 | $cent->(); |
64 | $cent->(); |
64 | |
65 | |
65 | my @meas; |
66 | my $min = 1e99; |
66 | |
67 | |
67 | while () { |
68 | for (1..$times) { |
68 | push @meas, Time::HiRes::time; |
|
|
69 | $cent->(); |
69 | my $t = $cent->(); |
70 | $meas[-1] = Time::HiRes::time - $meas[-1]; |
|
|
71 | |
70 | |
72 | my $mean = (List::Util::sum @meas) / @meas; |
71 | $min = $t if $t < $min; |
73 | if (@meas >= $times) { |
|
|
74 | @meas = grep $_ > $mean * 0.9 && $_ < $mean * 1.1, @meas; |
|
|
75 | my @meas = grep $_ > $mean * 0.9999 && $_ < $mean * 1.1111, @meas; |
|
|
76 | if (@meas >= $times) { |
|
|
77 | return $count / $mean; |
|
|
78 | } |
|
|
79 | } |
|
|
80 | } |
72 | } |
|
|
73 | |
|
|
74 | return $count / $min; |
81 | } |
75 | } |
82 | |
76 | |
83 | printf "%-10s | %10s | %10s |\n", "module", "encode", "decode"; |
77 | printf "%-10s | %10s | %10s |\n", "module", "encode", "decode"; |
84 | printf "-----------|------------|------------|\n"; |
78 | printf "-----------|------------|------------|\n"; |
85 | for my $module (sort keys %tst) { |
79 | for my $module (sort keys %tst) { |