1 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 ; 34 import java.io.IOException ; 35 import java.awt.*; 36 import java.util.GregorianCalendar ; 37 38 44 public class LazyDemo 45 { 46 private static String rrd1 = "eth0.rrd"; 47 private static String rrd2 = "eth1.rrd"; 48 private static String graph1 = Util.getJRobinDemoPath( "lazy-graph1.png" ); 49 private static String graph2 = Util.getJRobinDemoPath( "lazy-graph2.png" ); 50 private static String graph3 = Util.getJRobinDemoPath( "lazy-graph3.png" ); 51 private static String graph4 = Util.getJRobinDemoPath( "lazy-graph4.png" ); 52 private static String 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 start1, end1, start2, end2, start3, end3, start4, end4; 62 63 static 64 { 65 start1 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 3 ); 67 end1 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 4 ); 68 69 start2 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 1 ); 70 end2 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 8 ); 71 72 start3 = new GregorianCalendar ( 2004, GregorianCalendar.JUNE, 1 ); 73 end3 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 1 ); 74 75 start4 = new GregorianCalendar ( 2004, GregorianCalendar.JULY, 1 ); 76 end4 = new GregorianCalendar ( 2004, GregorianCalendar.AUGUST, 1 ); 77 } 78 79 private static void println( String str ) { 80 System.out.println( str ); 81 } 82 83 private static void prepare( String [] 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 91 { 92 runStart = System.currentTimeMillis(); 93 94 graphDef.setTimePeriod( start1, end1 ); 96 graph.saveAsPNG( graph1 ); 97 98 graphDef.setTimePeriod( start2, end2 ); 100 graph.saveAsPNG( graph2 ); 101 102 graphDef.setTimePeriod( start3, end3 ); 104 graph.saveAsPNG( graph3 ); 105 106 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 [] args ) throws RrdException, IOException 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 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 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 println( "\n>>> GRAPH RUN 1" ); 148 graphDef.setLazy( false ); createGraphs(); 150 151 println( "\n>>> GRAPH RUN 2" ); 152 graphDef.setLazy( true ); createGraphs(); 154 155 157 fsl.setPersistent( true ); 159 println( "\n>>> Manually opening all RRD references" ); 160 fsl.openAll(); 162 println( "\n>>> GRAPH RUN 3" ); 163 graphDef.setLazy( false ); createGraphs(); 165 166 println( "\n>>> GRAPH RUN 4" ); 167 graphDef.setLazy( true ); createGraphs(); 169 170 fsl.setPersistent( false ); 172 println( "\n>>> Manually releasing all RRD references" ); 173 fsl.releaseAll(); 175 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 |