1#!/usr/bin/env perl
2
3# Copyright (C) 2002 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 many processes to test a MySQL server.
21#
22
23$opt_loop_count=10000; # 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_threads=2;
36$opt_host=$opt_user=$opt_password=""; $opt_db="test";
37
38GetOptions("host=s","db=s","user=s","password=s","loop-count=i","skip-create","skip-in","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","threads=i") || 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 truncate from $opt_threads multiple connections $opt_loop_count times\n";
42
43@testtables = ( ["bench_f31", "type=heap"]);
44
45####
46####  Start timeing and start test
47####
48
49$start_time=new Benchmark;
50$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
51		    $opt_user, $opt_password,
52		  { PrintError => 0}) || die $DBI::errstr;
53if (!$opt_skip_create)
54{
55  my $table_def;
56  foreach $table_def (@testtables)
57  {
58    my ($table,$extra)= ($table_def->[0], $table_def->[1]);
59    print "Creating table $table in database $opt_db\n";
60    $dbh->do("drop table if exists $table");
61    $dbh->do("create table $table".
62	     " (id int(6) not null,".
63	     " info varchar(32)," .
64	     " marker timestamp," .
65	     " flag int not null," .
66	     " primary key(id)) $extra")
67
68      or die $DBI::errstr;
69  }
70}
71
72$dbh->disconnect; $dbh=0;	# Close handler
73$|= 1;				# Autoflush
74
75####
76#### Start the tests
77####
78
79for ($i=0 ; $i < $opt_threads ; $i ++)
80{
81  test_truncate() if (($pid=fork()) == 0); $work{$pid}="truncate";
82}
83
84print "Started $opt_threads threads\n";
85
86$errors=0;
87$running_insert_threads=$opt_threads;
88while (($pid=wait()) != -1)
89{
90  $ret=$?/256;
91  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
92  --$running_insert_threads;
93  $errors++ if ($ret != 0);
94}
95
96#
97# Cleanup
98#
99
100if (!$opt_skip_delete && !$errors)
101{
102  my $table_def;
103  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
104		      $opt_user, $opt_password,
105		    { PrintError => 0}) || die $DBI::errstr;
106
107  foreach $table_def (@testtables)
108  {
109    $dbh->do("drop table " . $table_def->[0]);
110  }
111  $dbh->disconnect; $dbh=0;	# Close handler
112}
113
114print ($errors ? "Test failed\n" :"Test ok\n");
115$end_time=new Benchmark;
116print "Total time: " .
117  timestr(timediff($end_time, $start_time),"noc") . "\n";
118
119exit(0);
120
121
122#
123# Insert records in the table
124#
125
126sub test_truncate
127{
128  my ($dbh,$i,$j,$count,$table_def,$table);
129
130  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
131		      $opt_user, $opt_password,
132		    { PrintError => 0}) || die $DBI::errstr;
133
134  for ($count=0; $count < $opt_loop_count ; $count++)
135  {
136    my ($table)= ($testtables[0]->[0]);
137    $dbh->do("truncate table $table") || die "Got error on truncate: $DBI::errstr\n";
138  }
139  $dbh->disconnect; $dbh=0;
140  print "Test_truncate: Run $count times\n";
141  exit(0);
142}
143