KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > perflog > test > BoundedBufferPerformanceLoggerPerformanceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.perflog.test;
19
20
21 import org.sape.carbon.core.component.Lookup;
22 import org.sape.carbon.services.perflog.PerformanceLogger;
23 import org.sape.carbon.services.perflog.ReportingPerformanceLogger;
24
25 import junit.extensions.ActiveTestSuite;
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Template for junit test harness. Change this description to reflect what
35  * this class is testing.
36  *
37  * Copyright 2003 Sapient
38  * @since carbon 1.2
39  * @author Greg Hinkle, January 2003
40  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:31 $)
41  */

42 public class BoundedBufferPerformanceLoggerPerformanceTest extends TestCase {
43
44     /**
45      * Provides a handle to Apache-commons logger
46      */

47     private Log log = LogFactory.getLog(this.getClass());
48
49     private static final String JavaDoc PERF_LOG_SERVICE = "/log/perf/BoundBufferPerformanceLogger";
50
51     private static final int TRACKED_ITEMS = 50;
52
53     private static final int TRACKED_THREADS = 10;
54
55     private static final int PER_THREAD_EXECUTIONS = 1000;
56
57     private static String JavaDoc[] trackings;
58     static {
59         trackings = new String JavaDoc[TRACKED_ITEMS];
60         for (int i = 0; i < TRACKED_ITEMS; i++) {
61             trackings[i] = "Function" + Math.random();
62         }
63     }
64
65     public BoundedBufferPerformanceLoggerPerformanceTest(String JavaDoc name) {
66         super(name);
67     }
68
69
70     public void testSimpleCalls() {
71         for (int i = 0; i < PER_THREAD_EXECUTIONS; i++) {
72             int item = (int) (Math.random() * (double)TRACKED_ITEMS);
73
74             PerformanceLogger logger =
75                 (PerformanceLogger)
76                 Lookup.getInstance().fetchComponent(PERF_LOG_SERVICE);
77
78             logger.start(trackings[item]);
79
80             try {
81                 Thread.sleep((int)(100D * Math.random()));
82             } catch (InterruptedException JavaDoc ie) { }
83
84             logger.end(trackings[item]);
85         }
86     }
87
88     public void testReport() {
89         ReportingPerformanceLogger logger =
90             (ReportingPerformanceLogger)
91             Lookup.getInstance().fetchComponent(PERF_LOG_SERVICE);
92
93         log.info(logger.getReport());
94     }
95
96
97     /**
98      * Method called by jUnit to get all the tests in this test case.
99      * @return Test the suite of tests in this test case
100      */

101     public static Test suite() {
102         TestSuite masterSuite = new TestSuite();
103
104         // add multi threaded tests
105
Test multiThreadedTests = getMultiThreadedTests();
106         if(multiThreadedTests != null) {
107             masterSuite.addTest(multiThreadedTests);
108         }
109
110         // add single threaded tests
111
Test singleThreadedTests = getSingleThreadedTests();
112         if(singleThreadedTests != null) {
113             masterSuite.addTest(singleThreadedTests);
114         }
115
116         return masterSuite;
117     }
118
119     /**
120      * This method is used within the suite method to get all of the single
121      * threaded tests.
122      *
123      * Add all your single threaded tests in this method with a line like:
124      * suite.addTest(new BoundedBufferPerformanceLoggerPerformanceTest("testFunction1"));
125      *
126      * @return Test the suite of single threaded tests in this test case
127      */

128     private static Test getSingleThreadedTests() {
129         TestSuite suite = new TestSuite();
130
131         suite.addTest(new BoundedBufferPerformanceLoggerPerformanceTest("testReport"));
132
133         return suite;
134     }
135
136     /**
137      * This method is used within the suite method to get all of the multi
138      * threaded tests.
139      *
140      * Add all your multi threaded tests in this method with a line like:
141      * addTest(suite, "testFunction1", 5);
142      *
143      * @return Test the suite of multi-threaded tests in this test case
144      */

145     private static Test getMultiThreadedTests() {
146         TestSuite suite = new ActiveTestSuite();
147
148         addTest(suite,"testSimpleCalls",TRACKED_THREADS);
149
150         return suite;
151     }
152
153     /**
154      * This method will add the give test to the give suite the specified
155      * number of times. This is best used for multi-threaded tests where
156      * suite is an instance of ActiveTestSuite and you want to run the same
157      * test in multiple threads.
158      *
159      * @param suite the suite to add the test to.
160      * @param testName the name of the test to add.
161      * @param number the number of times to add the test to the suite
162      */

163     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
164         for(int count=0; count<number; count++) {
165             suite.addTest(new BoundedBufferPerformanceLoggerPerformanceTest(testName));
166         }
167     }
168 }
169
Popular Tags