1#!/usr/bin/perl
2# Copyright (c) 2000, 2001, 2003, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
3# Use is subject to license terms.
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Library General Public
7# License as published by the Free Software Foundation; version 2
8# of the License.
9#
10# This library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Library General Public License for more details.
14#
15# You should have received a copy of the GNU Library General Public
16# License along with this library; if not, write to the Free
17# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18# MA 02110-1301, USA
19#
20
21use Cwd;
22use DBI;
23use Benchmark;
24
25$opt_loop_count=10;
26
27$pwd = cwd(); $pwd = "." if ($pwd eq '');
28require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
29
30$into_table = "";
31
32if ($opt_small_test)
33{
34  $opt_loop_count/=5;
35}
36
37####
38####  Connect and start timeing
39####
40
41$dbh = $server->connect();
42$start_time=new Benchmark;
43
44####
45#### Create needed tables
46####
47
48init_data();
49init_query();
50
51print "Wisconsin benchmark test\n\n";
52
53if ($opt_skip_create)
54{
55  if ($opt_lock_tables)
56  {
57    @tmp=@table_names; push(@tmp,@extra_names);
58    $sth = $dbh->do("LOCK TABLES " . join(" WRITE,", @tmp) . " WRITE") ||
59      die $DBI::errstr;
60  }
61  goto start_benchmark;
62}
63
64$loop_time= new Benchmark;
65for($ti = 0; $ti <= $#table_names; $ti++)
66{
67  my $table_name = $table_names[$ti];
68  my $array_ref = $tables[$ti];
69
70  # This may fail if we have no table so do not check answer
71  $sth = $dbh->do("drop table $table_name" . $server->{'drop_attr'});
72  print "Creating table $table_name\n" if ($opt_verbose);
73  do_many($dbh,@$array_ref);
74}
75$end_time=new Benchmark;
76print "Time for create_table ($#tables): " .
77  timestr(timediff($end_time, $loop_time),"all") . "\n\n";
78
79if ($opt_fast && defined($server->{vacuum}))
80{
81  $server->vacuum(1,\$dbh);
82}
83
84
85####
86#### Insert data
87####
88
89print "Inserting data\n";
90$loop_time= new Benchmark;
91$row_count=0;
92if ($opt_lock_tables)
93{
94  @tmp=@table_names; push(@tmp,@extra_names);
95  $sth = $dbh->do("LOCK TABLES " . join(" WRITE,", @tmp) . " WRITE") ||
96    die $DBI::errstr;
97}
98
99if ($opt_fast && $server->{'limits'}->{'load_data_infile'})
100{
101  for ($ti = 0; $ti <= $#table_names; $ti++)
102  {
103    my $table_name = $table_names[$ti];
104    if ($table_name =~ /tenk|Bprime/i) {
105      $filename = "$pwd/Data/Wisconsin/tenk.data";
106    } else {
107      $filename = "$pwd/Data/Wisconsin/$table_name.data";
108    }
109    $row_count+=$server->insert_file($table_name,$filename,$dbh);
110  }
111}
112else
113{
114  if ($opt_fast && $server->{transactions})
115  {
116    $dbh->{AutoCommit} = 0;
117  }
118
119  for ($ti = 0; $ti <= $#table_names; $ti++)
120  {
121    my $table_name = $table_names[$ti];
122    my $array_ref = $tables[$ti];
123    my @table = @$array_ref;
124    my $insert_start = "insert into $table_name values (";
125
126    if ($table_name =~ /tenk|Bprime/i) {
127      $filename = "$pwd/Data/Wisconsin/tenk.data";
128    } else {
129      $filename = "$pwd/Data/Wisconsin/$table_name.data";
130    }
131    open(DATA, "$filename") || die "Can't open text file: $filename\n";
132    while(<DATA>)
133    {
134      chomp;
135      $command = $insert_start . $_ . ")";
136      print "$command\n" if ($opt_debug);
137      $sth = $dbh->do($command) or die $DBI::errstr;
138      $row_count++;
139    }
140  }
141  close(DATA);
142}
143
144if ($opt_lock_tables)
145{
146  do_query($dbh,"UNLOCK TABLES");
147}
148if ($opt_fast && $server->{transactions})
149{
150  $dbh->commit;
151  $dbh->{AutoCommit} = 1;
152}
153
154$end_time=new Benchmark;
155print "Time to insert ($row_count): " .
156  timestr(timediff($end_time, $loop_time),"all") . "\n";
157
158## Oracle runs out of rollback segments here if using the default "small"
159## configuration so disconnect and reconnect to use a new segment
160if ($server->small_rollback_segment())
161{
162  $dbh->disconnect;				# close connection
163  $dbh=$server->connect();
164}
165
166if ($opt_fast && defined($server->{vacuum}))
167{
168  $server->vacuum(0,\$dbh,@table_names);
169}
170
171if ($opt_lock_tables)
172{
173  @tmp=@table_names; push(@tmp,@extra_names);
174  $sth = $dbh->do("LOCK TABLES " . join(" WRITE,", @tmp) . " WRITE") ||
175  die $DBI::errstr;
176}
177
178$loop_time= $end_time;
179print "Delete from Bprime where unique2 >= 1000\n" if ($opt_debug);
180$sth = $dbh->do("delete from Bprime where Bprime.unique2 >= 1000") or
181  die $DBI::errstr;
182$end_time=new Benchmark;
183print "Time to delete_big (1): " .
184  timestr(timediff($end_time, $loop_time),"all") . "\n\n";
185
186if ($opt_fast && defined($server->{vacuum}))
187{
188  $server->vacuum(0,\$dbh);
189}
190
191####
192#### Running the benchmark
193####
194
195start_benchmark:
196
197print "Running the actual benchmark\n";
198
199$loop_time= new Benchmark;
200$count=0;
201for ($i = 0; $i <= $#query; $i+=2)
202{
203  if ($query[$i+1])				# If the server can handle it
204  {
205    $loop_count = 1;
206    $loop_count = $opt_loop_count if ($query[$i] =~ /^select/i);
207    $query[$i] =~ s/\sAS\s+[^\s,]+//ig if (!$limits->{'column_alias'});
208    timeit($loop_count, "fetch_all_rows(\$dbh,\"$query[$i]\")");
209    $count+=$loop_count;
210  }
211}
212
213$end_time=new Benchmark;
214print "Time for wisc_benchmark ($count): " .
215  timestr(timediff($end_time, $loop_time),"all") . "\n\n";
216
217if (!$opt_skip_delete)
218{
219  for ($ti = 0; $ti <= $#table_names; $ti++)
220  {
221    my $table_name = $table_names[$ti];
222    $sth = $dbh->do("drop table $table_name" . $server->{'drop_attr'});
223  }
224}
225
226if ($opt_fast && defined($server->{vacuum}))
227{
228  $server->vacuum(0,\$dbh);
229}
230
231####
232#### The END
233####
234
235$dbh->disconnect;                               # close connection
236end_benchmark($start_time);
237
238
239################################
240###### subroutine for database structure
241################################
242
243sub init_data
244{
245  @onek=
246    $server->create("onek",
247		    ["unique1 int(5) NOT NULL",
248		     "unique2 int(4) NOT NULL",
249		     "two int(4)",
250		     "four int(4)",
251		     "ten int(4)",
252		     "twenty int(4)",
253		     "hundred int(4) NOT NULL",
254		     "thousand int(4)",
255		     "twothousand int(4)",
256		     "fivethous int(4)",
257		     "tenthous int(4)",
258		     "odd int(4)",
259		     "even int(4)",
260		     "stringu1 char(16)",
261		     "stringu2 char(16)",
262		     "string4 char(16)"],
263		    ["UNIQUE (unique1)",
264		     "UNIQUE (unique2)",
265		     "INDEX hundred1 (hundred)"]);
266
267  @tenk1=
268    $server->create("tenk1",
269		    ["unique1 int(4) NOT NULL",
270		     "unique2 int(4) NOT NULL",
271		     "two int(4)",
272		     "four int(4)",
273		     "ten int(4)",
274		     "twenty int(4)",
275		     "hundred int(4) NOT NULL",
276		     "thousand int(4)",
277		     "twothousand int(4)",
278		     "fivethous int(4)",
279		     "tenthous int(4)",
280		     "odd int(4)",
281		     "even int(4)",
282		     "stringu1 char(16)",
283		     "stringu2 char(16)",
284		     "string4 char(16)"],
285		    ["UNIQUE (unique1)",
286		     "UNIQUE (unique2)",
287		     "INDEX hundred2 (hundred)"]);
288
289  @tenk2=
290    $server->create("tenk2",
291		    ["unique1 int(4) NOT NULL",
292		     "unique2 int(4) NOT NULL",
293		     "two int(4)",
294		     "four int(4)",
295		     "ten int(4)",
296		     "twenty int(4)",
297		     "hundred int(4) NOT NULL",
298		     "thousand int(4)",
299		     "twothousand int(4)",
300		     "fivethous int(4)",
301		     "tenthous int(4)",
302		     "odd int(4)",
303		     "even int(4)",
304		     "stringu1 char(16)",
305		     "stringu2 char(16)",
306		     "string4 char(16)"],
307		    ["UNIQUE (unique1)",
308		     "UNIQUE (unique2)",
309		     "INDEX hundred3 (hundred)"]);
310
311  @Bprime=
312    $server->create("Bprime",
313		    ["unique1 int(4) NOT NULL",
314		     "unique2 int(4) NOT NULL",
315		     "two int(4)",
316		     "four int(4)",
317		     "ten int(4)",
318		     "twenty int(4)",
319		     "hundred int(4) NOT NULL",
320		     "thousand int(4)",
321		     "twothousand int(4)",
322		     "fivethous int(4)",
323		     "tenthous int(4)",
324		     "odd int(4)",
325		     "even int(4)",
326		     "stringu1 char(16)",
327		     "stringu2 char(16)",
328		     "string4 char(16)"],
329		    ["UNIQUE (unique1)",
330		     "UNIQUE (unique2)",
331		     "INDEX hundred4 (hundred)"]);
332
333  @tables =
334    (\@onek, \@tenk1, \@tenk2, \@Bprime);
335
336  @table_names =
337    ("onek", "tenk1", "tenk2", "Bprime");
338
339# Alias used in joins
340  @extra_names=
341    ("tenk1 as t", "tenk1 as t1","tenk1 as t2", "Bprime as B","onek as o");
342}
343
344
345sub init_query
346{
347  @query=
348    ("select * $into_table from tenk1 where (unique2 > 301) and (unique2 < 402)",1,
349     "select * $into_table from tenk1 where (unique1 > 647) and (unique1 < 1648)",1,
350     "select * from tenk1 where unique2 = 2001",1,
351     "select * from tenk1 where (unique2 > 301) and (unique2 < 402)",1,
352     "select t1.*, t2.unique1 AS t2unique1, t2.unique2 AS t2unique2, t2.two AS t2two, t2.four AS t2four, t2.ten AS t2ten, t2.twenty AS t2twenty, t2.hundred AS t2hundred, t2.thousand AS t2thousand, t2.twothousand AS t2twothousand, t2.fivethous AS t2fivethous, t2.tenthous AS t2tenthous, t2.odd AS t2odd, t2.even AS t2even, t2.stringu1 AS t2stringu1, t2.stringu2 AS t2stringu2, t2.string4 AS t2string4 $into_table from tenk1 t1, tenk1 t2 where (t1.unique2 = t2.unique2) and (t2.unique2 < 1000)",$limits->{'table_wildcard'},
353     "select t.*,B.unique1 AS Bunique1,B.unique2 AS Bunique2,B.two AS Btwo,B.four AS Bfour,B.ten AS Bten,B.twenty AS Btwenty,B.hundred AS Bhundred,B.thousand AS Bthousand,B.twothousand AS Btwothousand,B.fivethous AS Bfivethous,B.tenthous AS Btenthous,B.odd AS Bodd,B.even AS Beven,B.stringu1 AS Bstringu1,B.stringu2 AS Bstringu2,B.string4 AS Bstring4 $into_table from tenk1 t, Bprime B where t.unique2 = B.unique2",$limits->{'table_wildcard'},
354     "select t1.*,o.unique1 AS ounique1,o.unique2 AS ounique2,o.two AS otwo,o.four AS ofour,o.ten AS oten,o.twenty AS otwenty,o.hundred AS ohundred,o.thousand AS othousand,o.twothousand AS otwothousand,o.fivethous AS ofivethous,o.tenthous AS otenthous,o.odd AS oodd, o.even AS oeven,o.stringu1 AS ostringu1,o.stringu2 AS ostringu2,o.string4 AS ostring4 $into_table from onek o, tenk1 t1, tenk1 t2 where (o.unique2 = t1.unique2) and (t1.unique2 = t2.unique2) and (t1.unique2 < 1000) and (t2.unique2 < 1000)",$limits->{'table_wildcard'},
355     "select two, four, ten, twenty, hundred, string4 $into_table from tenk1",1,
356     "select * $into_table from onek",1,
357     "select MIN(unique2) as x $into_table from tenk1",$limits->{'group_functions'},
358     "insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even,stringu1,stringu2, string4) values (10001, 74001, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, 'ron may choi','jae kwang choi', 'u. c. berkeley')",1,
359     "insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even,stringu1,stringu2, string4) values (19991, 60001, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, 'ron may choi','jae kwang choi', 'u. c. berkeley')",1,
360     "delete from tenk1 where tenk1.unique2 = 877",1,
361     "delete from tenk1 where tenk1.unique2 = 876",1,
362     "update tenk1 set unique2 = 10001 where tenk1.unique2 =1491",1,
363     "update tenk1 set unique2 = 10023 where tenk1.unique2 =1480",1,
364     "insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4) values (20002, 70002, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, 'ron may choi', 'jae kwang choi', 'u. c. berkeley')",1,
365     "insert into tenk1 (unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4) values (50002, 40002, 0, 2, 0, 10, 50, 688, 1950, 4950, 9950, 1, 100, 'ron may choi', 'jae kwang choi', 'u. c. berkeley')",1,
366     "delete from tenk1 where tenk1.unique2 = 10001",1,
367     "delete from tenk1 where tenk1.unique2 = 900",1,
368     "update tenk1 set unique2 = 10088 where tenk1.unique2 =187",1,
369     "update tenk1 set unique2 = 10003 where tenk1.unique2 =2000",1,
370     "update tenk1 set unique2 = 10020 where tenk1.unique2 =1974",1,
371     "update tenk1 set unique2 = 16001 where tenk1.unique2 =1140",1,
372     );
373}
374