summaryrefslogtreecommitdiff
path: root/big_sqlite_db/bigdb.pl
blob: 429970d7466dc4672809456f91f3040a40ce20b0 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 );