01: /**
02: * Copyright (C) 2005 manfred andres
03: * Created: 07.03.2005 (16:50:46)
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * as published by the Free Software Foundation; either version 2
08: * of the License, or (at your option) any later version.
09: *
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: */package freecs.util.logger;
19:
20: /**
21: * @author manfred andres
22: *
23: */
24: public class LogWriterBenchmark implements Runnable {
25: public static int threads = 10;
26: public static int msgsPerSecond = 10;
27:
28: private final String[] logPaths;
29: private final int id;
30:
31: public LogWriterBenchmark(String[] filePaths, int id) {
32: this .logPaths = filePaths;
33: this .id = id;
34: }
35:
36: public static void main(String args[]) {
37: for (int i = 0; i < args.length; i++) {
38: String curr = args[i];
39: if (curr.startsWith("-threads="))
40: threads = Integer.parseInt(curr.substring(9));
41: else if (curr.startsWith("-messages="))
42: msgsPerSecond = Integer.parseInt(curr.substring(10));
43: }
44: String[] filePaths = new String[5];
45: filePaths[0] = "d:/var/log/freecs/freecs1.log";
46: filePaths[1] = "d:/var/log/freecs/freecs2.log";
47: filePaths[2] = "d:/var/log/freecs/freecs3.log";
48: filePaths[3] = "d:/var/log/freecs/freecs4.log";
49: filePaths[4] = "d:/var/log/freecs/freecs5.log";
50:
51: ThreadGroup tg = new ThreadGroup("LogWriterBenchmarkers");
52: tg.setDaemon(true);
53: for (int i = 0; i < threads; i++) {
54: Thread t = new Thread(tg, new LogWriterBenchmark(filePaths,
55: i));
56: t.start();
57: }
58: try {
59: Thread.sleep(60000 * 60); // run for one hour
60: } catch (InterruptedException ie) {
61: }
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()
72: * (logPaths.length - 1))];
73: StringBuffer sb = new StringBuffer();
74: sb.append(counter);
75: sb.append(". message of the LogWriter started with id ");
76: sb.append(id);
77: sb.append(" having a current sleepTime of ");
78: sb.append(sleepTime);
79: sb.append(" millis");
80: LogWriter.instance.addLogMessage(path, sb.toString());
81: counter++;
82: sleepTime = (defaultSleepTime - (System.currentTimeMillis() - start));
83: if (sleepTime > 0)
84: try {
85: Thread.sleep(sleepTime);
86: } catch (InterruptedException ie) {
87: }
88: }
89: }
90: }
|