summaryrefslogtreecommitdiff
path: root/tests/rename_test.pl
blob: ff1b73434e54e1315dcbea13b41bb39de5598e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl -w

# Copyright (C) 2000, 2001 MySQL AB
# Use is subject to license terms
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA

#
# This is a test with uses processes to insert, select and drop tables.
#

$opt_loop_count=100000; # Change this to make test harder/easier

##################### Standard benchmark inits ##############################

use DBI;
use Getopt::Long;
use Benchmark;

package main;

$opt_skip_create=$opt_skip_delete=$opt_skip_flush=0;
$opt_host=""; $opt_db="test";

GetOptions("host=s","db=s","loop-count=i","skip-create","skip-delete",
"skip-flush") || die "Aborted";

print "Testing 5 multiple connections to a server with 1 insert, 1 rename\n";
print "1 select and 1 flush thread\n";

$firsttable  = "bench_f1";

####
####  Start timing and start test
####

$start_time=new Benchmark;
if (!$opt_skip_create)
{
  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  $dbh->do("drop table if exists $firsttable, ${firsttable}_1, ${firsttable}_2");

  print "Creating table $firsttable in database $opt_db\n";
  $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  $dbh->disconnect; $dbh=0;	# Close handler
}
$|= 1;				# Autoflush

####
#### Start the tests
####

test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
test_rename(1) if (($pid=fork()) == 0); $work{$pid}="rename 1";
test_rename(2) if (($pid=fork()) == 0); $work{$pid}="rename 2";
test_select() if (($pid=fork()) == 0); $work{$pid}="select";
if (!$opt_skip_flush)
{
  test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
}
$errors=0;
while (($pid=wait()) != -1)
{
  $ret=$?/256;
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  $errors++ if ($ret != 0);
}

if (!$opt_skip_delete && !$errors)
{
  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  $dbh->do("drop table $firsttable");
  $dbh->disconnect; $dbh=0;	# Close handler
}
print ($errors ? "Test failed\n" :"Test ok\n");

$end_time=new Benchmark;
print "Total time: " .
  timestr(timediff($end_time, $start_time),"noc") . "\n";

exit(0);

#
# Insert records in the table.  Delete table when test is finished
#

sub test_insert
{
  my ($dbh,$i,$error);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  for ($i=0 ; $i < $opt_loop_count; $i++)
  {
    if (!$dbh->do("insert into $firsttable values ($i,'This is entry $i','')"))
    {
      $error=$dbh->errstr;
      $dbh->do("drop table ${firsttable}"); # End other threads
      die "Warning; Got error on insert: " . $error . "\n";
    }
  }
  sleep(1);
  $dbh->do("drop table ${firsttable}") || die "Got error on drop table: " . $dbh->errstr . "\n";
  $dbh->disconnect; $dbh=0;
  print "Test_insert: Inserted $i rows\n";
  exit(0);
}


sub test_rename
{
  my ($id) = @_;
  my ($dbh,$i,$error_counter,$sleep_time);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  $error_counter=0;
  $sleep_time=2;
  for ($i=0 ; $i < $opt_loop_count ; $i++)
  {
    sleep($sleep_time);
    $dbh->do("create table ${firsttable}_$id (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
    if (!$dbh->do("rename table $firsttable to ${firsttable}_${id}_1, ${firsttable}_$id to ${firsttable}"))
    {
      last if ($dbh->errstr =~ /^Can\'t find/);
      die "Got error on rename: " . $dbh->errstr . "\n";
    }
    $dbh->do("drop table ${firsttable}_${id}_1") || die "Got error on drop table: " . $dbh->errstr . "\n";
  }
  $dbh->disconnect; $dbh=0;
  print "Test_drop: Did a drop $i times\n";
  exit(0);
}


#
# select records
#

sub test_select
{
  my ($dbh,$i,$sth,@row,$sleep_time);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $sleep_time=3;
  for ($i=0 ; $i < $opt_loop_count ; $i++)
  {
    sleep($sleep_time);
    $sth=$dbh->prepare("select sum(t.id) from $firsttable as t,$firsttable as t2") || die "Got error on select: $dbh->errstr;\n";
    if ($sth->execute)
    {
      @row = $sth->fetchrow_array();
      $sth->finish;
    }
    else
    {
      $sth->finish;
      last if (! ($dbh->errstr =~ /doesn\'t exist/));
      die "Got error on select: " . $dbh->errstr . "\n";
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_select: ok\n";
  exit(0);
}

#
# flush records
#

sub test_flush
{
  my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $error_counter=0;
  $sleep_time=5;
  for ($i=0 ; $i < $opt_loop_count ; $i++)
  {
    sleep($sleep_time);
    $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepar: $dbh->errstr;\n";
    if ($sth->execute)
    {
      @row = $sth->fetchrow_array();
      $sth->finish;
      $sleep_time=5;
      $dbh->do("flush tables $firsttable") || die "Got error on flush table: " . $dbh->errstr . "\n";
    }
    else
    {
      last if (! ($dbh->errstr =~ /doesn\'t exist/));
      die "Got error on flush: " . $dbh->errstr . "\n";
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_select: ok\n";
  exit(0);
}