001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.pool.performance;
018:
019: import org.apache.commons.pool.impl.GenericObjectPool;
020:
021: /**
022: * Multi-thread performance test
023: *
024: * @author Dirk Verbeeck
025: * @version $Revision: 155430 $ $Date: 2005-02-26 08:13:28 -0500 (Sat, 26 Feb 2005) $
026: */
027: public class PerformanceTest {
028: private int logLevel = 0;
029: private int nrIterations = 5;
030: private int nrThreads = 100;
031:
032: private GenericObjectPool pool;
033: private boolean start = false;
034: private volatile int waiting = 0;
035: private volatile int complete = 0;
036: private volatile long totalBorrowTime = 0;
037: private volatile long totalReturnTime = 0;
038: private volatile int nrSamples = 0;
039:
040: public void setLogLevel(int i) {
041: logLevel = i;
042: }
043:
044: private void init() {
045: start = false;
046: waiting = 0;
047: complete = 0;
048: totalBorrowTime = 0;
049: totalReturnTime = 0;
050: nrSamples = 0;
051: }
052:
053: class MyThread implements Runnable {
054: long borrowTime;
055: long returnTime;
056:
057: public void runOnce() {
058: try {
059: waiting++;
060: if (logLevel >= 5) {
061: String name = "thread"
062: + Thread.currentThread().getName();
063: System.out.println(name + " waiting: " + waiting
064: + " complete: " + complete);
065: }
066: long bbegin = System.currentTimeMillis();
067: Object o = pool.borrowObject();
068: long bend = System.currentTimeMillis();
069: waiting--;
070: do {
071: Thread.yield();
072: } while (!start);
073:
074: if (logLevel >= 3) {
075: String name = "thread"
076: + Thread.currentThread().getName();
077: System.out.println(name + " waiting: " + waiting
078: + " complete: " + complete);
079: }
080:
081: long rbegin = System.currentTimeMillis();
082: pool.returnObject(o);
083: long rend = System.currentTimeMillis();
084: Thread.yield();
085: complete++;
086: borrowTime = (bend - bbegin);
087: returnTime = (rend - rbegin);
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: public void run() {
094: runOnce(); // warmup
095: for (int i = 0; i < nrIterations; i++) {
096: runOnce();
097: totalBorrowTime += borrowTime;
098: totalReturnTime += returnTime;
099: nrSamples++;
100: if (logLevel >= 2) {
101: String name = "thread"
102: + Thread.currentThread().getName();
103: System.out.println("result " + nrSamples + "\t"
104: + name + "\t" + "borrow time: "
105: + borrowTime + "\t" + "return time: "
106: + returnTime + "\t" + "waiting: " + waiting
107: + "\t" + "complete: " + complete);
108: }
109: }
110: }
111: }
112:
113: private void run(int nrIterations, int nrThreads, int maxActive,
114: int maxIdle) {
115: this .nrIterations = nrIterations;
116: this .nrThreads = nrThreads;
117: init();
118:
119: SleepingObjectFactory factory = new SleepingObjectFactory();
120: if (logLevel >= 4) {
121: factory.setDebug(true);
122: }
123: pool = new GenericObjectPool(factory);
124: pool.setMaxActive(maxActive);
125: pool.setMaxIdle(maxIdle);
126: pool.setTestOnBorrow(true);
127:
128: Thread[] threads = new Thread[nrThreads];
129: for (int i = 0; i < threads.length; i++) {
130: threads[i] = new Thread(new MyThread(), Integer.toString(i));
131: Thread.yield();
132: }
133: if (logLevel >= 1) {
134: System.out.println("created");
135: }
136: Thread.yield();
137:
138: for (int i = 0; i < threads.length; i++) {
139: threads[i].start();
140: Thread.yield();
141: }
142: if (logLevel >= 1) {
143: System.out.println("started");
144: }
145: Thread.yield();
146:
147: start = true;
148: if (logLevel >= 1) {
149: System.out.println("go");
150: }
151: Thread.yield();
152:
153: for (int i = 0; i < threads.length; i++) {
154: try {
155: threads[i].join();
156: } catch (InterruptedException e) {
157: e.printStackTrace();
158: }
159: }
160: if (logLevel >= 1) {
161: System.out.println("finish");
162: }
163: System.out.println("-----------------------------------------");
164: System.out.println("nrIterations: " + nrIterations);
165: System.out.println("nrThreads: " + nrThreads);
166: System.out.println("maxActive: " + maxActive);
167: System.out.println("maxIdle: " + maxIdle);
168: System.out.println("nrSamples: " + nrSamples);
169: System.out.println("totalBorrowTime: " + totalBorrowTime);
170: System.out.println("totalReturnTime: " + totalReturnTime);
171: System.out.println("avg BorrowTime: " + totalBorrowTime
172: / nrSamples);
173: System.out.println("avg ReturnTime: " + totalReturnTime
174: / nrSamples);
175: }
176:
177: public static void main(String[] args) {
178: PerformanceTest test = new PerformanceTest();
179: test.setLogLevel(0);
180: System.out.println("Increase threads");
181: test.run(1, 50, 5, 5);
182: test.run(1, 100, 5, 5);
183: test.run(1, 200, 5, 5);
184: test.run(1, 400, 5, 5);
185:
186: System.out.println("Increase threads & poolsize");
187: test.run(1, 50, 5, 5);
188: test.run(1, 100, 10, 10);
189: test.run(1, 200, 20, 20);
190: test.run(1, 400, 40, 40);
191:
192: System.out.println("Increase maxIdle");
193: test.run(1, 400, 40, 5);
194: test.run(1, 400, 40, 40);
195:
196: // System.out.println("Show creation/destruction of objects");
197: // test.setLogLevel(4);
198: // test.run(1, 400, 40, 5);
199: }
200:
201: }
|