summaryrefslogtreecommitdiff
path: root/big_sqlite_db/bigdb.pl
diff options
context:
space:
mode:
authorstderr64 <stderr64@null.net>2024-10-24 23:04:43 +0300
committerstderr64 <stderr64@null.net>2024-10-24 23:04:43 +0300
commitab607fc39b6dfc766f7481c33e5f1cf35a2f55d9 (patch)
tree56317bc3756fa1439f748fda422996c526a76b14 /big_sqlite_db/bigdb.pl
downloadexperiments-ab607fc39b6dfc766f7481c33e5f1cf35a2f55d9.tar.gz
experiments-ab607fc39b6dfc766f7481c33e5f1cf35a2f55d9.tar.zst
Recreated repository
Diffstat (limited to 'big_sqlite_db/bigdb.pl')
-rwxr-xr-xbig_sqlite_db/bigdb.pl81
1 files changed, 81 insertions, 0 deletions
diff --git a/big_sqlite_db/bigdb.pl b/big_sqlite_db/bigdb.pl
new file mode 100755
index 0000000..429970d
--- /dev/null
+++ b/big_sqlite_db/bigdb.pl
@@ -0,0 +1,81 @@
+#!/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 );