1#!/usr/bin/env perl
2
3# Copyright (C) 2000 MySQL AB
4# Use is subject to license terms
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; version 2 of the License.
9#
10# This program 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
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
18
19#
20# This is a test with uses processes to insert, select and drop tables.
21#
22
23$opt_loop_count=100000; # Change this to make test harder/easier
24
25##################### Standard benchmark inits ##############################
26
27use DBI;
28use Getopt::Long;
29use Benchmark;
30
31package main;
32
33$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
34  $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
35$opt_host=""; $opt_db="test";
36
37GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
38"verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
39$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
40
41print "Testing 5 multiple connections to a server with 1 insert, 2 drop/rename\n";
42print "1 select and 1 flush thread\n";
43
44$firsttable  = "bench_f1";
45
46####
47####  Start timeing and start test
48####
49
50$start_time=new Benchmark;
51if (!$opt_skip_create)
52{
53  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
54		      $opt_user, $opt_password,
55		    { PrintError => 0}) || die $DBI::errstr;
56  $dbh->do("drop table if exists $firsttable, ${firsttable}_1, ${firsttable}_2");
57
58  print "Creating table $firsttable in database $opt_db\n";
59  $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
60  $dbh->disconnect; $dbh=0;	# Close handler
61}
62$|= 1;				# Autoflush
63
64####
65#### Start the tests
66####
67
68test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
69test_drop(1) if (($pid=fork()) == 0); $work{$pid}="drop 1";
70test_drop(2) if (($pid=fork()) == 0); $work{$pid}="drop 2";
71test_select() if (($pid=fork()) == 0); $work{$pid}="select";
72test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
73
74$errors=0;
75while (($pid=wait()) != -1)
76{
77  $ret=$?/256;
78  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
79  $errors++ if ($ret != 0);
80}
81
82if (!$opt_skip_delete && !$errors)
83{
84  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
85		      $opt_user, $opt_password,
86		    { PrintError => 0}) || die $DBI::errstr;
87  $dbh->do("drop table $firsttable");
88  $dbh->disconnect; $dbh=0;	# Close handler
89}
90print ($errors ? "Test failed\n" :"Test ok\n");
91
92$end_time=new Benchmark;
93print "Total time: " .
94  timestr(timediff($end_time, $start_time),"noc") . "\n";
95
96exit(0);
97
98#
99# Insert records in the table
100#
101
102sub test_insert
103{
104  my ($dbh,$i);
105
106  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
107		      $opt_user, $opt_password,
108		    { PrintError => 0}) || die $DBI::errstr;
109  for ($i=0 ; $i < $opt_loop_count; $i++)
110  {
111    if (!$dbh->do("insert into $firsttable values ($i,'This is entry $i','')"))
112    {
113      print "Warning; Got error on insert: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
114    }
115  }
116  $dbh->disconnect; $dbh=0;
117  print "Test_insert: Inserted $i rows\n";
118  exit(0);
119}
120
121
122sub test_drop
123{
124  my ($id) = @_;
125  my ($dbh,$i,$sth,$error_counter,$sleep_time);
126
127  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
128		      $opt_user, $opt_password,
129		    { PrintError => 0}) || die $DBI::errstr;
130  $error_counter=0;
131  $sleep_time=2;
132  for ($i=0 ; $i < $opt_loop_count ; $i++)
133  {
134    sleep($sleep_time);
135    # Check if insert thread is ready
136    $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on select from $firsttable: $dbh->errstr\n";
137    if (!$sth->execute || !(@row = $sth->fetchrow_array()) ||
138	!$row[0])
139    {
140      $sth->finish;
141      $sleep_time=1;
142      last if ($error_counter++ == 5);
143      next;
144    }
145    $sleep_time=2;
146    $sth->finish;
147
148    # Change to use a new table
149    $dbh->do("create table ${firsttable}_$id (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
150    $dbh->do("drop table if exists $firsttable") || die "Got error on drop table: $dbh->errstr\n";
151    if (!$dbh->do("alter table ${firsttable}_$id rename to $firsttable"))
152    {
153      print "Warning; Got error from alter table: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /already exist/));
154      $dbh->do("drop table if exists ${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
155    }
156  }
157  $dbh->do("drop table if exists $firsttable,${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
158  $dbh->disconnect; $dbh=0;
159  print "Test_drop: Did a drop $i times\n";
160  exit(0);
161}
162
163
164#
165# select records
166#
167
168sub test_select
169{
170  my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
171
172  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
173		      $opt_user, $opt_password,
174		    { PrintError => 0}) || die $DBI::errstr;
175
176  $error_counter=0;
177  $sleep_time=3;
178  for ($i=0 ; $i < $opt_loop_count ; $i++)
179  {
180    sleep($sleep_time);
181    $sth=$dbh->prepare("select sum(t.id) from $firsttable as t,$firsttable as t2") || die "Got error on select: $dbh->errstr;\n";
182    if ($sth->execute)
183    {
184      @row = $sth->fetchrow_array();
185      $sth->finish;
186      $sleep_time=3;
187    }
188    else
189    {
190      print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
191      $sth->finish;
192      last if ($error_counter++ == 5);
193      $sleep_time=1;
194    }
195  }
196  $dbh->disconnect; $dbh=0;
197  print "Test_select: ok\n";
198  exit(0);
199}
200
201#
202# flush records
203#
204
205sub test_flush
206{
207  my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
208
209  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
210		      $opt_user, $opt_password,
211		    { PrintError => 0}) || die $DBI::errstr;
212
213  $error_counter=0;
214  $sleep_time=5;
215  for ($i=0 ; $i < $opt_loop_count ; $i++)
216  {
217    sleep($sleep_time);
218    $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepar: $dbh->errstr;\n";
219    if ($sth->execute)
220    {
221      @row = $sth->fetchrow_array();
222      $sth->finish;
223      $sleep_time=5;
224      $dbh->do("flush tables $firsttable") || die "Got error on flush table: " . $dbh->errstr . "\n";
225    }
226    else
227    {
228      print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
229      $sth->finish;
230      last if ($error_counter++ == 5);
231      $sleep_time=1;
232    }
233  }
234  $dbh->disconnect; $dbh=0;
235  print "Test_select: ok\n";
236  exit(0);
237}
238