KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > PerformanceTest


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.text.*;
55 import java.util.*;
56
57 import org.jdom.*;
58 import org.jdom.input.*;
59 import com.protomatter.syslog.xml.*;
60
61 /**
62  * A performance testing rig for Syslog. Run this program with no
63  * arguments for usage information and examples.
64  */

65 public class PerformanceTest
66 {
67   static Syslogger logger = null;
68   static LogPolicy policy = null;
69   static SyslogTextFormatter format = null;
70   static SyslogMessage message = new SyslogMessage();
71
72   static int POLICY_TEST = 0;
73   static int FORMAT_TEST = 1;
74   static int DIRECT_TEST = 2;
75   static int SYSLOG_TEST = 3;
76
77   static int runs[] = { 0, 0, 0, 0 };
78   static boolean tests[] = { false, false, false, false };
79   static String JavaDoc testNames[] =
80     { "LogPolicy.shouldLog()", "Format.formatLogEntry()",
81       "Direct Logger.log()", "Syslog.log()" };
82
83   public static void main(String JavaDoc args[])
84   {
85     if (args.length != 2)
86     {
87       System.out.println("");
88       System.out.println("Usage: PerformanceTest syslog-config.xml test-config.xml");
89       System.out.println("");
90       System.out.println("Example test-config.xml file:");
91       System.out.println("-----------------------------------------------------------------");
92       System.out.println("");
93       System.out.println("<PerformanceTest>");
94       System.out.println("");
95       System.out.println(" <Message>");
96       System.out.println(" <loggerClassname>com.protomatter.syslog.PerformanceTest</loggerClassname>");
97       System.out.println(" <level>ERROR</level>");
98       System.out.println(" <channel>DEFAULT_CHANNEL</channel>");
99       System.out.println(" <message>This is the short message text, it's right here.</message>");
100       System.out.println(" <detail></detail>");
101       System.out.println(" </Message>");
102       System.out.println("");
103       System.out.println(" <PolicyTest>true</PolicyTest>");
104       System.out.println(" <FormatTest>true</FormatTest>");
105       System.out.println(" <DirectTest>true</DirectTest>");
106       System.out.println(" <SyslogTest>true</SyslogTest>");
107       System.out.println("");
108       System.out.println(" <NumThreads>5</NumThreads>");
109       System.out.println("");
110       System.out.println(" <PolicyIterations>10000000</PolicyIterations>");
111       System.out.println(" <FormatIterations>1000000</FormatIterations>");
112       System.out.println(" <LogIterations>100000</LogIterations>");
113       System.out.println("");
114       System.out.println("</PerformanceTest>");
115       System.out.println("");
116       System.out.println("-----------------------------------------------------------------");
117       System.out.println("");
118       System.exit(0);
119     }
120     try
121     {
122       DecimalFormat msFormat = new DecimalFormat("###,###,###,###,##0");
123       DecimalFormat timeFormat = new DecimalFormat("###,###,###,###,##0.0");
124       DecimalFormat psFormat = new DecimalFormat("###,###,###,###,##0.0");
125       DecimalFormat idFormat = new DecimalFormat("00");
126
127       System.out.println("Syslog performance test utility");
128       System.out.println("");
129       System.out.println("JVM Information:");
130       System.out.println(" VM Name: " + System.getProperty("java.vm.name"));
131       System.out.println(" VM Version: " + System.getProperty("java.vm.version"));
132       System.out.println(" Runtime name: " + System.getProperty("java.runtime.name"));
133       System.out.println(" Runtime version: " + System.getProperty("java.runtime.version"));
134       System.out.println("");
135       System.out.println("OS Information:");
136       System.out.println(" " + System.getProperty("os.name") + " " + System.getProperty("os.version"));
137       System.out.println("");
138       System.out.println("Configuring Syslog from \"" + args[0] + "\"");
139       long time = System.currentTimeMillis();
140       SyslogXML.configure(new File(args[0]));
141       time = System.currentTimeMillis() - time;
142       System.out.println("Configuration completed in " + msFormat.format(time) + " ms");
143
144       System.out.println("");
145       System.out.println("Configuring test from \"" + args[1] + "\"");
146       time = System.currentTimeMillis();
147       Document d = (new SAXBuilder(false)).build(new File(args[1]));
148       Element root = d.getRootElement();
149
150       runs[POLICY_TEST] = Integer.parseInt(root.getChild("PolicyIterations").getText());
151       runs[FORMAT_TEST] = Integer.parseInt(root.getChild("FormatIterations").getText());
152       runs[DIRECT_TEST] = Integer.parseInt(root.getChild("LogIterations").getText());
153       runs[SYSLOG_TEST] = runs[DIRECT_TEST];
154
155       int numThreads = Integer.parseInt(root.getChild("NumThreads").getText());
156
157       Element mElement = root.getChild("Message");
158       String JavaDoc tmp = mElement.getChild("level").getText();
159       if (tmp.equalsIgnoreCase("debug"))
160         message.level = Syslog.DEBUG;
161       else if (tmp.equalsIgnoreCase("info"))
162         message.level = Syslog.INFO;
163       else if (tmp.equalsIgnoreCase("warning"))
164         message.level = Syslog.WARNING;
165       else if (tmp.equalsIgnoreCase("error"))
166         message.level = Syslog.ERROR;
167       else if (tmp.equalsIgnoreCase("fatal"))
168         message.level = Syslog.FATAL;
169
170       message.logger = PerformanceTest.class;
171       message.loggerClassname = mElement.getChild("loggerClassname").getText();
172       message.channel = mElement.getChild("channel").getText();
173       message.msg = mElement.getChild("message").getText();
174       message.detail = mElement.getChild("detail").getText();
175       if (message.detail != null && message.detail.toString().equals(""))
176         message.detail = null;
177       time = System.currentTimeMillis() - time;
178
179       boolean theTests[] = { false, false, false, false };
180       theTests[POLICY_TEST] = "true".equalsIgnoreCase(root.getChild("PolicyTest").getText());
181       theTests[FORMAT_TEST] = "true".equalsIgnoreCase(root.getChild("FormatTest").getText());
182       theTests[DIRECT_TEST] = "true".equalsIgnoreCase(root.getChild("DirectTest").getText());
183       theTests[SYSLOG_TEST] = "true".equalsIgnoreCase(root.getChild("SyslogTest").getText());
184
185       System.out.println("Configuration completed in " + msFormat.format(time) + " ms");
186
187       System.out.println("");
188       System.out.println("Policy/Format/Direct test message:");
189       System.out.println(" logger: " + message.loggerClassname);
190       System.out.println(" channel: \"" + message.channel + "\"");
191       System.out.println(" level: " + mElement.getChild("level").getText().toUpperCase());
192       System.out.println(" message: \"" + message.msg + "\"");
193       System.out.println(" detail: \"" + message.detail + "\"");
194       System.out.println("");
195       System.out.println("Number of test threads: " + numThreads);
196       System.out.println("");
197
198       logger = (Syslogger)Syslog.getLoggers().next();
199       policy = logger.getPolicy();
200       format = logger.getTextFormatter();
201
202       TestThread threads[] = new TestThread[numThreads];
203
204       for (int i=0; i<tests.length; i++)
205       {
206         if (theTests[i])
207         {
208           for(int j=tests.length; --j>=0;)
209             tests[j] = false;
210           tests[i] = true;
211
212           System.out.println("Running " + testNames[i]
213             + " test (" + msFormat.format(runs[i]) + " iterations per thread):");
214           for (int j=0; j<threads.length; j++)
215             threads[j] = new TestThread();
216
217           time = System.currentTimeMillis();
218           for(int j=threads.length; --j>=0;)
219             threads[j].start();
220           for(int j=threads.length; --j>=0;)
221             threads[j].join();
222           time = System.currentTimeMillis() - time;
223           System.out.println("Test complete in " + timeFormat.format((double)time/(double)1000) + " s");
224
225           System.out.println("");
226           System.out.println("Timings per thread:");
227           System.out.println("");
228           System.out.println(" Note: \"ms\" denotes milli-seconds (1/1,000 sec)");
229           System.out.println(" \"us\" denotes micro-seconds (1/1,000,000 sec)");
230           System.out.println("");
231
232           for (int j=0; j<threads.length; j++)
233           {
234             long total = threads[j].total;
235             double uAverage = ((double)total*1000)/((double)(runs[i]));
236             double perSecond = ((double)1000000)/(uAverage);
237
238             System.out.println(" " + idFormat.format(j) + ":");
239             System.out.println(" Total time: " + msFormat.format(total) + " ms");
240             System.out.println(" Each call: " + timeFormat.format(uAverage) + " us");
241             System.out.println(" Per second: " + psFormat.format(perSecond));
242           }
243           double uAverage = ((double)time*1000)/((double)(runs[i]*numThreads));
244           double perSecond = ((double)1000000)/(uAverage);
245           System.out.println("");
246           System.out.println(" Aggregate timings:");
247           System.out.println(" Total time: " + msFormat.format(time) + " ms");
248           System.out.println(" Average time: " + timeFormat.format(uAverage) + " us");
249           System.out.println(" Per second: " + psFormat.format(perSecond));
250           System.out.println("");
251           System.out.println("");
252         }
253       }
254
255       System.out.println("Syslog.shutdown()");
256       Syslog.shutdown();
257       System.out.println(" done.");
258     }
259     catch (Exception JavaDoc x)
260     {
261       x.printStackTrace();
262     }
263   }
264
265   static class TestThread
266   extends Thread JavaDoc
267   {
268     double uAverage = 0;
269     long total = 0;
270
271     public TestThread()
272     {
273       super();
274       setDaemon(true);
275     }
276
277     public void run()
278     {
279       long time = 0;
280       if (tests[POLICY_TEST])
281       {
282         boolean retval = false;
283         time = System.currentTimeMillis();
284         for (int i=runs[POLICY_TEST]; --i>=0;)
285         {
286           retval = policy.shouldLog(message);
287         }
288         total = System.currentTimeMillis() - time;
289         uAverage = ((double)total*1000)/((double)(runs[POLICY_TEST]));
290       }
291
292       if (tests[FORMAT_TEST])
293       {
294         time = System.currentTimeMillis();
295         for (int i=runs[FORMAT_TEST]; --i>=0;)
296         {
297           StringBuffer JavaDoc b = new StringBuffer JavaDoc(128);
298           format.formatLogEntry(b, message);
299         }
300         total = System.currentTimeMillis() - time;
301         uAverage = ((double)total*1000)/((double)(runs[FORMAT_TEST]));
302       }
303
304       if (tests[DIRECT_TEST])
305       {
306         time = System.currentTimeMillis();
307         for (int i=runs[DIRECT_TEST]; --i>=0;)
308         {
309           logger.log(message);
310         }
311         total = System.currentTimeMillis() - time;
312         uAverage = ((double)total*1000)/((double)(runs[DIRECT_TEST]));
313       }
314
315       if (tests[SYSLOG_TEST])
316       {
317         time = System.currentTimeMillis();
318         for (int i=runs[SYSLOG_TEST]; --i>=0;)
319         {
320           Syslog.log(PerformanceTest.class, message.msg, message.detail, message.level);
321         }
322         total = System.currentTimeMillis() - time;
323         uAverage = ((double)total*1000)/((double)(runs[SYSLOG_TEST]));
324       }
325     }
326   }
327 }
328
Popular Tags