KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > logger > LogWriterBenchmark


1 /**
2  * Copyright (C) 2005 manfred andres
3  * Created: 07.03.2005 (16:50:46)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package freecs.util.logger;
20
21
22 /**
23  * @author manfred andres
24  *
25  */

26 public class LogWriterBenchmark implements Runnable JavaDoc {
27     public static int threads=10;
28     public static int msgsPerSecond=10;
29     
30     private final String JavaDoc[] logPaths;
31     private final int id;
32     
33     public LogWriterBenchmark(String JavaDoc[] filePaths, int id) {
34         this.logPaths=filePaths;
35         this.id = id;
36     }
37
38     public static void main(String JavaDoc args[]) {
39         for (int i = 0; i < args.length; i++) {
40             String JavaDoc 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 JavaDoc[] filePaths = new String JavaDoc[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 JavaDoc tg = new ThreadGroup JavaDoc("LogWriterBenchmarkers");
54         tg.setDaemon(true);
55         for (int i = 0; i < threads; i++) {
56             Thread JavaDoc t = new Thread JavaDoc(tg, new LogWriterBenchmark(filePaths, i));
57             t.start();
58         }
59         try {
60             Thread.sleep(60000*60); // run for one hour
61
} catch (InterruptedException JavaDoc 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 JavaDoc path = logPaths[(int) Math.round(Math.random()*(logPaths.length-1))];
72             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc ie) { }
85         }
86     }
87 }
88
Popular Tags