KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > StressTest


1 package org.jrobin.demo;
2
3 /* ============================================================
4  * JRobin : Pure java implementation of RRDTool's functionality
5  * ============================================================
6  *
7  * Project Info: http://www.jrobin.org
8  * Project Lead: Sasa Markovic (saxon@jrobin.org);
9  *
10  * (C) Copyright 2003, by Sasa Markovic.
11  *
12  * This library is free software; you can redistribute it and/or modify it under the terms
13  * of the GNU Lesser General Public License as published by the Free Software Foundation;
14  * either version 2.1 of the License, or (at your option) any later version.
15  *
16  * Developers: Sasa Markovic (saxon@jrobin.org)
17  * Arne Vandamme (cobralord@jrobin.org)
18  *
19  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License along with this
24  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */

27
28 import org.jrobin.core.*;
29 import org.jrobin.graph.RrdGraphDef;
30 import org.jrobin.graph.RrdGraph;
31
32 import java.io.IOException JavaDoc;
33 import java.io.BufferedReader JavaDoc;
34 import java.io.FileReader JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.awt.*;
37
38 class StressTest {
39     static final String JavaDoc FACTORY_NAME = "NIO";
40
41     static final String JavaDoc RRD_PATH = Util.getJRobinDemoPath("stress.rrd");
42     static final long RRD_START = 946710000L;
43     static final long RRD_STEP = 30;
44
45     static final String JavaDoc RRD_DATASOURCE_NAME = "T";
46     static final int RRD_DATASOURCE_COUNT = 6;
47
48     static final long TIME_START = 1060142010L;
49     static final long TIME_END = 1080013472L;
50
51     static final String JavaDoc PNG_PATH = Util.getJRobinDemoPath("stress.png");
52     static final int PNG_WIDTH = 400;
53     static final int PNG_HEIGHT = 250;
54
55     static void printLapTime(String JavaDoc message) {
56         System.out.println(message + " " + Util.getLapTime());
57     }
58
59     public static void main(String JavaDoc[] args) throws RrdException, IOException JavaDoc {
60         if(args.length == 0) {
61             System.out.println("Usage: StressTest [path to stress-test.txt file]");
62             System.out.println("You can download separate stress-test.txt file from:");
63             System.out.println("http://www.sourceforge.net/projects/jrobin");
64             System.exit(-1);
65         }
66         System.out.println("********************************************************************");
67         System.out.println("* JRobinStressTest *");
68         System.out.println("* *");
69         System.out.println("* This demo creates single RRD file and tries to update it *");
70         System.out.println("* more than 600.000 times. Real data (> 20Mb) is obtained from the *");
71         System.out.println("* stress-test.txt file provided by Vadim Tkachenko *");
72         System.out.println("* (http://diy-zoning.sourceforge.net). *");
73         System.out.println("* *");
74         System.out.println("* Finally, a single PNG graph will be created from the RRD file. *");
75         System.out.println("* The stress test takes about one hour to complete on a 1.6GHz *");
76         System.out.println("* computer with 256MB of RAM. *");
77         System.out.println("********************************************************************");
78         printLapTime("Starting demo at " + new Date JavaDoc());
79         RrdDb.setDefaultFactory(FACTORY_NAME);
80         printLapTime("Backend factory set to " + FACTORY_NAME);
81         // create RRD database
82
printLapTime("Creating RRD definition");
83         RrdDef def = new RrdDef(RRD_PATH);
84         def.setStartTime(RRD_START);
85         def.setStep(RRD_STEP);
86         for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) {
87             def.addDatasource(RRD_DATASOURCE_NAME + i, "GAUGE", 90, -60, 85);
88         }
89         def.addArchive("LAST", 0.5, 1, 5760);
90         def.addArchive("MIN", 0.5, 1, 5760);
91         def.addArchive("MAX", 0.5, 1, 5760);
92         def.addArchive("AVERAGE", 0.5, 5, 13824);
93         def.addArchive("MIN", 0.5, 5, 13824);
94         def.addArchive("MAX", 0.5, 5, 13824);
95         def.addArchive("AVERAGE", 0.5, 60, 16704);
96         def.addArchive("MIN", 0.5, 60, 16704);
97         def.addArchive("MAX", 0.5, 60, 16704);
98         def.addArchive("AVERAGE", 0.5, 1440, 50000);
99         def.addArchive("MIN", 0.5, 1440, 50000);
100         def.addArchive("MAX", 0.5, 1440, 50000);
101         printLapTime("Definition created, creating RRD file");
102         RrdDb rrd = RrdDbPool.getInstance().requestRrdDb(def);
103         printLapTime("RRD file created: " + RRD_PATH);
104         BufferedReader JavaDoc r = new BufferedReader JavaDoc(new FileReader JavaDoc(args[0]));
105         printLapTime("Buffered reader created, processing data");
106         int count = 0;
107         Date JavaDoc updateStart = new Date JavaDoc();
108         for(String JavaDoc line; (line = r.readLine()) != null;) {
109             Sample sample = rrd.createSample();
110             try {
111                 sample.setAndUpdate(line);
112                 if(++count % 1000 == 0) {
113                     Date JavaDoc now = new Date JavaDoc();
114                     long speed = (long)(count * 1000.0 / (now.getTime() - updateStart.getTime()));
115                     printLapTime(count + " samples stored, " + speed + " updates/sec");
116                 }
117             }
118             catch(RrdException e) {
119                 printLapTime("RRD ERROR: " + line);
120             }
121         }
122         RrdDbPool.getInstance().release(rrd);
123         printLapTime("FINISHED: " + count + " samples stored");
124         // GRAPH
125
printLapTime("Creating composite graph definition");
126         RrdGraphDef gdef = new RrdGraphDef(TIME_START, TIME_END);
127         gdef.setTitle("Temperatures");
128         gdef.setVerticalLabel("Fahrenheits");
129         final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA,
130              Color.CYAN, Color.ORANGE };
131         // datasources
132
for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) {
133             String JavaDoc name = RRD_DATASOURCE_NAME + i;
134             gdef.datasource(name, RRD_PATH, name, "AVERAGE");
135         }
136         // lines
137
for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) {
138             String JavaDoc name = RRD_DATASOURCE_NAME + i;
139             gdef.line(name, colors[i], name);
140         }
141         gdef.comment("@c");
142         gdef.comment("\nOriginal data provided by diy-zoning.sf.net@c");
143         printLapTime("Graph definition created");
144         RrdGraph g = new RrdGraph(gdef, true);
145         g.saveAsPNG(PNG_PATH, PNG_WIDTH, PNG_HEIGHT);
146         printLapTime("Graph saved: " + PNG_PATH);
147         printLapTime("Finished at " + new Date JavaDoc());
148     }
149 }
150
Popular Tags