1 |
#!/usr/bin/perl -w |
2 |
|
3 |
require 5.004; |
4 |
use strict; |
5 |
|
6 |
use blib; |
7 |
|
8 |
require DBI; |
9 |
require Benchmark; |
10 |
|
11 |
|
12 |
my($i); |
13 |
sub TimeMe ($$$$) { |
14 |
my($startMsg, $endMsg, $code, $count) = @_; |
15 |
printf("\n%s\n", $startMsg); |
16 |
my($t1) = Benchmark->new(); |
17 |
$@ = ''; |
18 |
eval { |
19 |
for ($i = 0; $i < $count; $i++) { |
20 |
&$code; |
21 |
} |
22 |
}; |
23 |
if ($@) { |
24 |
print "Test failed, message: $@\n"; |
25 |
} else { |
26 |
my($td) = Benchmark::timediff(Benchmark->new(), $t1); |
27 |
my($dur) = $td->cpu_a; |
28 |
printf($endMsg, $count, $dur, $count / $dur); |
29 |
print "\n"; |
30 |
} |
31 |
} |
32 |
|
33 |
|
34 |
TimeMe("Testing empty loop speed ...", |
35 |
"%d iterations in %.1f cpu+sys seconds (%d per sec)", |
36 |
sub { |
37 |
}, |
38 |
100000); |
39 |
|
40 |
|
41 |
my($dbh); |
42 |
TimeMe("Testing connect/disconnect speed ...", |
43 |
"%d connections in %.1f cpu+sys seconds (%d per sec)", |
44 |
sub { |
45 |
$dbh = DBI->connect("DBI:RAM:", undef, undef, |
46 |
{ 'RaiseError' => 1 }); |
47 |
$dbh->disconnect(); |
48 |
}, |
49 |
2000); |
50 |
|
51 |
$dbh = DBI->connect("DBI:RAM:", undef, undef, |
52 |
{ 'RaiseError' => 1 }); |
53 |
TimeMe("Testing CREATE/DROP TABLE speed ...", |
54 |
"%d files in %.1f cpu+sys seconds (%d per sec)", |
55 |
sub { |
56 |
$dbh->do("CREATE TABLE bench (id INTEGER, name CHAR(40)," |
57 |
. " firstname CHAR(40), address CHAR(40)," |
58 |
. " zip CHAR(10), city CHAR(40), email CHAR(40))"); |
59 |
$dbh->do("DROP TABLE bench"); |
60 |
}, |
61 |
500); |
62 |
|
63 |
$dbh->do("CREATE TABLE bench (id INTEGER, name CHAR(40)," |
64 |
. " firstname CHAR(40), address CHAR(40)," |
65 |
. " zip CHAR(10), city CHAR(40), email CHAR(40))"); |
66 |
my(@vals) = (0 .. 499); |
67 |
my($num); |
68 |
TimeMe("Testing INSERT speed ...", |
69 |
"%d rows in %.1f cpu+sys seconds (%d per sec)", |
70 |
sub { |
71 |
($num) = splice(@vals, int(rand(@vals)), 1); |
72 |
$dbh->do("INSERT INTO bench VALUES (?, 'Wiedmann', 'Jochen'," |
73 |
. " 'Am Eisteich 9', '72555', 'Metzingen'," |
74 |
. " 'joe\@ispsoft.de')", undef, $num); |
75 |
}, |
76 |
500); |
77 |
|
78 |
my($sth); |
79 |
TimeMe("Testing SELECT speed ...", |
80 |
"%d single rows in %.1f cpu+sys seconds (%.1f per sec)", |
81 |
sub { |
82 |
$num = int(rand(500)); |
83 |
$sth = $dbh->prepare("SELECT * FROM bench WHERE id = $num"); |
84 |
$sth->execute(); |
85 |
$sth->fetch() or die "Expected result for id = $num"; |
86 |
}, |
87 |
100); |
88 |
|
89 |
|
90 |
TimeMe("Testing SELECT speed (multiple rows) ...", |
91 |
"%d times 100 rows in %.1f cpu+sys seconds (%.1f per sec)", |
92 |
sub { |
93 |
$num = int(rand(400)); |
94 |
$sth = $dbh->prepare("SELECT * FROM bench WHERE id >= $num" |
95 |
. " AND id < " . ($num+100)); |
96 |
$sth->execute(); |
97 |
($sth->rows() == 100) |
98 |
or die "Expected 100 rows for id = $num, got " . $sth->rows(); |
99 |
while ($sth->fetch()) { |
100 |
} |
101 |
}, |
102 |
100); |
103 |
|