Cleaned up regex's looking for defunct CVS files
[koha.git] / misc / benchmark.pl
1 #!/usr/bin/perl
2
3 use HTTPD::Bench::ApacheBench;
4 use C4::Context;
5 use strict;
6
7 # 1st, find some maximal values
8 my $dbh=C4::Context->dbh();
9 my $sth = $dbh->prepare("select max(borrowernumber) from borrowers");
10 $sth->execute;
11 my ($borrowernumber_max) = $sth->fetchrow;
12
13 $sth = $dbh->prepare("select max(biblionumber) from biblio");
14 $sth->execute;
15 my ($biblionumber_max) = $sth->fetchrow;
16
17 $sth = $dbh->prepare("select max(itemnumber) from items");
18 $sth->execute;
19 my ($itemnumber_max) = $sth->fetchrow;
20
21 my $baseurl= "http://i17.bureau.paulpoulain.com/cgi-bin/koha";
22 my $max_tries = 200;
23 my $concurrency = 5;
24
25 $|=1;
26 #
27 # the global benchmark we do at the end...
28 #
29 my $b = HTTPD::Bench::ApacheBench->new;
30 $b->concurrency( $concurrency );
31 #
32 # mainpage : (very) low mySQL dependancy
33 #
34 my $b0 = HTTPD::Bench::ApacheBench->new;
35 $b0->concurrency( $concurrency );
36
37 my @mainpage;
38 print "--------------\n";
39 print "Koha benchmark\n";
40 print "--------------\n";
41 print "benchmarking with $max_tries occurences of each operation\n";
42 print "mainpage (no mySQL) ";
43 for (my $i=1;$i<=$max_tries;$i++) {
44     push @mainpage,"$baseurl/mainpage.pl";
45 }
46 my $run0 = HTTPD::Bench::ApacheBench::Run->new
47     ({ urls => \@mainpage,
48     });
49 $b0->add_run($run0);
50 $b->add_run($run0);
51
52 # send HTTP request sequences to server and time responses
53 my $ro = $b0->execute;
54 # calculate hits/sec
55 print ("\t".$b0->total_time."ms\t".(1000*$b0->total_requests/$b0->total_time)." pages/sec\n");
56 print "ALERT : ".$b0->total_responses_failed." failures\n" if $b0->total_responses_failed;
57
58 #
59 # biblios
60 #
61 my $b1 = HTTPD::Bench::ApacheBench->new;
62 $b1->concurrency( $concurrency );
63
64 my @biblios;
65 print "biblio (MARC detail)";
66 for (my $i=1;$i<=$max_tries;$i++) {
67     my $rand_biblionumber = int(rand($biblionumber_max)+1);
68     push @biblios,"$baseurl/catalogue/MARCdetail.pl?biblionumber=$rand_biblionumber";
69 }
70 my $run1 = HTTPD::Bench::ApacheBench::Run->new
71     ({ urls => \@biblios,
72     });
73 $b1->add_run($run1);
74 $b->add_run($run1);
75
76 # send HTTP request sequences to server and time responses
77 my $ro = $b1->execute;
78 # calculate hits/sec
79 print ("\t".$b1->total_time."ms\t".(1000*$b1->total_requests/$b1->total_time)." biblios/sec\n");
80 print "ALERT : ".$b1->total_responses_failed." failures\n" if $b1->total_responses_failed;
81
82 #
83 # borrowers
84 #
85 my $b2 = HTTPD::Bench::ApacheBench->new;
86 $b2->concurrency( $concurrency );
87
88 my @borrowers;
89 print "borrower detail        ";
90 for (my $i=1;$i<=$max_tries;$i++) {
91     my $rand_borrowernumber = int(rand($borrowernumber_max)+1);
92 #     print "$baseurl/members/moremember.pl?borrowernumber=$rand_borrowernumber\n";
93     push @borrowers,"$baseurl/members/moremember.pl?borrowernumber=$rand_borrowernumber";
94 }
95 my $run2 = HTTPD::Bench::ApacheBench::Run->new
96     ({ urls => \@borrowers,
97     });
98 $b2->add_run($run2);
99 $b->add_run($run2);
100
101 # send HTTP request sequences to server and time responses
102 my $ro = $b2->execute;
103 # calculate hits/sec
104 print ("\t".$b2->total_time."ms\t".(1000*$b2->total_requests/$b2->total_time)." borrowers/sec\n");
105
106
107 #
108 # issue (& then return) books
109 #
110 my $b3 = HTTPD::Bench::ApacheBench->new;
111 $b3->concurrency( $concurrency );
112 my $b4 = HTTPD::Bench::ApacheBench->new;
113 $b4->concurrency( $concurrency );
114
115 my @issues;
116 my @returns;
117 print "Issues detail          ";
118 my $sth = $dbh->prepare("SELECT barcode FROM items WHERE itemnumber=?");
119 my $sth2 = $dbh->prepare("SELECT borrowernumber FROM borrowers WHERE borrowernumber=?");
120 for (my $i=1;$i<=$max_tries;$i++) {
121     my $rand_borrowernumber;
122     # check that the borrowernumber exist
123     until ($rand_borrowernumber) {
124         $rand_borrowernumber = int(rand($borrowernumber_max)+1);
125         $sth2->execute($rand_borrowernumber);
126         ($rand_borrowernumber) = $sth2->fetchrow;
127     }
128     # find a barcode & check it exists
129     my $rand_barcode;
130     until ($rand_barcode) {
131         my $rand_itemnumber = int(rand($itemnumber_max)+1);
132         $sth->execute($rand_itemnumber);
133         ($rand_barcode) = $sth->fetchrow();
134 #         print "$baseurl/circ/circulation.pl?borrowernumber=$rand_borrowernumber&barcode=$rand_barcode&issueconfirmed=1&year=2010&month=01&day=01\n";
135     }
136     push @issues,"$baseurl/circ/circulation.pl?borrowernumber=$rand_borrowernumber&barcode=$rand_barcode&issueconfirmed=1";
137     push @returns,"$baseurl/circ/returns.pl?barcode=$rand_barcode";
138 }
139 my $run3 = HTTPD::Bench::ApacheBench::Run->new
140     ({ urls => \@issues,
141     });
142 $b3->add_run($run3);
143 $b->add_run($run3);
144
145 # send HTTP request sequences to server and time responses
146 my $ro = $b3->execute;
147 # calculate hits/sec
148 print ("\t".$b3->total_time."ms\t".(1000*$b3->total_requests/$b3->total_time)." issues/sec\n");
149
150 print "Returns detail         ";
151 my $run4 = HTTPD::Bench::ApacheBench::Run->new
152     ({ urls => \@returns,
153     });
154 $b4->add_run($run4);
155 $b->add_run($run4);
156
157 # send HTTP request sequences to server and time responses
158 my $ro = $b4->execute;
159 # calculate hits/sec
160 print ("\t".$b4->total_time."ms\t".(1000*$b4->total_requests/$b4->total_time)." returns/sec\n");
161
162 print "Benchmarking everything";
163 my $ro = $b->execute;
164 print ("\t".$b->total_time."ms\t".(1000*$b->total_requests/$b->total_time)." operations/sec\n");