More updates from Bernhard Fischer
[rrdtool.git] / examples / perftest.pl.in
1 #! @PERL@
2 #
3 # $Id:$
4 #
5 # Created By Tobi Oetiker <tobi@oetiker.ch>
6 # Date 2006-10-27
7 #
8 #makes programm work AFTER install
9
10 use lib qw( @prefix@/lib/perl );
11
12 print <<NOTE;
13
14 RRDtool Performance Tester
15 --------------------------
16 Runnion on $RRDs::VERSION;
17
18 RRDtool update performance is ultimately disk-bound. Since very little data
19 does actually get written to disk in a single update, the performance
20 is highly dependent on the cache situation in your machine.
21
22 This test tries to cater for this. It works like this:
23
24 1) Create 20k RRD files (and sync them to disk)
25
26 2) Update the RRD files several times in a row.
27    We run the Update several times to see the difference
28    it makes in the cache.
29
30 NOTE
31
32 use strict;
33 use Time::HiRes qw(time);
34 use RRDs;
35 use IO::File;
36 use Time::HiRes qw( usleep );
37
38 sub create($$){
39   my $file = shift;
40   my $time = shift;
41   my $start = time; #since we loaded HiRes
42   RRDs::create  ( $file.".rrd", "-b$time", qw(
43                         -s300                        
44                         DS:in:GAUGE:400:U:U
45                         DS:out:GAUGE:400:U:U
46                         RRA:AVERAGE:0.5:1:600
47                         RRA:AVERAGE:0.5:6:600
48                         RRA:MAX:0.5:6:600
49                         RRA:AVERAGE:0.5:24:600
50                         RRA:MAX:0.5:24:600
51                         RRA:AVERAGE:0.5:144:600
52                         RRA:MAX:0.5:144:600
53                 ));
54    my $total = time - $start;
55    my $error =  RRDs::error;
56    die $error if $error;
57    return $total;
58 }
59
60 sub update($$){
61   my $file = shift;
62   my $time = shift;
63   my $in = rand(1000);
64   my $out = rand(1000);
65   my $start = time;
66   my $ret = RRDs::updatev($file.".rrd", $time.":$in:$out");
67 #  print join("",map {"  $_ " . $ret->{$_}."\n" } grep /AVERAGE.\[1\]/, sort keys %$ret)."\n** $time\n\n";
68   # sync updates to disk immediately  
69 #  usleep(1) if (rand(3) <1 );
70   my $total = time - $start;
71   my $error =  RRDs::error;
72   die $error if $error;
73   return $total;
74 }
75
76 sub tune($){
77   my $file = shift;
78   my $start = time;
79   RRDs::tune ($file.".rrd", "-a","in:U","-a","out:U","-d","in:GAUGE","-d","out:GAUGE");
80   my $total = time - $start;
81   my $error =  RRDs::error;
82   die $error if $error;
83   return $total;
84 }
85
86 sub infofetch($){
87   my $file = shift;
88   my $start = time;
89   my $info = RRDs::info ($file.".rrd");
90   my $error =  RRDs::error;
91   die $error if $error;
92   my $lasttime =  $info->{last_update} - $info->{last_update} % $info->{step};           
93   my $fetch = RRDs::fetch ($file.".rrd",'AVERAGE','-s',$lasttime-1,'-e',$lasttime);
94   my $total = time - $start;
95   my $error =  RRDs::error;
96   die $error if $error;
97   return $total;
98 }
99
100 sub stddev ($$$){ #http://en.wikipedia.org/wiki/Standard_deviation
101   my $sum = shift;
102   my $squaresum = shift;
103   my $count = shift;
104   return sqrt( 1 / $count * ( $squaresum - $sum*$sum / $count ))
105 }
106
107 sub makerrds($$$$){
108     my $count = shift;
109     my $total = shift;
110     my $list = shift;
111     my $time = shift;
112     my @files;
113     for (1..$count){
114         my $id = sprintf ("%07d",$total);
115         $id =~ s/^(.)(.)(.)(.)(.)//;
116         push @$list, "$1/$2/$3/$4/$5/$id";    
117         -d "$1" or mkdir "$1";
118         -d "$1/$2" or mkdir "$1/$2";
119         -d "$1/$2/$3" or mkdir "$1/$2/$3";
120         -d "$1/$2/$3/$4" or mkdir "$1/$2/$3/$4";
121         -d "$1/$2/$3/$4/$5" or mkdir "$1/$2/$3/$4/$5";
122         push @files, $list->[$total];
123         create $list->[$total++],$time-2;
124         print STDERR ".";
125     }
126    for (@files){ 
127         my $fd = new IO::File("$_.rrd","r");
128         if (defined $fd) {
129             $fd->sync;
130             $fd->close;
131         } else {
132             warn "failed to sync $_\n";
133         }        
134     }
135     return $count;
136 }
137     
138     
139 sub main (){
140     mkdir "db-$$" or die $!;
141     chdir "db-$$";
142
143     my $step = 100000; # number of rrds to creat for every round
144     
145     my @path;
146     my $time=int(time);
147
148     my $tracksize = 0;
149     my $uppntr = 0;
150
151     
152     my %squaresum = ( cr => 0, up => 0 );
153     my %sum = ( cr => 0, up => 0 );
154     my %count =( cr => 0, up => 0 );
155
156     my $printtime = time;
157     while (1) {
158         # enhance the track
159             $time += 300;
160         $tracksize += makerrds $step,$tracksize,\@path,$time;            
161         # run benchmark
162         for (0..10){
163             $time += 300;
164             my $count = 0;
165             my $sum = 0;
166             my $squaresum = 0;
167             for (my $i = 0; $i<$tracksize;$i ++){
168                my $elapsed = update($path[$i],$time); 
169                $sum += $elapsed;
170                $squaresum += $elapsed**2;
171                $count++;
172             };
173 #            for (my $i = 0; $i<$tracksize;$i ++){
174 #              my $fh = new IO::File "$path[$i].rrd","r";
175 #              if (defined $fh) {
176 #                  $fh->sync;
177 #                  $fh->close;
178 #                } else {
179 #                  warn "failed to sync $path[$i]\n";
180 #               }       
181 #            }
182             my $ups = $count/$sum;
183             my $sdv = stddev($sum,$squaresum,$count);
184             printf STDERR "%4d %6.0f Up/s (%6.5f sdv)\n",$count,$ups,$sdv;
185         }
186         print STDERR "\n";
187         exit ;
188     }
189 }
190
191 main;
192