ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.5
Committed: Sat Mar 24 22:10:09 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.4: +7 -7 lines
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 root 1.5 my $xs = JSON::XS->new->pretty->canonical->shrink;
19 root 1.1
20     my $json; # the test string
21    
22     local $/;
23     $json = <>;
24    
25 root 1.5 srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 2555), 0..2047]);
26 root 1.3
27 root 1.1 #if (1) {
28     # use Storable;
29     # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
30     # my $faces = Storable::thaw do { <$fh> };
31     # $json = objToJson $faces;
32     # open my $fh2, ">:unix", "faces.json" or die "$!";
33     # print $fh2 $json;
34     # warn length $json;
35     #}
36    
37     my %tst = (
38 root 1.5 # "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
39     # "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'],
40     # "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
41     # "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'],
42 root 1.1 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
43     "JSON::XS/2" => ['$xs->encode ($perl)' , '$xs->decode ($json)'],
44     );
45    
46     sub bench($) {
47     my ($code) = @_;
48    
49     my $perl = jsonToObj $json;
50 root 1.4 my $count = 20;
51 root 1.5 my $times = 25;
52 root 1.1
53     my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
54     $cent->();
55    
56     my @meas;
57    
58     while () {
59     push @meas, Time::HiRes::time;
60     $cent->();
61     $meas[-1] = Time::HiRes::time - $meas[-1];
62    
63     my $mean = (List::Util::sum @meas) / @meas;
64     if (@meas >= $times) {
65     @meas = grep $_ > $mean * 0.9 && $_ < $mean * 1.1, @meas;
66     my @meas = grep $_ > $mean * 0.9999 && $_ < $mean * 1.1111, @meas;
67     if (@meas >= $times) {
68     return $count / $mean;
69     }
70     }
71     }
72     }
73    
74     printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
75     printf "-----------|------------|------------|\n";
76     for my $module (sort keys %tst) {
77     my $enc = bench $tst{$module}[0];
78     my $dec = bench $tst{$module}[1];
79    
80     printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
81     }
82     printf "-----------+------------+------------+\n";
83