1#!/usr/bin/perl
2############################################################################
3#     Stress test for MySQL/InnoDB combined database
4#     (c) 2002 Innobase Oy & MySQL AB
5#
6############################################################################
7
8use Cwd;
9use DBI;
10use Benchmark;
11
12$opt_loop_count = 100000;
13
14$pwd = cwd(); $pwd = "." if ($pwd eq '');
15require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
16
17print "Innotest2: MySQL/InnoDB stress test in Perl for FOREIGN keys\n";
18print "------------------------------------------------------------\n";
19print "This is a randomized stress test for concurrent inserts,\n";
20print "updates, deletes, commits and rollbacks with foreign keys with\n";
21print "the ON DELETE ... clause. The test will generate\n";
22print "also a lot of deadlocks, duplicate key errors, and other SQL errors.\n";
23print "\n";
24print "You should run innotest2, innotest2a, and innotest2b concurrently.\n";
25print "The thing to watch is that the server does not crash or does not\n";
26print "print to the .err log anything. Currently, due to a buglet in MySQL,\n";
27print "warnings about MySQL lock reservations can appear in the .err log.\n";
28print "The test will run very long, even several hours. You can kill\n";
29print "the perl processes running this test at any time and do CHECK\n";
30print "TABLE on tables innotest2a, b, c, d in the 'test' database.\n";
31print "\n";
32print "Some of these stress tests will print a lot of SQL errors\n";
33print "to the standard output. That is not to be worried about.\n";
34print "You can direct the output to a file like this:\n";
35print "perl innotest2 > out2\n\n";
36
37print "Generating random keys\n";
38$random[$opt_loop_count] = 0;
39$rnd_str[$opt_loop_count] = "a";
40
41for ($i = 0; $i < $opt_loop_count; $i++) {
42
43	$random[$i] = ($i * 63857) % $opt_loop_count;
44
45  	if (0 == ($random[$i] % 3)) {
46  		$rnd_str[$i] = "khD";
47	} else { if (1 == ($random[$i] % 3)) {
48		$rnd_str[$i] = "khd";
49	} else { if (2 == ($random[$i] % 3)) {
50		$rnd_str[$i] = "kHd";
51	}}}
52
53	for ($j = 0; $j < (($i * 764877) % 10); $j++) {
54		$rnd_str[$i] = $rnd_str[$i]."k";
55	}
56}
57
58####
59####  Connect
60####
61
62$dbh = $server->connect()
63|| die $dbh->errstr;
64
65$dbh->do("set autocommit = 0");
66
67for ($i = 0; $i < 1; $i++) {
68	print "loop $i\n";
69
70	print "dropping table innotest2a\n";
71	$dbh->do("drop table innotest2a");
72
73	print "dropping table innotest2b\n";
74	$dbh->do("drop table innotest2b");
75
76	print "dropping table innotest2c\n";
77	$dbh->do("drop table innotest2c");
78
79	print "dropping table innotest2d\n";
80	$dbh->do("drop table innotest2d");
81
82	print "creating table innotest2b\n";
83	$dbh->do(
84	"create table innotest2b (A INT NOT NULL AUTO_INCREMENT, D INT NOT NULL, B VARCHAR(200) NOT NULL, C VARCHAR(175), PRIMARY KEY (A, D, B), INDEX (B, C), INDEX (C)) TYPE = INNODB")
85	|| die $dbh->errstr;
86
87	print "creating table innotest2a\n";
88
89	$dbh->do(
90	"create table innotest2a (A INT NOT NULL AUTO_INCREMENT, D INT NOT NULL, B VARCHAR(200) NOT NULL, C VARCHAR(175), PRIMARY KEY (A, D, B), INDEX (B, C), INDEX (C), FOREIGN KEY (A, D) REFERENCES innotest2b (A, D) ON DELETE CASCADE) TYPE = INNODB")
91	|| die $dbh->errstr;
92
93	print "creating table innotest2c\n";
94
95	$dbh->do(
96	"create table innotest2c (A INT NOT NULL AUTO_INCREMENT, D INT NOT NULL, B VARCHAR(200) NOT NULL, C VARCHAR(175), PRIMARY KEY (A, D, B), INDEX (B, C), INDEX (C), FOREIGN KEY (A, D) REFERENCES innotest2a (A, D) ON DELETE CASCADE, FOREIGN KEY (B, C) REFERENCES innotest2a (B, C) ON DELETE CASCADE) TYPE = INNODB")
97	|| die $dbh->errstr;
98
99	print "creating table innotest2d\n";
100
101	$dbh->do(
102	"create table innotest2d (A INT AUTO_INCREMENT, D INT, B VARCHAR(200), C VARCHAR(175), UNIQUE KEY (A, D, B), INDEX (B, C), INDEX (C), FOREIGN KEY (C) REFERENCES innotest2c (C) ON DELETE SET NULL, FOREIGN KEY (B, C) REFERENCES innotest2c (B, C) ON DELETE SET NULL) TYPE = INNODB")
103	|| die $dbh->errstr;
104	print "created\n";
105
106	for ($j = 0; $j < $opt_loop_count - 10; $j = $j + 2) {
107		$dbh->do(
108		"insert into innotest2b (D, B, C) values (5, '".$rnd_str[$j]."' ,'".$rnd_str[$j]."')")
109		|| print $dbh->errstr;
110
111		$dbh->do(
112		"insert into innotest2a (D, B, C) values (5, '".$rnd_str[$j]."' ,'".$rnd_str[$j]."')")
113		|| print $dbh->errstr;
114
115		$dbh->do(
116		"insert into innotest2c (D, B, C) values (5, '".$rnd_str[$j]."' ,'".$rnd_str[$j]."')")
117		|| print $dbh->errstr;
118
119		$dbh->do(
120		"insert into innotest2d (D, B, C) values (5, '".$rnd_str[$j]."' ,'".$rnd_str[$j]."')")
121		|| print $dbh->errstr;
122
123		$dbh->do("delete from innotest2b where A = ".$random[$random[$j]])
124		|| print $dbh->errstr;
125
126		if (0 == ($j % 10)) {
127			$dbh->do("commit");
128		}
129
130		if (0 == ($j % 39)) {
131			$dbh->do("rollback");
132		}
133
134		if (0 == ($j % 1000)) {
135			print "round $j\n";
136		}
137		if (0 == ($j % 20000)) {
138			print "Checking tables...\n";
139			$dbh->do("check table innotest2a");
140			$dbh->do("check table innotest2b");
141			$dbh->do("check table innotest2c");
142			$dbh->do("check table innotest2d");
143			print "Tables checked.\n";
144		}
145	}
146
147	$dbh->do("commit");
148}
149
150$dbh->disconnect;				# close connection
151