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