]> git.evergreen-ils.org Git - OpenSRF.git/blob - examples/math_bench.pl
better sorting and nesting
[OpenSRF.git] / examples / math_bench.pl
1 #!/usr/bin/perl -w
2 use strict;use warnings;
3 use OpenSRF::System qw(/pines/conf/client.conf);
4 use OpenSRF::Utils::SettingsClient;
5 use OpenSRF::DomainObject::oilsMethod;
6 use OpenSRF::DomainObject::oilsPrimitive;
7 use Time::HiRes qw/time/;
8 use OpenSRF::EX qw/:try/;
9
10 $| = 1;
11
12 # ----------------------------------------------------------------------------------------
13 # This is a quick and dirty script to perform benchmarking against the math server.
14 # Note: 1 request performs a batch of 4 queries, one for each supported method: add, sub,
15 # mult, div.
16 # Usage: $ perl math_bench.pl <num_requests>
17 # ----------------------------------------------------------------------------------------
18
19
20 my $count = $ARGV[0];
21
22 unless( $count ) {
23         print "usage: ./math_bench.pl <num_requests>\n";
24         exit;
25 }
26
27 warn "PID: $$\n";
28
29 OpenSRF::System->bootstrap_client();
30 my $session = OpenSRF::AppSession->create( "opensrf.math" );
31
32 my @times;
33 my %vals = ( add => 3, sub => -1, mult => 2, div => 0.5 );
34
35 for my $x (1..100) {
36         if( $x % 10 ) { print ".";}
37         else{ print $x/10; };
38 }
39 print "\n";
40
41 my $c = 0;
42
43 for my $scale ( 1..$count ) {
44         for my $mname ( keys %vals ) {
45
46                 my $req;
47                 my $resp;
48                 my $starttime;
49                 try {
50
51                         $starttime = time();
52                         if( ! ($session->connect()) ) { die "Connect timed out\n"; }
53                         $req = $session->request( $mname, 1, 2 );
54                         $resp = $req->recv( timeout => 10 );
55                         push @times, time() - $starttime;
56
57                 } catch OpenSRF::EX with {
58                         my $e = shift;
59                         die "ERROR\n $e";
60
61                 } catch Error with {
62                         my $e = shift;
63                         die "Caught unknown error: $e";
64                 };
65
66
67                 if( ! $req->complete ) { warn "\nIncomplete\n"; }
68
69                 if( UNIVERSAL::isa( $resp, "OpenSRF::EX" ) ) {
70                         print "-" x 50 . "\nReceived Error " . $resp . "\n" . "-" x 50 . "\n";
71
72                 } elsif( $resp ) {
73
74                         my $ret = $resp->content();
75                         if( "$ret" eq $vals{$mname} ) { print "+"; }
76
77                         else { print "*BAD*\n" . $resp->toString(1) . "\n"; }
78
79                 } else { print "*NADA*";        }
80
81                 $req->finish();
82 #               $session->disconnect();
83                 $c++;
84
85         }
86         print "\n[$c] \n" unless $scale % 25;
87 }
88
89 $session->kill_me();
90
91 my $total = 0;
92
93 $total += $_ for (@times);
94
95 $total /= scalar(@times);
96
97 print "\n\n\tAverage Round Trip Time: $total Seconds\n";
98