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