File size: 7,380 Bytes
2795186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



 */
package testsuite.perf;

import testsuite.BaseTestCase;

/**
 * Simplistic test for performance regression.
 * 
 * @author Mark Matthews
 */
public class RetrievalPerfTest extends BaseTestCase {
	// ~ Static fields/initializers
	// ---------------------------------------------

	private static final int NUM_TESTS = 10000;

	private static final int NUM_ROWS = 80;

	// ~ Constructors
	// -----------------------------------------------------------

	/**
	 * Constructor for RetrievalPerfTest.
	 * 
	 * @param name
	 *            name of the test to run
	 */
	public RetrievalPerfTest(String name) {
		super(name);
	}

	// ~ Methods
	// ----------------------------------------------------------------

	/**
	 * Runs all tests.
	 * 
	 * @param args
	 *            ignored
	 */
	public static void main(String[] args) {
		new RetrievalPerfTest("testRetrievalMyIsam").run();
		new RetrievalPerfTest("testRetrievalHeap").run();
		new RetrievalPerfTest("testRetrievalCached").run();
	}

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	public void setUp() throws Exception {
		super.setUp();
		createTable("retrievalPerfTestHeap",  "(priKey INT NOT NULL PRIMARY KEY,"
						+ "charField VARCHAR(80)) ", "HEAP");
		createTable("retrievalPerfTestMyIsam", "(priKey INT NOT NULL PRIMARY KEY,"
				+ "charField VARCHAR(80)) ", "MyISAM");

		for (int i = 0; i < NUM_ROWS; i++) {
			this.stmt
					.executeUpdate("INSERT INTO retrievalPerfTestHeap (priKey, charField) VALUES ("
							+ i
							+ ",'abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')");
			this.stmt
					.executeUpdate("INSERT INTO retrievalPerfTestMyIsam (priKey, charField) VALUES ("
							+ i
							+ ",'abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')");
		}
	}

	/**
	 * Tests retrieval from the query cache
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	public void testRetrievalCached() throws Exception {
		this.stmt.executeUpdate("SET QUERY_CACHE_TYPE = DEMAND");

		double fullBegin = System.currentTimeMillis();
		double averageQueryTimeMs = 0;
		double averageTraversalTimeMs = 0;

		for (int i = 0; i < NUM_TESTS; i++) {
			long queryBegin = System.currentTimeMillis();
			this.rs = this.stmt
					.executeQuery("SELECT SQL_CACHE * FROM retrievalPerfTestHeap");

			long queryEnd = System.currentTimeMillis();
			averageQueryTimeMs += ((double) (queryEnd - queryBegin) / NUM_TESTS);

			long traverseBegin = System.currentTimeMillis();

			while (this.rs.next()) {
				this.rs.getInt(1);
				this.rs.getString(2);
			}

			long traverseEnd = System.currentTimeMillis();
			averageTraversalTimeMs += ((double) (traverseEnd - traverseBegin) / NUM_TESTS);
		}

		double fullEnd = System.currentTimeMillis();
		double fullTime = (fullEnd - fullBegin) / 1000;
		double queriesPerSec = NUM_TESTS / fullTime;
		double rowsPerSec = (NUM_ROWS * NUM_TESTS) / fullTime;
		System.out.println("\nQuery Cache From Heap Retrieval\n");
		System.out.println("Full test took: " + fullTime + " seconds.");
		System.out.println("Queries/second: " + queriesPerSec);
		System.out.println("Rows/second: " + rowsPerSec);
		System.out.println("Avg. Query Exec Time: " + averageQueryTimeMs
				+ " ms");
		System.out.println("Avg. Traversal Time: " + averageTraversalTimeMs
				+ " ms");

		// We're doing something wrong if we can't beat 45 seconds :(
		assertTrue(fullTime < 45);
	}

	/**
	 * Tests retrieval from HEAP tables
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	public void testRetrievalHeap() throws Exception {
		double fullBegin = System.currentTimeMillis();
		double averageQueryTimeMs = 0;
		double averageTraversalTimeMs = 0;

		for (int i = 0; i < NUM_TESTS; i++) {
			long queryBegin = System.currentTimeMillis();
			this.rs = this.stmt
					.executeQuery("SELECT * FROM retrievalPerfTestHeap");

			long queryEnd = System.currentTimeMillis();
			averageQueryTimeMs += ((double) (queryEnd - queryBegin) / NUM_TESTS);

			long traverseBegin = System.currentTimeMillis();

			while (this.rs.next()) {
				this.rs.getInt(1);
				this.rs.getString(2);
			}

			long traverseEnd = System.currentTimeMillis();
			averageTraversalTimeMs += ((double) (traverseEnd - traverseBegin) / NUM_TESTS);
		}

		double fullEnd = System.currentTimeMillis();
		double fullTime = (fullEnd - fullBegin) / 1000;
		double queriesPerSec = NUM_TESTS / fullTime;
		double rowsPerSec = (NUM_ROWS * NUM_TESTS) / fullTime;
		System.out.println("\nHEAP Table Retrieval\n");
		System.out.println("Full test took: " + fullTime + " seconds.");
		System.out.println("Queries/second: " + queriesPerSec);
		System.out.println("Rows/second: " + rowsPerSec);
		System.out.println("Avg. Query Exec Time: " + averageQueryTimeMs
				+ " ms");
		System.out.println("Avg. Traversal Time: " + averageTraversalTimeMs
				+ " ms");

		// We're doing something wrong if we can't beat 45 seconds :(
		assertTrue(fullTime < 45);
	}

	/**
	 * Tests retrieval speed from MyISAM type tables
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	public void testRetrievalMyIsam() throws Exception {
		double fullBegin = System.currentTimeMillis();
		double averageQueryTimeMs = 0;
		double averageTraversalTimeMs = 0;

		for (int i = 0; i < NUM_TESTS; i++) {
			long queryBegin = System.currentTimeMillis();
			this.rs = this.stmt
					.executeQuery("SELECT * FROM retrievalPerfTestMyIsam");

			long queryEnd = System.currentTimeMillis();
			averageQueryTimeMs += ((double) (queryEnd - queryBegin) / NUM_TESTS);

			long traverseBegin = System.currentTimeMillis();

			while (this.rs.next()) {
				this.rs.getInt(1);
				this.rs.getString(2);
			}

			long traverseEnd = System.currentTimeMillis();
			averageTraversalTimeMs += ((double) (traverseEnd - traverseBegin) / NUM_TESTS);
		}

		double fullEnd = System.currentTimeMillis();
		double fullTime = (fullEnd - fullBegin) / 1000;
		double queriesPerSec = NUM_TESTS / fullTime;
		double rowsPerSec = (NUM_ROWS * NUM_TESTS) / fullTime;
		System.out.println("\nMyIsam Retrieval\n");
		System.out.println("Full test took: " + fullTime + " seconds.");
		System.out.println("Queries/second: " + queriesPerSec);
		System.out.println("Rows/second: " + rowsPerSec);
		System.out.println("Avg. Query Exec Time: " + averageQueryTimeMs
				+ " ms");
		System.out.println("Avg. Traversal Time: " + averageTraversalTimeMs
				+ " ms");

		// We're doing something wrong if we can't beat 45 seconds :(
		assertTrue(fullTime < 45);
	}
}