1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.performance.pool;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.logging.Logger;
23
24 import org.apache.commons.pool.ObjectPool;
25 import org.apache.commons.pool.KeyedObjectPool;
26 import org.apache.commons.math.random.RandomData;
27 import org.apache.commons.math.random.RandomDataImpl;
28 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
29 import org.apache.commons.performance.ClientThread;
30 import org.apache.commons.performance.Statistics;
31
32
33
34
35
36
37
38 public class PoolClientThread extends ClientThread {
39
40 private ObjectPool pool;
41 private KeyedObjectPool keyedPool;
42 private boolean keyed;
43 private List<Integer> keys;
44 private RandomData randomData = new RandomDataImpl();
45 private SummaryStatistics numActiveStats = new SummaryStatistics();
46 private SummaryStatistics numIdleStats = new SummaryStatistics();
47 private double samplingRate = 0;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public PoolClientThread(long iterations, long minDelay, long maxDelay,
66 double sigma, String delayType, long rampPeriod, long peakPeriod,
67 long troughPeriod, String cycleType, String rampType, Logger logger,
68 Statistics stats, ObjectPool pool, double samplingRate) {
69
70 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
71 peakPeriod, troughPeriod, cycleType,rampType, logger,
72 stats);
73 this.pool = pool;
74 this.keyed = false;
75 this.samplingRate = samplingRate;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public PoolClientThread(long iterations, long minDelay, long maxDelay,
95 double sigma, String delayType, long rampPeriod, long peakPeriod,
96 long troughPeriod, String cycleType, String rampType, Logger logger,
97 Statistics stats, KeyedObjectPool keyedPool, double samplingRate) {
98
99 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
100 peakPeriod, troughPeriod, cycleType,rampType, logger,
101 stats);
102
103 this.keyedPool = keyedPool;
104 this.keyed = true;
105 this.samplingRate = samplingRate;
106 keys = new ArrayList<Integer>();
107 for (int i = 0; i < 20; i++) {
108 keys.add(new Integer(i));
109 }
110 randomData = new RandomDataImpl();
111 }
112
113
114 public void execute() throws Exception {
115 if (keyed) {
116 Integer key = keys.get(randomData.nextInt(0, 19));
117 Waiter waiter = (Waiter) keyedPool.borrowObject(key);
118 waiter.doWait();
119 keyedPool.returnObject(key, waiter);
120 } else {
121 Waiter waiter = (Waiter) pool.borrowObject();
122 waiter.doWait();
123 pool.returnObject(waiter);
124 }
125 }
126
127 protected void cleanUp() throws Exception {
128
129 if (randomData.nextUniform(0, 1) < samplingRate) {
130 if (keyed) {
131 numIdleStats.addValue(keyedPool.getNumIdle());
132 numActiveStats.addValue(keyedPool.getNumActive());
133 } else {
134 numIdleStats.addValue(pool.getNumIdle());
135 numActiveStats.addValue(pool.getNumActive());
136 }
137 }
138 }
139
140 protected void finish() throws Exception {
141
142 stats.addStatistics(
143 numIdleStats, Thread.currentThread().getName(), "numIdle");
144 stats.addStatistics(
145 numActiveStats, Thread.currentThread().getName(), "numActive");
146 }
147 }