ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.1
Committed: Thu Mar 22 21:13:58 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1, rel-0_2
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     # Usage: bench json-file
4    
5     use JSON;
6     use JSON::DWIW;
7     use JSON::PC;
8     use JSON::XS;
9     use JSON::Syck;
10    
11     use Time::HiRes;
12     use List::Util;
13    
14     use utf8;
15    
16     my $dwiw = new JSON::DWIW;
17     my $pc = new JSON::PC;
18     my $xs = JSON::XS->new->pretty (1)->canonical (1);
19    
20     my $json; # the test string
21    
22     local $/;
23     $json = <>;
24    
25     #if (1) {
26     # use Storable;
27     # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
28     # my $faces = Storable::thaw do { <$fh> };
29     # $json = objToJson $faces;
30     # open my $fh2, ">:unix", "faces.json" or die "$!";
31     # print $fh2 $json;
32     # warn length $json;
33     #}
34    
35     my %tst = (
36     "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'],
37     "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'],
38     "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
39     "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
40     "JSON::XS" => ['to_json $perl' , 'from_json $json'],
41     "JSON::XS/2" => ['$xs->encode ($perl)' , '$xs->decode ($json)'],
42     );
43    
44     sub bench($) {
45     my ($code) = @_;
46    
47     my $perl = jsonToObj $json;
48     my $count = 100;
49     my $times = 15;
50    
51     my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
52     $cent->();
53    
54     my @meas;
55    
56     while () {
57     push @meas, Time::HiRes::time;
58     $cent->();
59     $meas[-1] = Time::HiRes::time - $meas[-1];
60    
61     my $mean = (List::Util::sum @meas) / @meas;
62     if (@meas >= $times) {
63     @meas = grep $_ > $mean * 0.9 && $_ < $mean * 1.1, @meas;
64     my @meas = grep $_ > $mean * 0.9999 && $_ < $mean * 1.1111, @meas;
65     if (@meas >= $times) {
66     return $count / $mean;
67     }
68     }
69     }
70     }
71    
72     printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
73     printf "-----------|------------|------------|\n";
74     for my $module (sort keys %tst) {
75     my $enc = bench $tst{$module}[0];
76     my $dec = bench $tst{$module}[1];
77    
78     printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
79     }
80     printf "-----------+------------+------------+\n";
81