1 19 package freecs.util.logger; 20 21 22 26 public class LogWriterBenchmark implements Runnable { 27 public static int threads=10; 28 public static int msgsPerSecond=10; 29 30 private final String [] logPaths; 31 private final int id; 32 33 public LogWriterBenchmark(String [] filePaths, int id) { 34 this.logPaths=filePaths; 35 this.id = id; 36 } 37 38 public static void main(String args[]) { 39 for (int i = 0; i < args.length; i++) { 40 String curr = args[i]; 41 if (curr.startsWith("-threads=")) 42 threads = Integer.parseInt(curr.substring(9)); 43 else if (curr.startsWith("-messages=")) 44 msgsPerSecond = Integer.parseInt(curr.substring(10)); 45 } 46 String [] filePaths = new String [5]; 47 filePaths[0]="d:/var/log/freecs/freecs1.log"; 48 filePaths[1]="d:/var/log/freecs/freecs2.log"; 49 filePaths[2]="d:/var/log/freecs/freecs3.log"; 50 filePaths[3]="d:/var/log/freecs/freecs4.log"; 51 filePaths[4]="d:/var/log/freecs/freecs5.log"; 52 53 ThreadGroup tg = new ThreadGroup ("LogWriterBenchmarkers"); 54 tg.setDaemon(true); 55 for (int i = 0; i < threads; i++) { 56 Thread t = new Thread (tg, new LogWriterBenchmark(filePaths, i)); 57 t.start(); 58 } 59 try { 60 Thread.sleep(60000*60); } catch (InterruptedException ie) { } 62 LogWriter.instance.stopLogging(); 63 } 64 65 public void run() { 66 long counter = 0; 67 long defaultSleepTime = Math.round(1000/msgsPerSecond); 68 long sleepTime = 0; 69 while (true) { 70 long start = System.currentTimeMillis(); 71 String path = logPaths[(int) Math.round(Math.random()*(logPaths.length-1))]; 72 StringBuffer sb = new StringBuffer (); 73 sb.append (counter); 74 sb.append (". message of the LogWriter started with id "); 75 sb.append (id); 76 sb.append (" having a current sleepTime of "); 77 sb.append (sleepTime); 78 sb.append (" millis"); 79 LogWriter.instance.addLogMessage(path, sb.toString()); 80 counter++; 81 sleepTime = (defaultSleepTime - (System.currentTimeMillis() - start)); 82 if (sleepTime > 0) try { 83 Thread.sleep(sleepTime); 84 } catch (InterruptedException ie) { } 85 } 86 } 87 } 88 | Popular Tags |