File: rrd_binding_test.cs

package info (click to toggle)
rrdtool 1.9.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,772 kB
  • sloc: ansic: 39,371; sh: 1,810; perl: 1,268; cs: 652; makefile: 573; python: 169; ruby: 61; awk: 30
file content (329 lines) | stat: -rw-r--r-- 12,950 bytes parent folder | download | duplicates (4)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*****************************************************************************
 * RRDLIB .NET Binding Test
 *****************************************************************************
 * Created 2010/06/29 by Chris Larsen
 * Updated 2011/04/15 - Modified the string arrays to use pointers as the old 
 * automatic marshalling of strings didn't seem to work well with 1.4.5
 * 
 * This project tests the .NET binding library by creating an rrd, inserting 
 * data, fetching data, creating graphs, dumping and exporting the data to
 * XML, then restoring from an XML file. The examples follow the tutorial 
 * written by Alex van den Bogaerdt found at 
 * https://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html
 ****************************************************************************/

using System;
using System.Collections;
using System.Runtime.InteropServices;
using dnrrdlib;
using System.IO;

namespace dnrrd_binding_test
{
    class rrd_binding_test
    {
        private static string path = "";

        static void Main(string[] args)
        {
            Console.WriteLine("----- Starting Tests -----");
            Console.WriteLine("RRDLib Version: " + rrd.Version());

            Test_Create();
            Test_Get_Info();
            Test_Update();
            Test_Fetch();
            Test_Graph();
            Test_Graph_Math();
            Test_Graph_Math2();
            Test_Graph_Math3();
            Test_First_Last();
            Test_Dump();
            Test_Xport();
            Test_Restore();
            Test_Tune();
            Console.WriteLine("\n!!!!!! Finished !!!!!!!");
            string inp = Console.ReadLine();
        }

