1 package org.jrobin.demo; 2 3 27 28 import org.jrobin.core.*; 29 import org.jrobin.graph.RrdGraphDef; 30 import org.jrobin.graph.RrdGraph; 31 32 import java.io.IOException ; 33 import java.io.BufferedReader ; 34 import java.io.FileReader ; 35 import java.util.Date ; 36 import java.awt.*; 37 38 class StressTest { 39 static final String FACTORY_NAME = "NIO"; 40 41 static final String 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 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 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 message) { 56 System.out.println(message + " " + Util.getLapTime()); 57 } 58 59 public static void main(String [] args) throws RrdException, IOException { 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 ()); 79 RrdDb.setDefaultFactory(FACTORY_NAME); 80 printLapTime("Backend factory set to " + FACTORY_NAME); 81 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 r = new BufferedReader (new FileReader (args[0])); 105 printLapTime("Buffered reader created, processing data"); 106 int count = 0; 107 Date updateStart = new Date (); 108 for(String line; (line = r.readLine()) != null;) { 109 Sample sample = rrd.createSample(); 110 try { 111 sample.setAndUpdate(line); 112 if(++count % 1000 == 0) { 113 Date now = new Date (); 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 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 for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) { 133 String name = RRD_DATASOURCE_NAME + i; 134 gdef.datasource(name, RRD_PATH, name, "AVERAGE"); 135 } 136 for(int i = 0; i < RRD_DATASOURCE_COUNT; i++) { 138 String 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 ()); 148 } 149 } 150 | Popular Tags |