Initial revision
[rrdtool.git] / bindings / perl-shared / t / base.t
1 #! /usr/bin/perl 
2
3 BEGIN { $| = 1; print "1..7\n"; }
4 END {
5   print "not ok 1\n" unless $loaded;
6   unlink "demo.rrd";
7 }
8
9 sub ok
10 {
11     my($what, $result) = @_ ;
12     $ok_count++;
13     print "not " unless $result;
14     print "ok $ok_count $what\n";
15 }
16
17 use strict;
18 use vars qw(@ISA $loaded);
19
20 use RRDs;
21 $loaded = 1;
22 my $ok_count = 1;
23
24 ok("loading",1);
25
26 ######################### End of black magic.
27
28 my $STEP  = 100;
29 my $RUNS  = 500;
30 my $GRUNS = 4;
31 my $RRD1  = "demo1.rrd";
32 my $RRD2  = "demo2.rrd";
33 my $GIF1  = "demo1.gif";
34 my $GIF2  = "demo2.gif";
35 my $time  = 30*int(time/30);
36 my $START = $time-$RUNS*$STEP;
37
38 my @options = ("-b", $START, "-s", $STEP,
39  "DS:a:GAUGE:2000:U:U",
40  "DS:b:GAUGE:200:U:U",
41  "DS:c:GAUGE:200:U:U",
42  "DS:d:GAUGE:200:U:U",
43  "DS:e:DERIVE:200:U:U",
44  "RRA:AVERAGE:0.5:1:5000",
45  "RRA:AVERAGE:0.5:10:500");
46
47 print "* Creating RRD $RRD1 starting at $time.\n\n";
48 RRDs::create $RRD1, @options;
49
50 my $ERROR = RRDs::error;
51 ok("create 1", !$ERROR);                                                        #  2
52 if ($ERROR) {
53   die "$0: unable to create `$RRD1': $ERROR\n";
54 }
55
56 print "* Creating RRD $RRD2 starting at $time.\n\n";
57 RRDs::create $RRD2, @options;
58
59 $ERROR= RRDs::error;
60 ok("create 2",!$ERROR);                                                 #  3
61 if ($ERROR) {
62   die "$0: unable to create `$RRD2': $ERROR\n";
63 }
64
65 my $last = RRDs::last $RRD1;
66 if ($ERROR = RRDs::error) {
67   die "$0: unable to get last `$RRD1': $ERROR\n";
68 }
69 ok("last 1", $last == $START);                                          #  4
70
71 $last = RRDs::last $RRD2;
72 if ($ERROR = RRDs::error) {
73   die "$0: unable to get last `$RRD2': $ERROR\n";
74 }
75 ok("last 2", $last == $START);                                          #  5
76
77 print "* Filling $RRD1 and $RRD2 with $RUNS*5 values. One moment please ...\n";
78 print "* If you are running over NFS this will take *MUCH* longer\n\n";
79
80 srand(int($time / 100));
81
82 @options = ();
83
84 my $counter = 1e7;
85 for (my $t=$START+1;
86      $t<$START+$STEP*$RUNS;
87      $t+=$STEP+int((rand()-0.5)*7)){
88   $counter += 2500*sin($t/2000)*$STEP;
89   my $data = (1000+500*sin($t/1000)).":".
90       (1000+900*sin($t/2330)).":".
91       (2000*cos($t/1550)).":".
92       (3220*sin($t/3420)).":$counter";
93   push(@options, "$t:$data");
94   RRDs::update $RRD1, "$t:$data";
95   if ($ERROR = RRDs::error) {
96     warn "$0: unable to update `$RRD1': $ERROR\n";
97   }
98 }
99
100 ok("update 1",!$ERROR);                                                 #  3
101
102 RRDs::update $RRD2, @options;
103
104 ok("update 2",!$ERROR);                                                 #  3
105
106 if ($ERROR = RRDs::error) {
107   die "$0: unable to update `$RRD2': $ERROR\n";
108 }
109
110 print "* Creating $GRUNS graphs: $GIF1 & $GIF2\n\n";
111 my $now = $time;
112 for (my $i=0;$i<$GRUNS;$i++) {
113   my @rrd_gifs = ($RRD1, $GIF1, $RRD2, $GIF2);
114   while (@rrd_gifs) {
115     my $RRD = shift(@rrd_gifs);
116     my $GIF = shift(@rrd_gifs);
117     my ($graphret,$xs,$ys) = RRDs::graph $GIF, "--title", 'Test GRAPH',
118           "--vertical-label", 'Dummy Units', "--start", (-$RUNS*$STEP),
119           "DEF:alpha=$RRD:a:AVERAGE",
120           "DEF:beta=$RRD:b:AVERAGE",
121           "DEF:gamma=$RRD:c:AVERAGE",
122           "DEF:delta=$RRD:d:AVERAGE",
123           "DEF:epsilon=$RRD:e:AVERAGE",
124           "CDEF:calc=alpha,beta,+,2,/",
125           "AREA:alpha#0022e9:Short",
126           "STACK:beta#00b871:Demo Text",
127           "LINE1:gamma#ff0000:Line 1",
128           "LINE2:delta#888800:Line 2",
129           "LINE3:calc#00ff44:Line 3",
130           "LINE3:epsilon#000000:Line 4",
131           "HRULE:1500#ff8800:Horizontal Line at 1500",
132           "PRINT:alpha:AVERAGE:Average Alpha %1.2lf",
133           "PRINT:alpha:MIN:Min Alpha %1.2lf %s",
134           "PRINT:alpha:MIN:Min Alpha %1.2lf",
135           "PRINT:alpha:MAX:Max Alpha %1.2lf",
136           "GPRINT:calc:AVERAGE:Average calc %1.2lf %s",
137           "GPRINT:calc:AVERAGE:Average calc %1.2lf",
138           "GPRINT:calc:MAX:Max calc %1.2lf",
139           "GPRINT:calc:MIN:Min calc %1.2lf",
140           "VRULE:".($now-3600)."#008877:60 Minutes ago",
141           "VRULE:".($now-7200)."#008877:120 Minutes ago";
142
143     if ($ERROR = RRDs::error) {
144       print "ERROR: $ERROR\n";
145     } else {
146       print "GIF Size: ${xs}x${ys}\n";
147       print "Graph Return:\n",(join "\n", @$graphret),"\n\n";
148     }
149   }
150 }
151
152
153
154 my ($start,$step,$names,$array) = RRDs::fetch $RRD1, "AVERAGE";
155 $ERROR = RRDs::error;
156 print "ERROR: $ERROR\n" if $ERROR ;
157 print "start=$start, step=$step\n";
158 print "             "; 
159 map {printf("%12s",$_)} @$names ;
160 foreach my $line (@$array){
161   print "".localtime($start),"   ";
162   $start += $step; 
163   foreach my $val (@$line) {            
164     printf "%12.1f", $val;
165   }
166   print "\n";
167 }