summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/XML/benchmark.pl
blob: 0435116055c362dffcaf90e4a892c0aaab1cf487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use Benchmark;
use Getopt::Long;
use File::Basename;
use XML::XPath;
use strict;

$|++;

my @default_drivers = qw(
    LibXSLT
    Sablotron
    );

use vars qw(
        $component $iter $ms $kb_in $kb_out $kb_sec $result $ref_size
        );

my @getopt_args = (
        'c=s', # config file
        'n=i', # number of benchmark times
        'd=s@', # drivers
        't', # only 1 iteration per test
        'v', # verbose
        'h', # help
        'x', # XSLTMark emulation
        );

my %options;

Getopt::Long::config("bundling");

unless (GetOptions(\%options, @getopt_args)) {
    usage();
}

usage() if $options{h};

$options{c} ||= 'testcases/default.conf';

my $basedir = dirname($options{c});

$options{d} ||= [@default_drivers];

$options{n} ||= 1;

# load drivers
for my $driver (@{$options{d}}) {
    warn "Loading $driver Driver\n" if $options{v};
    require "Driver/$driver.pm";
}

# load config
my @config;
open(CONFIG, $options{c}) || die "Can't open config file '$options{c}' : $!";
my $current = {};
while(my $line = <CONFIG>) {
    if ($line =~ /^\s*$/m && %$current) {
        push @config, $current;
        $current = {};
    }
    
    # ignore comments and full line comments
    $line =~ s/#.*$//;
    next unless $line =~ /\S/;
    
    if ($line =~ /^\s*\[(.*)\]\s*$/) {
        $current->{component} = $1;
    }
    elsif ($line =~ /^(.*?)\s*=\s*(.*)$/) {
        $current->{$1} = $2;
    }
}

for my $driver (@{$options{d}}) {
    my $pkg = "Driver::${driver}";
    
    $pkg->can('init')->(verbose => $options{v});
    
    $pkg->can('chdir')->($basedir);
    
    print "Testing: $driver\n\n";

    print_header();
    
    my %totals;

    COMPONENT:
    for my $cmp (@config) {
        warn "Running test: $cmp->{component}\n" if $options{v};
        for (1..$options{n}) {
            $component = $cmp->{component};
            $iter = $ms = $kb_in = $kb_out = $kb_sec = $ref_size = 0;

            if ($cmp->{skipdriver} =~ /\b\Q$driver\E\b/) {
                $result = 'SKIPPED';
                print_output() unless $cmp->{written};
                $cmp->{written}++;
                next COMPONENT;
            }

            eval {
                $pkg->can('load_stylesheet')->($cmp->{stylesheet});
                $pkg->can('load_input')->($cmp->{input});

                $iter = $cmp->{iterations};
                $iter = 1 if $options{t};


                my $bench = timeit($iter, sub {
                        $pkg->can('run_transform')->($cmp->{output});
                    });
                
                my $str = timestr($bench, 'all', '5.4f');
                
                if ($str =~ /\((\d+\.\d+)/) {
                    $ms = $1;
                    $ms *= 1000;
                }
                
                $kb_in = (stat($cmp->{input}))[7];

                if ($options{x}) {
                    $kb_in /= 1000;
                }
                else {
                    $kb_in += (stat($cmp->{stylesheet}))[7];
                    $kb_in /= 1024;
                }
                
                $kb_in *= $iter;

                $kb_out = (stat($cmp->{output}))[7];
                $kb_out /= 1024;
                $kb_out *= $iter;

                die "failed - no output\n" unless $kb_out > 0;

                $kb_sec = ($kb_in + $kb_out) /
                            ( $ms / 500 );

                if ($cmp->{reference}) {
                    $ref_size = (stat($cmp->{reference}))[7];
                    $ref_size /= 1024;

                    open(REFERENCE, $cmp->{reference}) || die "Can't open reference '$cmp->{reference}' : $!";
                    open(NEW, $cmp->{output}) || die "Can't open transform output '$cmp->{output}' : $!";
                    local $/;
                    my $ref = <REFERENCE>;
                    my $new = <NEW>;
                    close REFERENCE;
                    close NEW;
                    $new =~ s/\A<\?xml.*?\?>\s*//;
                    $new =~ s/\A<!DOCTYPE.*?>\s*//;

                    if (!length($new)) {
                        die "output length failed\n";
                    }
                    if ($new eq $ref) {
                        $result = 'OK';
                    }
                    else {
                        $result = 'CHECK OUTPUT';
                        eval {
                            my $rpp = XML::XPath->new(xml => $ref);
                            my $ppp = XML::XPath::XMLParser->new(xml => $new);
                            my $npp;
                            eval {
                                $npp = $ppp->parse;
                            };
                            if ($@) {
                                $npp = $ppp->parse("<norm>$new</norm>");
                            }
                            my @rnodes = $rpp->findnodes('//*');
                            my @nnodes = $npp->findnodes('//*');
#                            warn "ref nodes: ", scalar(@rnodes), "\n";
#                            warn "new nodes: ", scalar(@nnodes), "\n";
                            if (@rnodes == @nnodes) {
                                $result = 'COUNT OK';
                            }
                        };
                        if ($@) {
                            warn $@ if $options{v};
                        }
                    }
                }
                else {
                    $result = 'NO REFERENCE';
                }
            };
            if ($@) {
                warn "$component failed: $@" if $options{v};
                $result = 'ERROR';
            }
            
            if (($result =~ /OK/) || ($result eq 'NO REFERENCE')) {
                $totals{iter} += $iter;
                $totals{ms} += $ms;
                $totals{kb_in} += $kb_in;
                $totals{kb_out} += $kb_out;
            }

            print_output() unless $cmp->{written};
            $cmp->{written}++;
        } # $options{n} loop
        
        delete $cmp->{written};
    } # each component
    
    $pkg->can('shutdown')->();
    
    $component = 'total';
    $iter = $totals{iter};
    $ms = $totals{ms};
    $kb_in = $totals{kb_in};
    $kb_out = $totals{kb_out};
    $kb_sec = ($kb_in + $kb_out) / 
                ( $ms / 500 );
    $ref_size = 0;
    $result = '';
    print_output();
}

sub usage {
    print <<EOT;
usage: $0 [options]

    options:

        -c <file>   load configuration from <file>
                    defaults to testcases/default.conf
                    
        -n <num>    run each test case <num> times. Default = 1.
        
        -t          only one iteration per test case (note this
                    is different to -n 1)
        
        -d <Driver> test <Driver>. Use multiple -d options to test
                    more than one driver. Defaults are set in this
                    script (the \@default_drivers variable).
        
        -x          XSLTMark emulation. Infuriatingly XSLTMark thinks
                    there are 1000 bytes in a Kilobyte. Someone please
                    tell them some basic computer science...
                    
                    Without this option, this benchmark also includes
                    the size of the stylesheet in the KB In figure.
                    
        -v          be verbose.

Copyright 2001 AxKit.com Ltd. This is free software, you may use it and
distribute it under either the GNU GPL Version 2, or under the Perl
Artistic License.

EOT
    exit(0);
}

sub print_header {
    print STDOUT <<'EOF';
Test Component   Iter    ms   KB In  KB Out      KB/s     Result
==========================================================================
EOF
}

sub print_output {
    printf STDOUT "%-15.15s %5.0d %5.0d %7.0f %7.0f %9.2f   %-15.15s\n",
            $component, $iter, $ms, $kb_in, $kb_out, $kb_sec, $result;
}