First attempt at a perl data structure for marc records
[koha.git] / marc / benchmarks / generaterandomdata
1 #!/usr/bin/perl
2 #
3 # This script generates 80,000 random records in the kohabenchmark database for
4 # the purposes of comparing two different marc storage schemas.  It requires
5 # the presence of a word list for populating the data.  Mine is in
6 # /usr/share/dict/words.  Change that if necessary.  You'll also need to change
7 # your userid and password for the dbi->connect line.
8
9 use DBI;
10
11 my $dbh=DBI->connect("dbi:mysql:kohabenchmark", 'youruserid', 'yourpassword');
12 @subfields = ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n');
13
14
15 open (W, "/usr/share/dict/words");
16 while (<W>) {
17     chomp;
18     push @words, $_;
19 }
20
21 my $tagcounter=0;
22 my $subfieldcounter=0;
23 srand($$|time);
24 for ($bibid=1; $bibid<80000; $bibid++) {
25     my $numtags=int(rand(10)+5);
26     my $localtagcounter=0;
27     for ($i=1; $i<$numtags; $i++) {
28         $localtagcounter++;
29         $tagcounter++;
30         my $tag=$i*40+100;
31         my $numsubfields=int(rand(10)+1);
32         my $subfieldsused;
33         my $localsubfieldcounter=0;
34         my $tagvalue='';
35         for ($j=1; $j<=$numsubfields; $j++) {
36             my $code='';
37             until ($code) { 
38                 my $codepicker=int(rand($#subfields));
39                 if ($subfieldsused->{$subfields[$codepicker]}==0) {
40                     $subfieldsused->{$subfields[$codepicker]}=1;
41                     $code=$subfields[$codepicker];
42                 }
43             }
44             $subfieldcounter++;
45             $localsubfieldcounter++;
46             my $word=$words[int(rand($#words))];
47             $tagvalue.="\$$code $word\0";
48             my $sth=$dbh->prepare("insert into marc_2XX_subfield_table (subfieldid, tagid, tag, bibid, subfieldorder, subfieldcode, subfieldvalue) values (?,?,?,?,?,?,?)");
49             my $error=1;
50             while ($error) {
51                 $sth->execute($subfieldcounter, $tagcounter, $tag, $bibid, $localsubfieldcounter, $code, $word);
52                 $error=$dbh->err;
53                 if ($error) {
54                     sleep 1;
55                     print "ERROR: $error\n";
56                 }
57                 $sth->finish;
58             }
59         }
60         $tagvalue=~s/\0$//;
61         my $error=1;
62         my $sth=$dbh->prepare("insert into marc_0XX_tag_table (bibcode, tagnumber, tagorder, tagvalue) values (?, ?, ?, ?)");
63         while ($error) {
64             $sth->execute($bibid, $tag, $localtagcounter, $tagvalue);
65             $error=$dbh->err;
66             if ($error) {
67                 sleep 1;
68                 print "ERROR: $error\n";
69             }
70             $sth->finish;
71         }
72     }
73 }