big_record.pl 2.6 KB
Newer Older
unknown's avatar
unknown committed
1 2
#!/usr/bin/perl

3
# This is a test with stores big records in a blob.
unknown's avatar
unknown committed
4
# Note that for the default test the mysql server should have been
5 6
# started with at least 'mysqld -O max_allowed_packet=30M' and you should have
# at least 256M memory in your computer.
unknown's avatar
unknown committed
7 8

use DBI;
9 10 11 12 13
use Getopt::Long;

$opt_host="";
$opt_user=$opt_password="";
$opt_db="test";
14
$opt_rows=20;			# Test of blobs up to ($rows-1)*100000+1 bytes
15 16
$opt_compress=0;
$opt_table="test_big_record";
17
$opt_loop_count=100000; # Change this to make test harder/easier
unknown's avatar
unknown committed
18

19
GetOptions("host=s","db=s","user=s", "password=s", "table=s", "rows=i",
20
	   "compress", "loop-count=i") || die "Aborted";
unknown's avatar
unknown committed
21 22 23

print "Connection to database $test_db\n";

24 25
$extra_options="";
$extra_options.=":mysql_compression=1" if ($opt_compress);
unknown's avatar
unknown committed
26

27
$dbh = DBI->connect("DBI:mysql:$opt_db:$host$extra_options",$opt_user,$opt_password) || die "Can't connect: $DBI::errstr\n";
unknown's avatar
unknown committed
28

29 30 31
$dbh->do("drop table if exists $opt_table");

print "Creating table $opt_table\n";
unknown's avatar
unknown committed
32 33

($dbh->do("\
34
CREATE TABLE $opt_table (
unknown's avatar
unknown committed
35
  auto int(5) unsigned NOT NULL DEFAULT '0' auto_increment,
36
  test longblob,
unknown's avatar
unknown committed
37 38
  PRIMARY KEY (auto))"))  or die $DBI::errstr;

39 40 41
print "Inserting $opt_rows records\n";

$|=1;	# Flush output to stdout to be able to monitor process
unknown's avatar
unknown committed
42

43
for ($i=0 ; $i < $opt_rows ; $i++)
unknown's avatar
unknown committed
44
{
45
  $tmp= chr(65+($i % 16)) x ($i*100000+1);
unknown's avatar
unknown committed
46
  $tmp= $dbh->quote($tmp);
47
  $dbh->do("insert into $opt_table (test) values ($tmp)") or die $DBI::errstr;
48
  print ".";
unknown's avatar
unknown committed
49 50
}

51
print "\nReading records\n";
52 53

$sth=$dbh->prepare("select * from $opt_table", { "mysql_use_result" => 1}) or die $dbh->errstr;
unknown's avatar
unknown committed
54 55 56 57 58 59

$sth->execute() or die $sth->errstr;

$i=0;
while (($row = $sth->fetchrow_arrayref))
{
60
  die "Record $i had wrong data in blob" if ($row->[1] ne (chr(65+($i % 16)) x ($i*100000+1)));
unknown's avatar
unknown committed
61 62 63
  $i++;
}

64
die "Didn't get all rows from server" if ($i != $opt_rows);
unknown's avatar
unknown committed
65

66 67 68
#
# Test by insert/updating/deleting random rows for a while
#
unknown's avatar
unknown committed
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
print "Testing insert/update/delete\n";

$max_row_id= $rows;
for ($i= 0 ; $i < $opt_loop_count ; $i++)
{
  $length= int(rand 65535);
  $tmp= chr(65+($i % 16)) x $length;
  $tmp= $dbh->quote($tmp);
  $dbh->do("insert into $opt_table (test) values ($tmp)") or die $DBI::errstr;
  $max_row_id++;
  $length=int(rand 65535);
  $tmp= chr(65+($i % 16)) x $length;
  $tmp= $dbh->quote($tmp);
  $id= int(rand $max_row_id);
  $dbh->do("update $opt_table set test= $tmp where auto= $id") or die $DBI::errstr;
  if (($i % 2) == 1)
  {
    $id= int(rand $max_row_id);
    $dbh->do("delete from $opt_table where auto= $id") or die $DBI::errstr;
  }
  print "." if ($i % ($opt_loop_count/100) == 1);
}

# $dbh->do("drop table $opt_table") or die $DBI::errstr;

print "\nTest ok\n";
unknown's avatar
unknown committed
96
exit 0;