]> git.evergreen-ils.org Git - OpenSRF.git/blob - examples/math_bench.pl
using OO syntax
[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( "math" );
31
32 try {
33         if( ! ($session->connect()) ) { die "Connect timed out\n"; }
34
35 } catch OpenSRF::EX with {
36         my $e = shift;
37         warn "Connection Failed *\n";
38         die $e;
39 }
40
41 my @times;
42 my %vals = ( add => 3, sub => -1, mult => 2, div => 0.5 );
43
44 for my $x (1..100) {
45         if( $x % 10 ) { print ".";}
46         else{ print $x/10; };
47 }
48 print "\n";
49
50 my $c = 0;
51
52 for my $scale ( 1..$count ) {
53         for my $mname ( keys %vals ) {
54
55                 my $req;
56                 my $resp;
57                 my $starttime;
58                 try {
59
60                         $starttime = time();
61                         $req = $session->request( $mname, 1, 2 );
62                         $resp = $req->recv( timeout => 10 );
63                         push @times, time() - $starttime;
64
65                 } catch OpenSRF::EX with {
66                         my $e = shift;
67                         die "ERROR\n $e";
68
69                 } catch Error with {
70                         my $e = shift;
71                         die "Caught unknown error: $e";
72                 };
73
74
75                 if( ! $req->complete ) { warn "\nIncomplete\n"; }
76
77                 if( UNIVERSAL::isa( $resp, "OpenSRF::EX" ) ) {
78                         print "-" x 50 . "\nReceived Error " . $resp . "\n" . "-" x 50 . "\n";
79
80                 } elsif( $resp ) {
81
82                         my $ret = $resp->content();
83                         if( "$ret" eq $vals{$mname} ) { print "+"; }
84
85                         else { print "*BAD*\n" . $resp->toString(1) . "\n"; }
86
87                 } else { print "*NADA*";        }
88
89                 $req->finish();
90                 $session->disconnect();
91                 $c++;
92
93         }
94         print "\n[$c] \n" unless $scale % 25;
95 }
96
97 $session->kill_me();
98
99 my $total = 0;
100
101 $total += $_ for (@times);
102
103 $total /= scalar(@times);
104
105 print "\n\n\tAverage Round Trip Time: $total Seconds\n";
106