| 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.logging.Logger; |
| 21 | |
import org.apache.commons.dbcp.AbandonedConfig; |
| 22 | |
import org.apache.commons.dbcp.AbandonedObjectPool; |
| 23 | |
import org.apache.commons.pool.impl.GenericObjectPool; |
| 24 | |
import org.apache.commons.pool.impl.StackObjectPool; |
| 25 | |
import org.apache.commons.pool.impl.SoftReferenceObjectPool; |
| 26 | |
import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
| 27 | |
import org.apache.commons.pool.impl.StackKeyedObjectPool; |
| 28 | |
import org.apache.commons.performance.ConfigurationException; |
| 29 | |
import org.apache.commons.performance.ClientThread; |
| 30 | |
import org.apache.commons.performance.LoadGenerator; |
| 31 | |
import org.apache.commons.performance.Statistics; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class PoolSoak extends LoadGenerator { |
| 40 | |
|
| 41 | |
|
| 42 | |
private GenericObjectPool genericObjectPool; |
| 43 | |
private GenericKeyedObjectPool genericKeyedObjectPool; |
| 44 | |
private StackObjectPool stackObjectPool; |
| 45 | |
private SoftReferenceObjectPool softReferenceObjectPool; |
| 46 | |
private StackKeyedObjectPool stackKeyedObjectPool; |
| 47 | |
|
| 48 | |
|
| 49 | |
private String poolType; |
| 50 | |
private int maxActive; |
| 51 | |
private int maxActivePerKey; |
| 52 | |
private int maxIdle; |
| 53 | |
private int minIdle; |
| 54 | |
private long maxWait; |
| 55 | |
private byte exhaustedAction; |
| 56 | |
private boolean testOnBorrow; |
| 57 | |
private boolean testOnReturn; |
| 58 | |
private long timeBetweenEvictions; |
| 59 | |
private int testsPerEviction; |
| 60 | |
private long idleTimeout; |
| 61 | |
private boolean testWhileIdle; |
| 62 | 0 | private AbandonedConfig abandonedConfig = new AbandonedConfig(); |
| 63 | |
private boolean lifo; |
| 64 | |
private double samplingRate; |
| 65 | |
|
| 66 | |
|
| 67 | |
private long activateLatency; |
| 68 | |
private long destroyLatency; |
| 69 | |
private long makeLatency; |
| 70 | |
private long passivateLatency; |
| 71 | |
private long validateLatency; |
| 72 | |
private long waiterLatency; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
protected void configure() throws Exception { |
| 79 | 0 | super.configure(); |
| 80 | 0 | digester.addCallMethod("configuration/factory", |
| 81 | |
"configureFactory", 6); |
| 82 | 0 | digester.addCallParam( |
| 83 | |
"configuration/factory/activate-latency", 0); |
| 84 | 0 | digester.addCallParam( |
| 85 | |
"configuration/factory/destroy-latency", 1); |
| 86 | 0 | digester.addCallParam( |
| 87 | |
"configuration/factory/make-latency", 2); |
| 88 | 0 | digester.addCallParam( |
| 89 | |
"configuration/factory/passivate-latency", 3); |
| 90 | 0 | digester.addCallParam( |
| 91 | |
"configuration/factory/validate-latency", 4); |
| 92 | 0 | digester.addCallParam( |
| 93 | |
"configuration/factory/waiter-latency", 5); |
| 94 | 0 | digester.addCallMethod("configuration/pool", |
| 95 | |
"configurePool", 15); |
| 96 | 0 | digester.addCallParam( |
| 97 | |
"configuration/pool/max-active", 0); |
| 98 | 0 | digester.addCallParam( |
| 99 | |
"configuration/pool/max-active-per-key", 1); |
| 100 | 0 | digester.addCallParam( |
| 101 | |
"configuration/pool/max-idle", 2); |
| 102 | 0 | digester.addCallParam( |
| 103 | |
"configuration/pool/min-idle", 3); |
| 104 | 0 | digester.addCallParam( |
| 105 | |
"configuration/pool/max-wait", 4); |
| 106 | 0 | digester.addCallParam( |
| 107 | |
"configuration/pool/exhausted-action", 5); |
| 108 | 0 | digester.addCallParam( |
| 109 | |
"configuration/pool/test-on-borrow", 6); |
| 110 | 0 | digester.addCallParam( |
| 111 | |
"configuration/pool/test-on-return", 7); |
| 112 | 0 | digester.addCallParam( |
| 113 | |
"configuration/pool/time-between-evictions", 8); |
| 114 | 0 | digester.addCallParam( |
| 115 | |
"configuration/pool/tests-per-eviction", 9); |
| 116 | 0 | digester.addCallParam( |
| 117 | |
"configuration/pool/idle-timeout", 10); |
| 118 | 0 | digester.addCallParam( |
| 119 | |
"configuration/pool/test-while-idle", 11); |
| 120 | 0 | digester.addCallParam( |
| 121 | |
"configuration/pool/lifo", 12); |
| 122 | 0 | digester.addCallParam( |
| 123 | |
"configuration/pool/type", 13); |
| 124 | 0 | digester.addCallParam( |
| 125 | |
"configuration/pool/sampling-rate", 14); |
| 126 | 0 | digester.addCallMethod("configuration/abandoned-config", |
| 127 | |
"configureAbandonedConfig", 3); |
| 128 | 0 | digester.addCallParam( |
| 129 | |
"configuration/abandoned-config/log-abandoned", 0); |
| 130 | 0 | digester.addCallParam( |
| 131 | |
"configuration/abandoned-config/remove-abandoned", 1); |
| 132 | 0 | digester.addCallParam( |
| 133 | |
"configuration/abandoned-config/abandoned-timeout", 2); |
| 134 | |
|
| 135 | 0 | this.configFile = "config-pool.xml"; |
| 136 | |
|
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
protected void init() throws Exception { |
| 143 | |
|
| 144 | 0 | WaiterFactory factory = new WaiterFactory(activateLatency, destroyLatency, |
| 145 | |
makeLatency, passivateLatency, validateLatency, waiterLatency, |
| 146 | |
maxActive, maxActivePerKey); |
| 147 | |
|
| 148 | |
|
| 149 | 0 | if (poolType.equals("GenericObjectPool")) { |
| 150 | 0 | genericObjectPool = new GenericObjectPool(factory); |
| 151 | 0 | genericObjectPool.setMaxActive(maxActive); |
| 152 | 0 | genericObjectPool.setWhenExhaustedAction(exhaustedAction); |
| 153 | 0 | genericObjectPool.setMaxWait(maxWait); |
| 154 | 0 | genericObjectPool.setMaxIdle(maxIdle); |
| 155 | 0 | genericObjectPool.setMinIdle(minIdle); |
| 156 | 0 | genericObjectPool.setTestOnBorrow(testOnBorrow); |
| 157 | 0 | genericObjectPool.setTestOnReturn(testOnReturn); |
| 158 | 0 | genericObjectPool.setTimeBetweenEvictionRunsMillis( |
| 159 | |
timeBetweenEvictions); |
| 160 | 0 | genericObjectPool.setNumTestsPerEvictionRun(testsPerEviction); |
| 161 | 0 | genericObjectPool.setMinEvictableIdleTimeMillis(idleTimeout); |
| 162 | 0 | genericObjectPool.setTestWhileIdle(testWhileIdle); |
| 163 | |
|
| 164 | 0 | } else if (poolType.equals("AbandonedObjectPool")) { |
| 165 | 0 | genericObjectPool = new AbandonedObjectPool(null,abandonedConfig); |
| 166 | 0 | genericObjectPool.setMaxActive(maxActive); |
| 167 | 0 | genericObjectPool.setWhenExhaustedAction(exhaustedAction); |
| 168 | 0 | genericObjectPool.setMaxWait(maxWait); |
| 169 | 0 | genericObjectPool.setMaxIdle(maxIdle); |
| 170 | 0 | genericObjectPool.setMinIdle(minIdle); |
| 171 | 0 | genericObjectPool.setTestOnBorrow(testOnBorrow); |
| 172 | 0 | genericObjectPool.setTestOnReturn(testOnReturn); |
| 173 | 0 | genericObjectPool.setTimeBetweenEvictionRunsMillis( |
| 174 | |
timeBetweenEvictions); |
| 175 | 0 | genericObjectPool.setNumTestsPerEvictionRun(testsPerEviction); |
| 176 | 0 | genericObjectPool.setMinEvictableIdleTimeMillis(idleTimeout); |
| 177 | 0 | genericObjectPool.setTestWhileIdle(testWhileIdle); |
| 178 | |
|
| 179 | 0 | genericObjectPool.setFactory(factory); |
| 180 | 0 | } else if (poolType.equals("GenericKeyedObjectPool")) { |
| 181 | 0 | genericKeyedObjectPool = new GenericKeyedObjectPool(); |
| 182 | 0 | genericKeyedObjectPool.setMaxActive(maxActivePerKey); |
| 183 | 0 | genericKeyedObjectPool.setMaxTotal(maxActive); |
| 184 | 0 | genericKeyedObjectPool.setWhenExhaustedAction(exhaustedAction); |
| 185 | 0 | genericKeyedObjectPool.setMaxWait(maxWait); |
| 186 | 0 | genericKeyedObjectPool.setMaxIdle(maxIdle); |
| 187 | 0 | genericKeyedObjectPool.setMinIdle(minIdle); |
| 188 | 0 | genericKeyedObjectPool.setTestOnBorrow(testOnBorrow); |
| 189 | 0 | genericKeyedObjectPool.setTestOnReturn(testOnReturn); |
| 190 | 0 | genericKeyedObjectPool.setTimeBetweenEvictionRunsMillis( |
| 191 | |
timeBetweenEvictions); |
| 192 | 0 | genericKeyedObjectPool.setNumTestsPerEvictionRun(testsPerEviction); |
| 193 | 0 | genericKeyedObjectPool.setMinEvictableIdleTimeMillis(idleTimeout); |
| 194 | 0 | genericKeyedObjectPool.setTestWhileIdle(testWhileIdle); |
| 195 | |
|
| 196 | 0 | genericKeyedObjectPool.setFactory(factory); |
| 197 | 0 | } else if (poolType.equals("StackObjectPool")) { |
| 198 | 0 | stackObjectPool = new StackObjectPool(); |
| 199 | 0 | stackObjectPool.setFactory(factory); |
| 200 | 0 | } else if (poolType.equals("SoftReferenceObjectPool")) { |
| 201 | 0 | softReferenceObjectPool = new SoftReferenceObjectPool(); |
| 202 | 0 | softReferenceObjectPool.setFactory(factory); |
| 203 | 0 | } else if (poolType.equals("StackKeyedObjectPool")) { |
| 204 | 0 | stackKeyedObjectPool = new StackKeyedObjectPool(); |
| 205 | 0 | stackKeyedObjectPool.setFactory(factory); |
| 206 | |
} else { |
| 207 | 0 | throw new ConfigurationException( |
| 208 | |
"invalid pool type configuration: " + poolType); |
| 209 | |
} |
| 210 | |
|
| 211 | 0 | logger.info("Initialized pool with properties: "); |
| 212 | 0 | logger.info(" poolTypeT: " + poolType); |
| 213 | 0 | logger.info(" exhaustedAction: " + exhaustedAction); |
| 214 | 0 | logger.info(" maxActive: " + maxActive); |
| 215 | 0 | logger.info(" maxActivePerKey: " + maxActivePerKey); |
| 216 | 0 | logger.info(" maxIdle: " + maxIdle); |
| 217 | 0 | logger.info(" minIdle: " + minIdle); |
| 218 | 0 | logger.info(" testOnBorrow: " + testOnBorrow); |
| 219 | 0 | logger.info(" testWhileIdle: " + testWhileIdle); |
| 220 | 0 | logger.info(" timeBetweenEvictions: " + timeBetweenEvictions); |
| 221 | 0 | logger.info(" testsPerEviction: " + testsPerEviction); |
| 222 | 0 | logger.info(" idleTimeout: " + idleTimeout); |
| 223 | 0 | logger.info(" lifo: " + lifo); |
| 224 | 0 | logger.info(" abandonedConfig: "); |
| 225 | 0 | logger.info(" logAbandoned: " + |
| 226 | |
abandonedConfig.getLogAbandoned()); |
| 227 | 0 | logger.info(" removeAbandoned: " + |
| 228 | |
abandonedConfig.getRemoveAbandoned()); |
| 229 | 0 | logger.info(" abandonedTimeout: " + |
| 230 | |
abandonedConfig.getRemoveAbandonedTimeout()); |
| 231 | 0 | } |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
protected void cleanUp() throws Exception { |
| 237 | 0 | if (genericObjectPool != null) { |
| 238 | 0 | genericObjectPool.close(); |
| 239 | |
} |
| 240 | 0 | if (genericKeyedObjectPool != null) { |
| 241 | 0 | genericKeyedObjectPool.close(); |
| 242 | |
} |
| 243 | 0 | if (stackObjectPool != null) { |
| 244 | 0 | stackObjectPool.close(); |
| 245 | |
} |
| 246 | 0 | if (softReferenceObjectPool != null) { |
| 247 | 0 | softReferenceObjectPool.close(); |
| 248 | |
} |
| 249 | 0 | if (stackKeyedObjectPool != null) { |
| 250 | 0 | stackKeyedObjectPool.close(); |
| 251 | |
} |
| 252 | 0 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
protected ClientThread makeClientThread(long iterations, long minDelay, |
| 258 | |
long maxDelay, double sigma, String delayType, long rampPeriod, |
| 259 | |
long peakPeriod, long troughPeriod, String cycleType, |
| 260 | |
String rampType, Logger logger, Statistics stats) { |
| 261 | 0 | if (poolType.equals("GenericObjectPool")) { |
| 262 | 0 | return new PoolClientThread(iterations, minDelay, maxDelay, |
| 263 | |
sigma, delayType, rampPeriod, peakPeriod, troughPeriod, |
| 264 | |
cycleType, rampType, logger, stats, genericObjectPool, |
| 265 | |
samplingRate); |
| 266 | |
} |
| 267 | 0 | if (poolType.equals("GenericKeyedObjectPool")) { |
| 268 | 0 | return new PoolClientThread(iterations, minDelay, maxDelay, |
| 269 | |
sigma, delayType, rampPeriod, peakPeriod, troughPeriod, |
| 270 | |
cycleType, rampType, logger, stats, |
| 271 | |
genericKeyedObjectPool, samplingRate); |
| 272 | |
} |
| 273 | 0 | if (poolType.equals("StackKeyedObjectPool")) { |
| 274 | 0 | return new PoolClientThread(iterations, minDelay, maxDelay, |
| 275 | |
sigma, delayType, rampPeriod, peakPeriod, troughPeriod, |
| 276 | |
cycleType, rampType, logger, stats, |
| 277 | |
stackKeyedObjectPool, samplingRate); |
| 278 | |
} |
| 279 | 0 | if (poolType.equals("StackObjectPool")) { |
| 280 | 0 | return new PoolClientThread(iterations, minDelay, maxDelay, |
| 281 | |
sigma, delayType, rampPeriod, peakPeriod, troughPeriod, |
| 282 | |
cycleType, rampType, logger, stats, |
| 283 | |
stackObjectPool, samplingRate); |
| 284 | |
} |
| 285 | 0 | if (poolType.equals("SoftReferenceObjectPool")) { |
| 286 | 0 | return new PoolClientThread(iterations, minDelay, maxDelay, |
| 287 | |
sigma, delayType, rampPeriod, peakPeriod, troughPeriod, |
| 288 | |
cycleType, rampType, logger, stats, |
| 289 | |
softReferenceObjectPool, samplingRate); |
| 290 | |
} |
| 291 | 0 | return null; |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
public void configureFactory(String activateLatency, String destroyLatency, |
| 299 | |
String makeLatency, String passivateLatency, String validateLatency, |
| 300 | |
String waiterLatency) { |
| 301 | |
|
| 302 | 0 | this.activateLatency = Long.parseLong(activateLatency); |
| 303 | 0 | this.destroyLatency = Long.parseLong(destroyLatency); |
| 304 | 0 | this.makeLatency = Long.parseLong(makeLatency); |
| 305 | 0 | this.passivateLatency = Long.parseLong(passivateLatency); |
| 306 | 0 | this.validateLatency = Long.parseLong(validateLatency); |
| 307 | 0 | this.waiterLatency = Long.parseLong(waiterLatency); |
| 308 | 0 | } |
| 309 | |
|
| 310 | |
public void configurePool(String maxActive, String maxActivePerKey, |
| 311 | |
String maxIdle, String minIdle, String maxWait, |
| 312 | |
String exhaustedAction, String testOnBorrow, |
| 313 | |
String testOnReturn, String timeBetweenEvictions, |
| 314 | |
String testsPerEviction, String idleTimeout, |
| 315 | |
String testWhileIdle, String lifo, String type, String samplingRate) |
| 316 | |
throws ConfigurationException { |
| 317 | 0 | this.maxActive = Integer.parseInt(maxActive); |
| 318 | 0 | this.maxActivePerKey = Integer.parseInt(maxActivePerKey); |
| 319 | 0 | this.maxIdle = Integer.parseInt(maxIdle); |
| 320 | 0 | this.minIdle = Integer.parseInt(minIdle); |
| 321 | 0 | this.maxWait = Long.parseLong(maxWait); |
| 322 | 0 | this.testOnBorrow = Boolean.parseBoolean(testOnBorrow); |
| 323 | 0 | this.testOnReturn = Boolean.parseBoolean(testOnReturn); |
| 324 | 0 | this.timeBetweenEvictions = Long.parseLong(timeBetweenEvictions); |
| 325 | 0 | this.testsPerEviction = Integer.parseInt(testsPerEviction); |
| 326 | 0 | this.idleTimeout = Long.parseLong(idleTimeout); |
| 327 | 0 | this.testWhileIdle = Boolean.parseBoolean(testWhileIdle); |
| 328 | 0 | this.lifo = Boolean.parseBoolean(lifo); |
| 329 | 0 | this.poolType = type; |
| 330 | 0 | if (exhaustedAction.equals("block")) { |
| 331 | 0 | this.exhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; |
| 332 | 0 | } else if (exhaustedAction.equals("fail")) { |
| 333 | 0 | this.exhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; |
| 334 | 0 | } else if (exhaustedAction.equals("grow")) { |
| 335 | 0 | this.exhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; |
| 336 | |
} else { |
| 337 | 0 | throw new ConfigurationException( |
| 338 | |
"Bad configuration setting for exhausted action: " |
| 339 | |
+ exhaustedAction); |
| 340 | |
} |
| 341 | 0 | this.samplingRate = Double.parseDouble(samplingRate); |
| 342 | 0 | } |
| 343 | |
|
| 344 | |
public void configureAbandonedConfig(String logAbandoned, |
| 345 | |
String removeAbandoned, String abandonedTimeout) { |
| 346 | 0 | abandonedConfig.setLogAbandoned(Boolean.parseBoolean(logAbandoned)); |
| 347 | 0 | abandonedConfig.setRemoveAbandoned( |
| 348 | |
Boolean.parseBoolean(removeAbandoned)); |
| 349 | 0 | abandonedConfig.setRemoveAbandonedTimeout( |
| 350 | |
Integer.parseInt(abandonedTimeout)); |
| 351 | 0 | } |
| 352 | |
|
| 353 | |
|
| 354 | |
protected GenericObjectPool getGenericObjectPool() { |
| 355 | 0 | return genericObjectPool; |
| 356 | |
} |
| 357 | |
|
| 358 | |
protected GenericKeyedObjectPool getGenericKeyedObjectPool() { |
| 359 | 0 | return genericKeyedObjectPool; |
| 360 | |
} |
| 361 | |
|
| 362 | |
} |