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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/*
Derby - Class org.apache.derbyTesting.perf.basic.jdbc.ClobAccessTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.perf.basic.jdbc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBCPerfTestCase;
import org.apache.derbyTesting.perf.clients.BackToBackLoadGenerator;
import org.apache.derbyTesting.perf.clients.Client;
import org.apache.derbyTesting.perf.clients.DBFiller;
import org.apache.derbyTesting.perf.clients.LoadGenerator;
import org.apache.derbyTesting.perf.clients.SingleRecordFiller;
import org.apache.derbyTesting.perf.clients.SingleRecordSelectClient;
/**
* A series of tests accessing Clobs in various ways.
* <p>
* These tests are intended to detect Clob performance regressions. Before
* committing a patch that might change the Clob performance characteristics,
* first run these tests on a clean build and then with the patch applied. The
* results can only be compared when both runs are done on the same machine.
* <p>
* The results are time taken to execute the test. Lower duration is better
* (improvement). Currently the results are printed to standard out. There is
* one exception, which is {@code testConcurrency}. For this test, the
* throughput is printed and it will always run for a fixed amount of time.
* <p>
* The tests are written with two axis in mind: read-only vs update and small vs
* large. These axis were chosen based on the Clob implementation at the time.
* In the context of this test, small means the Clob is represented as a string
* by the Derby store and large means the Clob is represtend as a stream into
* the Derby store. When a Clob is modified, an in-memory or on disk temporary
* copy is created. The performance of these temporary representations are
* tested with the tests that modify the Clob content.
* <p>
* System properties controlling test behavior:
* <dl>
* <dt>derby.tests.disableSmallClobs</dt>
* <dd>Whether or not to disable the testing of small Clobs.</dd>
* <dt>derby.tests.disableLargeClobs</dt>
* <dd>Whether or not to disable the testing of large Clobs.</dd>
* <dt>derby.tests.disableConcurrencyTest</dt>
* <dd>Whether or not to disable the concurrency test.</dd>
* <dt>derby.tests.largeClobSize</dt>
* <dd>Size of the large Clobs in MB, 15 MB is the default.</dd>
* <dt>derby.tests.runLargeClobTests</dt>
* <dd>A list of one or more tests to run. Only tests using large Clobs
* should be specified, but this is not enforced. Example:
* <tt>testFetchLargeClobPieceByPiece,testLargeClobGetLength</tt></dd>
* </dl>
*
* <p>
* <b>NOTE</b>: Currently there are no tests for the client driver (network)
* or for encrypted Clobs.
*/
public class ClobAccessTest
extends JDBCPerfTestCase {
private static final boolean disableSmallClobs =
Boolean.getBoolean("derby.tests.disableSmallClobs");
private static final boolean disableLargeClobs =
Boolean.getBoolean("derby.tests.disableLargeClobs");
private static final boolean disableConcurrencyTest =
Boolean.getBoolean("derby.tests.disableConcurrencyTest");
/**
* A list of one or more tests to be run. Only tests using a large Clob
* should be specified.
*/
private static final String runLargeClobTests =
System.getProperty("derby.tests.runLargeClobTests", null);
private static final int largeClobSizeMB =
Integer.getInteger("derby.tests.largeClobSize", 15).intValue();
/** Maximum buffer size to use. */
private static final int MAX_BSIZE = 32676;
/**
* Instantiates a new test that will be run the specified number of
* iterations and repeated as specified.
*
* @param name name of the test to instantiate
* @param iterations number of iterations per repetition
* @param repeats number of repetitions
*/
public ClobAccessTest(String name, int iterations, int repeats) {
super(name, iterations, repeats);
}
/**
* Set autocommit to false by default.
*/
public void initializeConnection(Connection conn)
throws SQLException {
conn.setAutoCommit(false);
}
public static Test suite() {
BaseTestSuite mainSuite = new BaseTestSuite("ClobAccessTest suite");
if (!disableSmallClobs) {
int iters = 50;
int reps = 1;
println("Adding small Clob tests.");
BaseTestSuite smallSuite = new BaseTestSuite("Small Clob suite");
smallSuite.addTest(new ClobAccessTest(
"testFetchSmallClobs", iters, reps));
smallSuite.addTest(new ClobAccessTest(
"testFetchSmallClobsInaccurateLength", iters, reps));
smallSuite.addTest(new ClobAccessTest(
"testModifySmallClobs", iters, reps));
mainSuite.addTest(smallSuite);
}
if (!disableLargeClobs) {
int iters = 5;
int reps = 1;
String[] tests = new String[] {
"testFetchLargeClobs",
"testFetchLargeClobsModified",
"testFetchLargeClobWithStream",
"testFetchLargeClobOneByOneCharBaseline",
"testFetchLargeClobOneByOneCharModified",
"testFetchLargeClobOneByOneChar",
"testFetchLargeClobPieceByPiece",
"testFetchLargeClobPieceByPieceModified",
"testLargeClobGetLength",
"testLargeClobGetLengthModified",
"testLargeClobTruncateLengthMinusOne",
"testFetchLargeClobPieceByPieceBackwards",
};
// See if the user has overridden which tests to run.
if (runLargeClobTests != null) {
String[] specifiedTests = runLargeClobTests.split(",");
if (specifiedTests.length > 0) {
tests = specifiedTests;
}
}
println("Adding " + tests.length + " large Clob tests.");
BaseTestSuite largeSuite = new BaseTestSuite("Large Clob suite");
for (int i=0; i < tests.length; i++) {
largeSuite.addTest(new ClobAccessTest(tests[i] , iters, reps));
}
mainSuite.addTest(largeSuite);
}
if (!disableConcurrencyTest) {
mainSuite.addTest(new ClobAccessTest("testConcurrency", 1, 1));
}
return new CleanDatabaseTestSetup(mainSuite) {
protected void decorateSQL(Statement stmt)
throws SQLException {
initializeClobData(stmt);
}
};
}
/**
* Fetches a number of small Clobs, getting the content using getSubString.
* <p>
* The exact length of the clob is used when getting the string.
*/
public void testFetchSmallClobs()
throws SQLException {
PreparedStatement ps = prepareStatement(
"select dClob, length from smallClobs");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int clobLength = rs.getInt(2);
String content = clob.getSubString(1, clobLength);
}
rs.close();
}
/**
* Fetches a number of small Clobs, getting the content using getSubString.
* <p>
* A too long length of the clob is used when getting the string.
*/
public void testFetchSmallClobsInaccurateLength()
throws SQLException {
PreparedStatement ps = prepareStatement(
"select dClob, length from smallClobs");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int unusedLength = rs.getInt(2);
String content = clob.getSubString(1, 100);
}
rs.close();
}
/**
* Test fetching the content after adding a single character at the end.
*/
public void testModifySmallClobs()
throws SQLException {
PreparedStatement ps = prepareStatement(
"select dClob, length from smallClobs");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int length = rs.getInt(2);
clob.setString(length, "X");
String content = clob.getSubString(1, 100);
}
rs.close();
}
public void testFetchLargeClobs()
throws IOException, SQLException {
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs");
ResultSet rs = ps.executeQuery();
char[] charBuf = new char[16*1024]; // 16 KB
while (rs.next()) {
Clob clob = rs.getClob(1);
Reader content = clob.getCharacterStream();
long remaining = rs.getInt(2);
while (remaining > 0) {
remaining -= content.read(charBuf);
}
content.close();
}
rs.close();
}
public void testFetchLargeClobsModified()
throws IOException, SQLException {
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs");
ResultSet rs = ps.executeQuery();
char[] charBuf = new char[16*1024]; // 16 KB
while (rs.next()) {
Clob clob = rs.getClob(1);
clob.setString(1, "X");
Reader content = clob.getCharacterStream();
long remaining = rs.getInt(2);
while (remaining > 0) {
remaining -= content.read(charBuf);
}
content.close();
}
rs.close();
}
/**
* Fetches a single Clob and reads it char by char, but utilizing a
* buffered stream to get a lower time bound on the read operation.
*/
public void testFetchLargeClobOneByOneCharBaseline()
throws IOException, SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
Reader content = clob.getCharacterStream();
BufferedReader bufferedContent = new BufferedReader(content);
long remaining = rs.getInt(2);
while (bufferedContent.read() != -1) {
remaining--;
}
content.close();
assertEquals(0, remaining);
}
rs.close();
}
public void testFetchLargeClobOneByOneChar()
throws IOException, SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
Reader content = clob.getCharacterStream();
long remaining = rs.getInt(2);
while (content.read() != -1) {
remaining--;
}
content.close();
assertEquals(0, remaining);
}
rs.close();
}
public void testFetchLargeClobOneByOneCharModified()
throws IOException, SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
long remaining = rs.getInt(2);
clob.setString(++remaining, "X");
Reader content = clob.getCharacterStream();
while (content.read() != -1) {
remaining --;
}
content.close();
assertEquals(0, remaining);
}
rs.close();
}
/**
* Tests that repositioning within the current internal character buffer is
* cheap.
* <p>
* Note that the positions used in this test have been chosen based on the
* internal buffer size (8KB), which is an implementation detail.
*
* @throws SQLException if the test fails
*/
public void testFetchLargeClobPieceByPieceBackwards()
throws IOException, SQLException {
boolean modifyClob = false;
final int intBufSize = 8192; // Implementation detail.
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int remaining = rs.getInt(2);
if (modifyClob) {
// Modify the Clob to create a temporary copy in memory or on
// disk (depends on the Clob size).
long modifyStart = System.currentTimeMillis();
clob.setString(++remaining, "X");
println("Clob modification duration: " +
(System.currentTimeMillis() - modifyStart) + " ms");
}
// Go close to the middle of the Clob on a buffer border, then
// subtract the piece size to avoid repositioning.
final int pieceSize = 10;
final long pos = (remaining / 2 / intBufSize) *
intBufSize - pieceSize;
for (int i=0; i < intBufSize; i += pieceSize) {
String str = clob.getSubString(
pos -i, pieceSize);
}
}
rs.close();
}
/**
* Fetches a "large" Clob piece by piece using getSubString.
*/
public void testFetchLargeClobPieceByPiece()
throws IOException, SQLException {
fetchPieceByPiece(false);
}
/**
* Fetches a "large" Clob piece by piece using getSubString.
* <p>
* The Clob is modified before fetched to create a temporary Clob
* representation in memory / on disk.
*/
public void testFetchLargeClobPieceByPieceModified()
throws IOException, SQLException {
fetchPieceByPiece(true);
}
/**
* Fetches a "large" Clob piece by piece using getSubString.
*
* @param modifyClob whether to modify the Clob before fetching it
* (determines the internal Derby Clob representation)
*/
private void fetchPieceByPiece(final boolean modifyClob)
throws IOException, SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 4");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int remaining = rs.getInt(2);
Reader myReader = new LoopingAlphabetReader(remaining);
if (modifyClob) {
// Modify the Clob to create a temporary copy in memory or on
// disk (depends on the Clob size).
long modifyStart = System.currentTimeMillis();
clob.setString(++remaining, "X");
println("Clob modification duration: " +
(System.currentTimeMillis() - modifyStart) + " ms");
}
long pos = 1;
while (remaining > 0) {
String str = clob.getSubString(
pos, Math.min(MAX_BSIZE, remaining));
myReader.skip(Math.min(MAX_BSIZE, remaining) -1);
pos += str.length();
remaining -= str.length();
// Avoid failure on the last char when Clob is modified.
if (!modifyClob || remaining != 0) {
assertEquals(myReader.read(), str.charAt(str.length() -1));
}
}
}
rs.close();
}
public void testFetchLargeClobWithStream()
throws IOException, SQLException {
boolean modifyClob = false;
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 5");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int remaining = rs.getInt(2);
Reader myReader = new LoopingAlphabetReader(remaining);
if (modifyClob) {
// Modify the Clob to create a temporary copy in memory or on
// disk (depends on the Clob size).
long modifyStart = System.currentTimeMillis();
clob.setString(++remaining, "X");
println("Clob modification duration: " +
(System.currentTimeMillis() - modifyStart) + " ms");
}
Reader clobReader = clob.getCharacterStream();
char[] buf = new char[MAX_BSIZE];
while (remaining > 0) {
int read = clobReader.read(buf, 0, Math.min(MAX_BSIZE, remaining));
myReader.skip(read -1);
remaining -= read;
assertEquals(myReader.read(), buf[read -1]);
}
}
rs.close();
}
/**
* Tests if the Clob length is cached.
*/
public void testLargeClobGetLength() throws SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 7");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
long remaining = rs.getInt(2);
// This should be cached. Lots of data have to be skipped otherwise.
for (int i=0; i < 50; i++) {
assertEquals(remaining, clob.length());
}
}
rs.close();
}
/**
* Tests if the Clob length is cached.
*/
public void testLargeClobGetLengthModified() throws SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 7");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
clob.setString(1, "X");
long remaining = rs.getInt(2);
// This should be cached. Lots of data have to be skipped otherwise.
for (int i=0; i < 50; i++) {
assertEquals(remaining, clob.length());
}
}
rs.close();
}
/**
* Tests the speed of transferring data from the store to local temporary
* storage as part of the truncate operation.
*/
public void testLargeClobTruncateLengthMinusOne()
throws SQLException {
// Select just one Clob.
PreparedStatement ps = prepareStatement(
"select dClob, length from largeClobs where id = 8");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob(1);
int length = rs.getInt(2);
clob.truncate(length -1);
}
}
/**
* Runs a test using multiple threads.
* <p>
* This test intends to detect problems with small Clobs and general
* problems with concurrency.
* <p>
* <b>NOTE</b>: To produce more reliable numbers, please run the performance
* client independently outside this JUnit test framework. Performance also
* suffers greatly with SANE builds.
*/
public void testConcurrency()
throws InterruptedException, SQLException {
final int records = 100000;
final int tables = 1;
final int threads = 16;
DBFiller filler = new SingleRecordFiller(
records, tables, java.sql.Types.CLOB, false, false);
Connection conn = getConnection();
println("initializing database...");
filler.fill(conn);
conn.close();
Client[] clients = new Client[threads];
for (int i = 0; i < clients.length; i++) {
Connection c = openDefaultConnection();
c.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
clients[i] = new SingleRecordSelectClient(
records, tables, java.sql.Types.CLOB, false, false);
clients[i].init(c);
}
final int warmupSec = 30;
final int steadySec = 60;
LoadGenerator gen = new BackToBackLoadGenerator();
gen.init(clients);
println("starting warmup...");
gen.startWarmup();
Thread.sleep(1000L * warmupSec);
println("entering steady state...");
gen.startSteadyState();
Thread.sleep(1000L * steadySec);
println("stopping threads...");
gen.stop();
// Should get the printstream used by the test harness here.
gen.printReport(System.out);
}
/**
* Generates test data.
*/
private static void initializeClobData(Statement stmt)
throws SQLException {
Connection con = stmt.getConnection();
con.setAutoCommit(false);
if (!disableSmallClobs) {
println("Generating small Clobs test data.");
// Insert small Clob data.
try {
stmt.executeUpdate("drop table smallClobs");
} catch (SQLException sqle) {
assertSQLState("42Y55", sqle);
}
stmt.executeUpdate(
"create table smallClobs (dClob clob, length int)");
PreparedStatement smallClobInsert = con.prepareStatement(
"insert into smallClobs values (?,?)");
// Insert 15 000 small clobs.
for (int clobCounter = 1; clobCounter < 15001; clobCounter++) {
String content = Integer.toString(clobCounter);
smallClobInsert.setString(1, content);
smallClobInsert.setInt(2, content.length());
smallClobInsert.executeUpdate();
if (clobCounter % 1000 == 0) {
con.commit();
}
}
con.commit();
}
if (!disableLargeClobs) {
println("Generating large Clobs test data.");
// Insert large Clob data.
try {
stmt.executeUpdate("drop table largeClobs");
} catch (SQLException sqle) {
assertSQLState("42Y55", sqle);
}
stmt.executeUpdate("create table largeClobs (" +
"id int unique not null, dClob clob, length int)");
PreparedStatement largeClobInsert = con.prepareStatement(
"insert into largeClobs values (?,?,?)");
// Insert some large Clobs.
final int size = largeClobSizeMB*1024*1024; // 15 MB default
for (int clobCounter = 1; clobCounter < 11; clobCounter++) {
largeClobInsert.setInt(1, clobCounter);
largeClobInsert.setCharacterStream(
2, new LoopingAlphabetReader(size), size);
largeClobInsert.setInt(3, size);
largeClobInsert.executeUpdate();
println("Inserted large Clob #" + (clobCounter -1));
}
con.commit();
}
}
}
|