| 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 | 0 | private RandomData randomData = new RandomDataImpl(); |
| 45 | 0 | private SummaryStatistics numActiveStats = new SummaryStatistics(); |
| 46 | 0 | private SummaryStatistics numIdleStats = new SummaryStatistics(); |
| 47 | 0 | 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 | 0 | super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod, |
| 71 | |
peakPeriod, troughPeriod, cycleType,rampType, logger, |
| 72 | |
stats); |
| 73 | 0 | this.pool = pool; |
| 74 | 0 | this.keyed = false; |
| 75 | 0 | this.samplingRate = samplingRate; |
| 76 | 0 | } |
| 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 | 0 | super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod, |
| 100 | |
peakPeriod, troughPeriod, cycleType,rampType, logger, |
| 101 | |
stats); |
| 102 | |
|
| 103 | 0 | this.keyedPool = keyedPool; |
| 104 | 0 | this.keyed = true; |
| 105 | 0 | this.samplingRate = samplingRate; |
| 106 | 0 | keys = new ArrayList<Integer>(); |
| 107 | 0 | for (int i = 0; i < 20; i++) { |
| 108 | 0 | keys.add(new Integer(i)); |
| 109 | |
} |
| 110 | 0 | randomData = new RandomDataImpl(); |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
|
| 114 | |
public void execute() throws Exception { |
| 115 | 0 | if (keyed) { |
| 116 | 0 | Integer key = keys.get(randomData.nextInt(0, 19)); |
| 117 | 0 | Waiter waiter = (Waiter) keyedPool.borrowObject(key); |
| 118 | 0 | waiter.doWait(); |
| 119 | 0 | keyedPool.returnObject(key, waiter); |
| 120 | 0 | } else { |
| 121 | 0 | Waiter waiter = (Waiter) pool.borrowObject(); |
| 122 | 0 | waiter.doWait(); |
| 123 | 0 | pool.returnObject(waiter); |
| 124 | |
} |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
protected void cleanUp() throws Exception { |
| 128 | |
|
| 129 | 0 | if (randomData.nextUniform(0, 1) < samplingRate) { |
| 130 | 0 | if (keyed) { |
| 131 | 0 | numIdleStats.addValue(keyedPool.getNumIdle()); |
| 132 | 0 | numActiveStats.addValue(keyedPool.getNumActive()); |
| 133 | |
} else { |
| 134 | 0 | numIdleStats.addValue(pool.getNumIdle()); |
| 135 | 0 | numActiveStats.addValue(pool.getNumActive()); |
| 136 | |
} |
| 137 | |
} |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
protected void finish() throws Exception { |
| 141 | |
|
| 142 | 0 | stats.addStatistics( |
| 143 | |
numIdleStats, Thread.currentThread().getName(), "numIdle"); |
| 144 | 0 | stats.addStatistics( |
| 145 | |
numActiveStats, Thread.currentThread().getName(), "numActive"); |
| 146 | 0 | } |
| 147 | |
} |