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