        static void Test_Create()
        {
            // create
            ArrayList al = new ArrayList();
            al.Add("DS:speed:COUNTER:600:U:U");
            al.Add("RRA:AVERAGE:0.5:1:24");
            al.Add("RRA:AVERAGE:0.5:6:10");

            rrd.Create(path + "test_a.rrd", 300, 920804400, (string[])al.ToArray(typeof(string)));
            Console.WriteLine("Test create: Successful!");
        }
        static void Test_Get_Info()
        {
            Console.WriteLine("Try getting info...");
            var info = rrd.Info(path + "test_a.rrd");
            foreach (var kvp in info)
            {
                //info = (rrd_info_t)Marshal.PtrToStructure(info.next, typeof(rrd_info_t));
                Console.Write(kvp.Key + ": ");
                if (kvp.Value is string)
                {
                    Console.WriteLine("\"" + kvp.Value + "\"");
                }
                else
                {
                    Console.WriteLine(kvp.Value);
                }
            }
            Console.WriteLine("Test Info: Successful!");
            //Console.WriteLine("Printing information...");
            //Info_Print(((rrd_info_t)info));
        }
        static void Test_Update()
        {
            Console.WriteLine("Updating RRD...");
            ArrayList al = new ArrayList();

            // set to false if you want to use random values
            if (true)
            {
                al.Add("920804700:12345");
                al.Add("920805000:12357");
                al.Add("920805300:12363");
                al.Add("920805600:12363");
                al.Add("920805900:12363");
                al.Add("920806200:12373");
                al.Add("920806500:12383");
                al.Add("920806800:12393");
                al.Add("920807100:12399");
                al.Add("920807400:12405");
                al.Add("920807700:12411");
                al.Add("920808000:12415");
                al.Add("920808300:12420");
                al.Add("920808600:12422");
                al.Add("920808900:12423");
            }
            else
            {
                UInt32 ts = 920804700;
                for (int i = 0; i < 15; i++)
                {
                    al.Add(ts.ToString() + ":" + rrd.Random());
                    ts += 300;
                }
            }
            rrd.Update(path + "test_a.rrd", null, (string[])al.ToArray(typeof(string)));
            Console.WriteLine("Test update: Successful!");
        }
        static void Test_Fetch()
        {
            // FETCH
            Console.WriteLine("Attempting Fetch...");
            ArrayList al = new ArrayList();
            al.Add("fetch");
            al.Add(path + "test_a.rrd");
            al.Add("AVERAGE");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920809200");
            IntPtr data = new IntPtr();
            string[] rrds = new string[0];
            DateTime start = default(DateTime);
            DateTime end = default(DateTime);
            UInt32 step = 0;
            UInt32 dscnt = 0;
            rrd.Fetch((string[])al.ToArray(typeof(string)), ref start, ref end,
                ref step, ref dscnt, ref rrds, ref data);

            if (end > start)
            {
                for (Int32 ti = rrd.DateTimeToUnixTimestamp(start); ti < rrd.DateTimeToUnixTimestamp(end); ti += (Int32)step)
                {
                    Console.Write(ti + ": ");
                    for (Int32 i = 0; i < (Int32)dscnt; i++)
                    {
                        Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
                        data = new IntPtr(data.ToInt64() + sizeof(double));
                    }
                    Console.Write(Environment.NewLine);
                }
            }

            Console.WriteLine("Test fetch: Successful!");
        }
        static void Test_Graph()
        {
            Console.WriteLine("Creating graph...");
            ArrayList al = new ArrayList();
            al.Add("graph");
            al.Add(path + "graph_simple.png");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920808000");
            al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
            al.Add("LINE2:myspeed#00004D");

            var ret = rrd.Graph((string[])al.ToArray(typeof(string)));
            //TODO: Validate the returned data
            Console.WriteLine("Test graph: Successful!");
        }
        static void Test_Graph_Math()
        {
            Console.WriteLine("Creating graph...");
            ArrayList al = new ArrayList();
            al.Add("graph");
            al.Add(path + "graph_math.png");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920808000");
            al.Add("--vertical-label");
            al.Add("m/s");
            al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
            al.Add("CDEF:realspeed=myspeed,1000,*");
            al.Add("LINE2:realspeed#00004D");
            var ret = rrd.Graph((string[])al.ToArray(typeof(string)));
            //TODO: Validate the returned data
            Console.WriteLine("Test graph: Successful!");
        }
        static void Test_Graph_Math2()
        {
            Console.WriteLine("Creating graph...");
            ArrayList al = new ArrayList();
            al.Add("graph");
            al.Add(path + "graph_math2.png");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920808000");
            al.Add("--vertical-label");
            al.Add("m/s");
            al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
            al.Add("CDEF:kmh=myspeed,3600,*");
            al.Add("CDEF:fast=kmh,100,GT,kmh,0,IF");
            al.Add("CDEF:good=kmh,100,GT,0,kmh,IF");
            al.Add("HRULE:100#0000FF:\"Maximum allowed\"");
            al.Add("AREA:good#00FF00:\"Good speed\"");
            al.Add("AREA:fast#FF0000:\"Too fast\"");
            var ret = rrd.Graph((string[])al.ToArray(typeof(string)));
            //TODO: Validate the returned data
            Console.WriteLine("Test graph: Successful!");
        }
        static void Test_Graph_Math3()
        {
            Console.WriteLine("Creating graph...");
            ArrayList al = new ArrayList();
            al.Add("graph");
            al.Add(path + "graph_math3.png");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920808000");
            al.Add("--vertical-label");
            al.Add("m/s");
            al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
            al.Add("CDEF:nonans=myspeed,UN,0,myspeed,IF");
            al.Add("CDEF:kmh=nonans,3600,*");
            al.Add("CDEF:fast=kmh,100,GT,100,0,IF");
            al.Add("CDEF:over=kmh,100,GT,kmh,100,-,0,IF");
            al.Add("CDEF:good=kmh,100,GT,0,kmh,IF");
            al.Add("HRULE:100#0000FF:\"Maximum allowed\"");
            al.Add("AREA:good#00FF00:\"Good speed\"");
            al.Add("AREA:fast#550000:\"Too fast\"");
            al.Add("STACK:over#FF0000:\"Over speed\"");
            var ret = rrd.Graph((string[])al.ToArray(typeof(string)));
            //TODO: Validate the returned data
            Console.WriteLine("Test graph: Successful!");
        }
        static void Test_First_Last()
        {
            Console.WriteLine("Testing values...");
            Console.WriteLine("First Value: " + rrd.First(path + "test_a.rrd", 0));
            string err = rrd.Get_Error();
            if (err.Length > 1)
                Console.WriteLine("Error: " + err);
            Console.WriteLine("Last Value: " + rrd.Last(path + "test_a.rrd", 0));
            err = rrd.Get_Error();
            if (err.Length > 1)
                Console.WriteLine("Error: " + err);

            DateTime last_update = default(DateTime);
            UInt32 ds_count = 0;
            string[] ds_names = new string[0];
            string[] last_ds = new string[0];
            rrd.Last_Update(path + "test_a.rrd", ref last_update, ref ds_count, ref ds_names, ref last_ds);
            Console.WriteLine("Last Update: " + last_update);
            Console.WriteLine("Value testing successful!");
        }
        static void Test_Dump()
        {
            Console.WriteLine("Dumping RRD...");
            rrd.Dump(path + "test_a.rrd", path + "test_a.xml");
            Console.WriteLine("Test Dump: Successful!");
        }
        static void Test_Xport()
        {
            Console.WriteLine("Exporting RRD...");
            ArrayList al = new ArrayList();
            al.Add("xport");
            al.Add("--start");
            al.Add("920804400");
            al.Add("--end");
            al.Add("920808000");
            al.Add("DEF:myspeed=" + path.Replace(":", "\\:") + "test_a.rrd:speed:AVERAGE");
            al.Add("XPORT:myspeed:\"MySpeed\"");
            IntPtr data = new IntPtr();
            string[] legends = new string[0];
            DateTime start = default(DateTime);
            DateTime end = default(DateTime);
            UInt32 step = 0;
            UInt32 col_cnt = 0;
            rrd.Xport((string[])al.ToArray(typeof(string)), ref start, ref end,
                ref step, ref col_cnt, ref legends, ref data);

            if (end > start)
            {
                for (Int32 ti = rrd.DateTimeToUnixTimestamp(start); ti <= rrd.DateTimeToUnixTimestamp(end); ti += (Int32)step)
                {
                    Console.Write(ti + ": ");
                    for (Int32 i = 0; i < (Int32)col_cnt; i++)
                    {
                        Console.Write(((double)Marshal.PtrToStructure(data, typeof(double))).ToString(" 0.0000000000e+00"));
                        data = new IntPtr(data.ToInt64() + sizeof(double));
                    }
                    Console.Write(Environment.NewLine);
                }
            }
            Console.WriteLine("Test xport: Successful!");
        }
        static void Test_Restore()
        {
            FileInfo rrdDestination = new FileInfo(path + "restored_a.rrd");
            if (rrdDestination.Exists)
            {
                rrdDestination.Delete();
            }

            Console.WriteLine("Restoring RRD...");
            ArrayList al = new ArrayList();
            al.Add("restore");
            al.Add(path + "test_a.xml");
            al.Add(rrdDestination.FullName);
            
            rrd.Restore((string[])al.ToArray(typeof(string)));
            Console.WriteLine("Test restore: Successful!");
        }
        static void Test_Tune()
        {
            Console.WriteLine("Tuning RRD...");
            ArrayList al = new ArrayList();
            al.Add("tune");
            al.Add(path + "restored_a.rrd");
            al.Add("-h");
            al.Add("speed:650");
            rrd.Tune((string[])al.ToArray(typeof(string)));
            Console.WriteLine("Test tune: Successful!");
        }
    }
}