#!/usr/bin/perl use POSIX; use IO::File; use DBI; use DBD::SQLite; srand(); if ( scalar(@ARGV) < 3 ){ print( "Usage:".__FILE__." [database file] [rows to create] [rows before each commit]\n" ); exit( 0 ); } sub gen_random{ return int( rand($_[0]) ); } my $g_db = DBI->connect( "DBI:SQLite:dbname=".$ARGV[0], "", "" ) or die "Failed to open database"; sub tx_reset{ $g_db->do( "COMMIT;" ) or die "Failed to commit"; $g_db->do( "BEGIN;" ) or die "Failed to start transaction"; return; } my $tbl_create = $g_db->prepare( "create table if not exists testtable(rnd_num integer);" ) or die "Failed to prepare query"; $tbl_create->execute() or die "Failed to execute query"; $tbl_create->finish() or die "Failed to finish query"; undef $tbl_create; my $idx_create = $g_db->prepare( "create index if not exists testidx on testtable(rnd_num);" ) or die "Failed to prepare query"; $idx_create->execute() or die "Failed to execute query"; $idx_create->finish() or die "Failed to finish query"; undef $idx_create; my $c_row_count = 0; my $c_uncommitted = 0; my $setcounter = 0; my $iquery = $g_db->prepare( "insert into testtable(rnd_num) values(?);" ) or die "Failed to prepare insert query"; my $rnd_val = gen_random( 100 ); $g_db->do( "pragma synchronous = off;" ) or die "Failed to disable fsync"; $g_db->do( "BEGIN;" ) or die "Failed to start transaction"; for ( ; $c_row_count < int($ARGV[1]); ){ for ( ; $setcounter < 100000; $setcounter++ ){ $iquery->bind_param( 1, $rnd_val, $SQL_INTEGER ) or die "Failed to bind"; $iquery->execute() or die "Failed to execute query"; $iquery->finish() or die "Failed to finish query"; print( "value = ".$rnd_val."\tcount = ".$c_row_count."\n" ); $rnd_val = gen_random( 100 ); $c_row_count++; $c_uncommitted++; if ( $c_uncommitted >= int($ARGV[2]) ){ print( "Commit threshold reached, comitting and starting new transaction\n" ); tx_reset(); print( "Commit finished and new transaction started\n" ); $c_uncommitted = 0; } } print( "value = ".$rnd_val."\tcount = ".$c_row_count."\n" ); print( "Generated set of 100000 rows, waiting 1 second before next\n" ); $setcounter = 0; sleep( 1 ); } $g_db->do( "COMMIT;" ) or die "Failed to do final commit"; $g_db->disconnect() or die "Failed to disconnect"; undef $iquery; undef $c_row_count; undef $c_uncommitted; undef $g_db; undef $rnd_val; undef $setcounter; exit( 0 );