001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.util.Random;
009:
010: import org.h2.test.TestBase;
011: import org.h2.util.StringCache;
012:
013: /**
014: * Tests the string cache facility.
015: */
016: public class TestStringCache extends TestBase {
017:
018: public static void main(String[] args) throws Exception {
019: new TestStringCache().runBenchmark();
020: }
021:
022: private void runBenchmark() throws Exception {
023: returnNew = false;
024: for (int i = 0; i < 6; i++) {
025: useIntern = (i % 2) == 1;
026: long time = System.currentTimeMillis();
027: testSingleThread(100000);
028: time = System.currentTimeMillis() - time;
029: System.out.println(time + " ms (useIntern=" + useIntern
030: + ")");
031: }
032:
033: }
034:
035: private Random random = new Random(1);
036: private String[] some = new String[] { null, "", "ABC",
037: "this is a medium sized string", "1", "2" };
038: private volatile boolean stop;
039: private boolean returnNew;
040: private boolean useIntern;
041:
042: public void test() throws Exception {
043: returnNew = true;
044: StringCache.clearCache();
045: testSingleThread(getSize(5000, 20000));
046: testMultiThreads();
047: returnNew = false;
048: StringCache.clearCache();
049: testSingleThread(getSize(5000, 20000));
050: testMultiThreads();
051: }
052:
053: String randomString() {
054: if (random.nextBoolean()) {
055: String s = some[random.nextInt(some.length)];
056: if (s != null && random.nextBoolean()) {
057: s = new String(s);
058: }
059: return s;
060: } else {
061: int len = random.nextBoolean() ? random.nextInt(1000)
062: : random.nextInt(10);
063: StringBuffer buff = new StringBuffer(len);
064: for (int i = 0; i < len; i++) {
065: buff.append(random.nextInt(0xfff));
066: }
067: return buff.toString();
068: }
069: }
070:
071: void testString() {
072: String a = randomString();
073: if (returnNew) {
074: String b = StringCache.getNew(a);
075: try {
076: check(a, b);
077: } catch (Exception e) {
078: TestBase.logError("error", e);
079: }
080: if (a != null && a == b && a.length() > 0) {
081: throw new Error("a=" + System.identityHashCode(a)
082: + " b=" + System.identityHashCode(b));
083: }
084: } else {
085: String b;
086: if (useIntern) {
087: b = a == null ? null : a.intern();
088: } else {
089: b = StringCache.get(a);
090: }
091: try {
092: check(a, b);
093: } catch (Exception e) {
094: TestBase.logError("error", e);
095: }
096: }
097: }
098:
099: private void testSingleThread(int len) throws Exception {
100: for (int i = 0; i < len; i++) {
101: testString();
102: }
103: }
104:
105: private void testMultiThreads() throws Exception {
106: int threadCount = getSize(3, 100);
107: Thread[] threads = new Thread[threadCount];
108: for (int i = 0; i < threadCount; i++) {
109: Thread t = new Thread(new Runnable() {
110: public void run() {
111: while (!stop) {
112: testString();
113: }
114: }
115: });
116: threads[i] = t;
117: }
118: for (int i = 0; i < threadCount; i++) {
119: threads[i].start();
120: }
121: int wait = getSize(200, 2000);
122: Thread.sleep(wait);
123: stop = true;
124: for (int i = 0; i < threadCount; i++) {
125: threads[i].join();
126: }
127: }
128:
129: }
|