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