KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > demo > graph > LazyDemo


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

25 package org.jrobin.demo.graph;
26
27 import org.jrobin.core.Util;
28 import org.jrobin.core.RrdException;
29 import org.jrobin.graph.RrdGraphDef;
30 import org.jrobin.graph.RrdGraph;
31 import org.jrobin.graph.FetchSourceList;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.awt.*;
36 import java.util.GregorianCalendar JavaDoc;
37
38 /**
39  * <p>This demo shows the use of the FetchSourceList class, the RrdOpener class
40  * and the RrdGraphDef setLazy() method.</p>
41  *
42  * @author Arne Vandamme (cobralord@cherrymon.org)
43  */

44 public class LazyDemo
45 {
46     private static String JavaDoc rrd1 = "eth0.rrd";
47     private static String JavaDoc rrd2 = "eth1.rrd";
48     private static String JavaDoc graph1 = Util.getJRobinDemoPath( "lazy-graph1.png" );
49     private static String JavaDoc graph2 = Util.getJRobinDemoPath( "lazy-graph2.png" );
50     private static String JavaDoc graph3 = Util.getJRobinDemoPath( "lazy-graph3.png" );
51     private static String JavaDoc graph4 = Util.getJRobinDemoPath( "lazy-graph4.png" );
52     private static String JavaDoc demoResources = "";
53
54     private static int runCount = 0;
55     private static long runStart, runStop;
56     private static long[] runTimes = new long[4];
57
58     private static RrdGraph graph;
59     private static RrdGraphDef graphDef;
60
61     private static GregorianCalendar JavaDoc start1, end1, start2, end2, start3, end3, start4, end4;
62
63     static
64     {
65         // Statically initialize the start and end times
66
start1 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 3 );
67         end1 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 4 );
68
69         start2 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 1 );
70         end2 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 8 );
71
72         start3 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JUNE, 1 );
73         end3 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 1 );
74
75         start4 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.JULY, 1 );
76         end4 = new GregorianCalendar JavaDoc( 2004, GregorianCalendar.AUGUST, 1 );
77     }
78
79     private static void println( String JavaDoc str ) {
80         System.out.println( str );
81     }
82
83     private static void prepare( String JavaDoc[] args )
84     {
85         demoResources = Util.getJRobinHomeDirectory() + "/res/demo/";
86         rrd1 = demoResources + rrd1;
87         rrd2 = demoResources + rrd2;
88     }
89
90     private static void createGraphs() throws RrdException, IOException JavaDoc
91     {
92         runStart = System.currentTimeMillis();
93
94         // -- First graph is one day in time
95
graphDef.setTimePeriod( start1, end1 );
96         graph.saveAsPNG( graph1 );
97
98         // -- Second is one week in time
99
graphDef.setTimePeriod( start2, end2 );
100         graph.saveAsPNG( graph2 );
101
102         // -- Third is one month in time
103
graphDef.setTimePeriod( start3, end3 );
104         graph.saveAsPNG( graph3 );
105
106         // -- Fourth is again one month in time
107
graphDef.setTimePeriod( start4, end4 );
108         graph.saveAsPNG( graph4 );
109
110         runStop = System.currentTimeMillis();
111         runTimes[ runCount++ ] = (runStop - runStart);
112     }
113
114     public static void main( String JavaDoc[] args ) throws RrdException, IOException JavaDoc
115     {
116         prepare( args );
117
118         println( "+-----------------------------------------------------------------------------------+" );
119         println( "| JRobin LazyDemo |" );
120         println( "| |" );
121         println( "| This demo illustrates the use of RrdOpener, FetchSourceList and the lazy flag of |" );
122         println( "| the RrdGraphDef. |" );
123         println( "| |" );
124         println( "+-----------------------------------------------------------------------------------+" );
125
126         long execStart = System.currentTimeMillis();
127
128         // -- Create the basic RrdGraphDef and RrdGraph
129
graphDef = new RrdGraphDef();
130         graphDef.area( "bitsOutEth0", Color.RED, "Outgoing eth0" );
131         graphDef.stack( "bitsOutEth1", Color.GREEN, "Outgoing eth1" );
132         graphDef.stack( "bitsInEth0", Color.BLUE, "Incoming eth0" );
133         graphDef.stack( "bitsInEth1", Color.YELLOW, "Incoming eth1" );
134         graph = new RrdGraph( graphDef );
135
136         // -- Put the datasources in a FetchSourceList and add it to the graphDef
137
// -- The FetchSourceList also uses our LazyDemoOpener, overriding the default RrdOpener for a graph
138
// -- Lock the RrdOpener, this way it is not overridden by graph generation
139
FetchSourceList fsl = new FetchSourceList( 2 , false, true, new LazyDemoOpener() );
140         fsl.add( "bitsOutEth0", rrd1, "ifOutOctets", "AVERAGE" );
141         fsl.add( "bitsInEth0", rrd1, "ifInOctets", "AVERAGE" );
142         fsl.add( "bitsOutEth1", rrd2, "ifOutOctets", "AVERAGE" );
143         fsl.add( "bitsInEth1", rrd2, "ifInOctets", "AVERAGE" );
144         graphDef.setDatasources( fsl );
145
146         // -- Run the graphs 2 times, second time with lazy, but the FetchSourceList should not be persistent
147
println( "\n>>> GRAPH RUN 1" );
148         graphDef.setLazy( false ); // Disable lazy flag, graphs will be generated
149
createGraphs();
150
151         println( "\n>>> GRAPH RUN 2" );
152         graphDef.setLazy( true ); // Enable lazy flag, graphs will be skipped
153
createGraphs();
154
155         // -- Now run the graphs 2 times again, second time with lazy, but now the FetchSourceList is persistent
156

157         fsl.setPersistent( true ); // Mark the FSL as persistent, RRDs will only be opened once, not closed again
158

159         println( "\n>>> Manually opening all RRD references" );
160         fsl.openAll(); // Manually open all RRDs, this is more obvious than the implicit opening in graph generation
161

162         println( "\n>>> GRAPH RUN 3" );
163         graphDef.setLazy( false ); // Disable lazy flag, graphs will be generated
164
createGraphs();
165
166         println( "\n>>> GRAPH RUN 4" );
167         graphDef.setLazy( true ); // Enable lazy flag, graphs will be skipped
168
createGraphs();
169
170         fsl.setPersistent( false ); // Remove persistence so we can release datasources
171

172         println( "\n>>> Manually releasing all RRD references" );
173         fsl.releaseAll(); // Release the datasources again
174

175         // -- Print out timing results
176
println( "\n>>> Generation time results:" );
177         println( " First run, no lazy, no persistence - normal generation: " + runTimes[0] + " ms" );
178         println( " Second run, lazy, no persistence - generation skipped: " + runTimes[1] + " ms" );
179         println( " Third run, no lazy, persistence - normal generation: " + runTimes[2] + " ms" );
180         println( " Fourth run, lazy, persistence - generation skipped: " + runTimes[3] + " ms" );
181
182     }
183 }
184
Popular Tags