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