KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > logging > Performance


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.logging;
11
12 import org.mmbase.util.ResourceLoader;
13 import org.apache.log4j.Category;
14 import org.apache.log4j.xml.DOMConfigurator;
15
16 /**
17  * This program is meant to show the performance of the
18  * org.mmbase.util.logging classes. You can also easily check the
19  * performance of a configuration with it. Simply feed it the configuration file, and it will
20  * try to log (info priority) to the class A0123456789.B0123456789.C0123456789.
21  * It has several other command line options (starting with -).
22  *
23  **/

24
25
26 public class Performance {
27
28     static final double SECOND = 1000; // one second in milliseconds.
29

30     static int repeats = 1000;
31     static boolean isdebugenabled = false;
32     static boolean nosystem = false;
33     static boolean log4j = false;
34     static String JavaDoc description = null;
35     static boolean delay = false;
36     static int delaytime = 10;
37     static int burstLen = 3;
38     static int warmingup = 200;
39
40     static double doCaseLog4j(String JavaDoc s) {
41         DOMConfigurator.configure(s);
42         int i;
43         Category cat = Category.getInstance("A0123456789.B0123456789.C0123456789");
44         for (i = 0; i < warmingup; i++) {
45             cat.info("warming up.");
46         }
47         long before = System.currentTimeMillis();
48         for (i = 0; i < repeats; i++) {
49             cat.info("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
50         }
51         long after = System.currentTimeMillis();
52         return (double)1000*(after - before)/ repeats;
53
54     }
55
56     static double doCaseNoSystem() {
57         int i;
58         for (i = 0; i < warmingup; i++) {
59             System.err.println("warming up.");
60         }
61         long before = System.currentTimeMillis();
62         for (i = 0; i < repeats; i++) {
63             System.err.println("INFO abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
64         }
65         long after = System.currentTimeMillis();
66         return (double)1000*(after - before)/ repeats;
67     }
68         
69     static double doCase(Logger log) {
70         if (delay) {
71             return doCaseDelayed(log);
72         }
73         int i;
74         for (i = 0; i < warmingup; i++) {
75             log.info("warming up.");
76         }
77         long before = System.currentTimeMillis();
78         for (i = 0; i < repeats; i++) {
79             log.info("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
80         }
81         long after = System.currentTimeMillis();
82         return (double)1000*(after - before)/ repeats;
83     }
84
85     static double doCaseDelayed(Logger log) {
86         int j = 0;
87         int i;
88         try {
89         // Warming up makes the results better reproducable.
90
for (i = 0; i < warmingup; i++) {
91             log.info("warming up.");
92             if(++j == burstLen) {
93                 j = 0;
94                 Thread.sleep(delaytime);
95             }
96         }
97         long before = System.currentTimeMillis();
98         for (i = 0; i < repeats; i++) {
99             log.info("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
100             if(++j == burstLen) {
101                 j = 0;
102                 Thread.sleep(delaytime);
103             }
104         }
105         long after = System.currentTimeMillis();
106         j = 0;
107         // sleep a little in between.
108
Thread.sleep(3000);
109
110         long before_ref = System.currentTimeMillis();
111         // do the same but without logging (trace will not be logged, and we've seen that it's time is neglectable).
112
for (i = 0; i < repeats; i++) {
113             log.trace("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
114             if(j++ == burstLen) {
115                 j = 0;
116                 Thread.sleep(delaytime);
117             }
118         }
119
120         long after_ref = System.currentTimeMillis();
121
122         //System.out.println("(with: " + (after - before) + " without " + (after_ref - before_ref) + " ) ");
123
return (double)1000*(after - before - (after_ref - before_ref))/ repeats;
124         } catch (Exception JavaDoc e) {}
125         return 0;
126     }
127
128     static double doCaseIfDebug(Logger log) {
129         int i;
130         for (i = 0; i < warmingup; i++) {
131             if (log.isDebugEnabled()) {
132                 log.debug("warming up.");
133             }
134         }
135         long before = System.currentTimeMillis();
136         for (i = 0; i < repeats; i++) {
137             if (log.isDebugEnabled()) {
138                 log.debug("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
139             }
140         }
141         long after = System.currentTimeMillis();
142         return (double)1000*(after - before)/ repeats;
143     }
144
145     static double Case(String JavaDoc configuration) {
146         if(log4j) {
147             return doCaseLog4j(configuration);
148         }
149         if(nosystem) {
150             return doCaseNoSystem();
151         }
152
153         Logging.configure(ResourceLoader.getConfigurationRoot(), configuration);
154         Logger log = Logging.getLoggerInstance("A0123456789.B0123456789.C0123456789");
155
156         if(isdebugenabled) {
157             return doCaseIfDebug(log);
158         } else {
159             return doCase(log);
160         }
161
162     }
163
164     static void printCase(String JavaDoc configuration) {
165         String JavaDoc s = (description == null ? "" : description) + " (" + configuration + ") : ";
166         for (int i = s.length(); i< 50; i++) s += " "; // damn, sprintf would be nice..
167
System.out.print(s);
168
169         double benchmark = Case(configuration);
170         
171         System.out.println(benchmark + " us/logging"); // we follow the example of log4j and report in microseconds (us)
172
}
173
174     public static void main(String JavaDoc[] args) {
175         for(int i = 0; i < args.length; i++) {
176             if(args[i].charAt(0) == '-') { // an command line option
177
if(args[i].substring(1).equals("repeats")){
178                     repeats = Integer.valueOf(args[++i]).intValue();
179                 }
180                 if(args[i].substring(1).equals("isdebugenabled")){
181                     isdebugenabled = ! isdebugenabled;
182                 }
183                 if(args[i].substring(1).equals("nosystem")){
184                     nosystem = ! nosystem;
185                 }
186                 if(args[i].substring(1).equals("log4j")){
187                     log4j = ! log4j;
188                 }
189                 if(args[i].substring(1).equals("desc")){
190                     description = args[++i];
191                 }
192                 if(args[i].substring(1).equals("delay")){
193                     delay = ! delay;
194                 }
195                 if(args[i].substring(1).equals("delaytime")){
196                     delaytime = Integer.valueOf(args[++i]).intValue();
197                 }
198                 if(args[i].substring(1).equals("burstlen")){
199                     burstLen = Integer.valueOf(args[++i]).intValue();
200                 }
201             } else {
202                 warmingup = repeats / 10;
203                 printCase(args[i]);
204             }
205         }
206         Logging.shutdown();
207     }
208 }
209
Popular Tags