| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DBCPClientThread |
|
| 3.75;3.75 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | ||
| 18 | package org.apache.commons.performance.dbcp; | |
| 19 | ||
| 20 | import java.sql.Connection; | |
| 21 | import java.sql.ResultSet; | |
| 22 | import java.sql.Statement; | |
| 23 | import java.util.logging.Logger; | |
| 24 | import javax.sql.DataSource; | |
| 25 | ||
| 26 | import org.apache.commons.performance.ClientThread; | |
| 27 | import org.apache.commons.performance.Statistics; | |
| 28 | ||
| 29 | /** | |
| 30 | * Client thread that executes requests in a loop using a configured | |
| 31 | * DataSource, with the number of requests, time between requests and | |
| 32 | * query strings governed by constructor parameters. See | |
| 33 | * {@link ClientThread ClientThread javadoc} for a description | |
| 34 | * of how times between requests are computed. | |
| 35 | * | |
| 36 | */ | |
| 37 | public class DBCPClientThread extends ClientThread { | |
| 38 | /** Initial segment of query string */ | |
| 39 | 0 | private String queryString = null; |
| 40 | /** Whether or not the query is on the text column */ | |
| 41 | 0 | private boolean textQuery = false; |
| 42 | /** DataSource used to connect */ | |
| 43 | 0 | private DataSource dataSource = null; |
| 44 | /** Database connection */ | |
| 45 | 0 | Connection conn = null; |
| 46 | /** Current query */ | |
| 47 | 0 | String currentQuery = null; |
| 48 | ||
| 49 | /** | |
| 50 | * Create a dbcp client thread. | |
| 51 | * | |
| 52 | * @param iterations number of iterations | |
| 53 | * @param minDelay minimum delay time between client requests | |
| 54 | * @param maxDelay maximum delay time between client requests | |
| 55 | * @param sigma standard deviation of delay times between client requests | |
| 56 | * @param delayType distribution of time between client requests | |
| 57 | * @param queryType type of query | |
| 58 | * @param rampPeriod rampup, rampdown period for cyclic load | |
| 59 | * @param peakPeriod period of sustained peak for cyclic load | |
| 60 | * @param troughPeriod period of sustained minimum load | |
| 61 | * @param cycleType type of cycle for mean delay | |
| 62 | * @param rampType type of ramp (linear or random jumps) | |
| 63 | * @param logger common logger shared by all clients | |
| 64 | * @param dataSource DataSource for connections | |
| 65 | * @param stats Statistics container | |
| 66 | */ | |
| 67 | public DBCPClientThread(long iterations, long minDelay, long maxDelay, | |
| 68 | double sigma, String delayType, String queryType, long rampPeriod, | |
| 69 | long peakPeriod, long troughPeriod, String cycleType, | |
| 70 | String rampType, Logger logger, DataSource dataSource, | |
| 71 | Statistics stats) { | |
| 72 | ||
| 73 | 0 | super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod, |
| 74 | peakPeriod, troughPeriod, cycleType, rampType, logger, | |
| 75 | stats); | |
| 76 | ||
| 77 | 0 | this.dataSource = dataSource; |
| 78 | ||
| 79 | 0 | if (queryType.equals("no-op")) { |
| 80 | 0 | return; |
| 81 | } | |
| 82 | ||
| 83 | 0 | if (queryType.equals("integerIndexed")) { |
| 84 | 0 | queryString = "select * from test_table WHERE indexed="; |
| 85 | 0 | } else if (queryType.equals("integerScan")) { |
| 86 | 0 | queryString = "select * from test_table WHERE not_indexed="; |
| 87 | } else { | |
| 88 | 0 | queryString = "select * from test_table WHERE text='"; |
| 89 | 0 | textQuery = true; |
| 90 | } | |
| 91 | 0 | } |
| 92 | ||
| 93 | /** Generate a random query */ | |
| 94 | public void setUp() throws Exception { | |
| 95 | 0 | if (queryString == null) { |
| 96 | 0 | return; |
| 97 | } | |
| 98 | 0 | if (textQuery) { |
| 99 | 0 | currentQuery = queryString + |
| 100 | randomData.nextHexString(20) + "';"; | |
| 101 | } else { | |
| 102 | 0 | currentQuery = queryString + |
| 103 | randomData.nextInt(0, 100) + ";"; | |
| 104 | } | |
| 105 | 0 | } |
| 106 | ||
| 107 | /** Execute query */ | |
| 108 | public void execute() throws Exception { | |
| 109 | 0 | conn = dataSource.getConnection(); |
| 110 | 0 | if (queryString == null) { |
| 111 | 0 | return; |
| 112 | } | |
| 113 | 0 | Statement stmt = conn.createStatement(); |
| 114 | 0 | stmt.execute(currentQuery); |
| 115 | 0 | ResultSet rs = stmt.getResultSet(); |
| 116 | 0 | if (!rs.isAfterLast()) { |
| 117 | 0 | rs.next(); |
| 118 | } | |
| 119 | 0 | rs.close(); |
| 120 | 0 | stmt.close(); |
| 121 | 0 | } |
| 122 | ||
| 123 | /** Close connection */ | |
| 124 | public void cleanUp() throws Exception { | |
| 125 | 0 | if (conn != null) { |
| 126 | try { | |
| 127 | 0 | conn.close(); |
| 128 | } finally { | |
| 129 | 0 | conn = null; |
| 130 | 0 | } |
| 131 | } | |
| 132 | 0 | } |
| 133 | ||
| 134 | } |