001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.junit.client.Benchmark;
019: import com.google.gwt.junit.client.IntRange;
020: import com.google.gwt.junit.client.Operator;
021:
022: import java.util.Arrays;
023:
024: /**
025: * Benchmarks sorts on arrays.
026: */
027: public class ArraySortBenchmark extends Benchmark {
028:
029: private static class TestObject implements Comparable<TestObject> {
030:
031: private int value;
032: @SuppressWarnings("unused")
033: private int index;
034:
035: public TestObject(int value, int index) {
036: this .value = value;
037: this .index = index;
038: }
039:
040: public int compareTo(TestObject o) {
041: return value - o.value;
042: }
043: }
044:
045: public final static int SUBARRAY_SKIP = 2;
046:
047: public final static int MAX_ARRAY_SIZE = 8192;
048:
049: // protected since the generated code is a subclass
050: protected byte[] initByteArray;
051: protected int[] initIntArray;
052: protected TestObject[] initObjectArray;
053:
054: protected byte[] byteArray;
055: protected int[] intArray;
056: protected TestObject[] objectArray;
057:
058: final IntRange sizeRange = new IntRange(128, MAX_ARRAY_SIZE,
059: Operator.ADD, 256);
060:
061: public String getModuleName() {
062: return "com.google.gwt.emultest.EmulSuite";
063: }
064:
065: public void beginByteArray(Integer size) {
066: byteArray = new byte[size.intValue()];
067: System.arraycopy(initByteArray, 0, byteArray, 0, size
068: .intValue());
069: }
070:
071: public void beginIntArray(Integer size) {
072: intArray = new int[size.intValue()];
073: System.arraycopy(initIntArray, 0, intArray, 0, size.intValue());
074: }
075:
076: public void beginObjectArray(Integer size) {
077: objectArray = new TestObject[size.intValue()];
078: System.arraycopy(initObjectArray, 0, objectArray, 0, size
079: .intValue());
080: }
081:
082: public void beginSubarray(Integer size) {
083: byteArray = new byte[size.intValue()];
084: System.arraycopy(initByteArray, 0, byteArray, 0, size
085: .intValue());
086: }
087:
088: // Required for JUnit
089: public void testByteArray() {
090: }
091:
092: /**
093: * Sorts <code>size</code> byte entries
094: * @gwt.benchmark.param size -limit = sizeRange
095: */
096: public void testByteArray(Integer size) {
097: Arrays.sort(byteArray);
098: }
099:
100: // Required for JUnit
101: public void testIntArray() {
102: }
103:
104: /**
105: * Sorts <code>size</code> int entries
106: * @gwt.benchmark.param size -limit = sizeRange
107: */
108: public void testIntArray(Integer size) {
109: Arrays.sort(intArray);
110: }
111:
112: // Required for JUnit
113: public void testObjectArray() {
114: }
115:
116: /**
117: * Sorts <code>size</code> object entries
118: * @gwt.benchmark.param size -limit = sizeRange
119: */
120: public void testObjectArray(Integer size) {
121: Arrays.sort(objectArray);
122: }
123:
124: // Required for JUnit
125: public void testSubarray() {
126: }
127:
128: /**
129: * Sorts <code>size</code> byte entries as a subarray
130: * @gwt.benchmark.param size -limit = sizeRange
131: */
132: public void testSubarray(Integer size) {
133: Arrays.sort(byteArray, SUBARRAY_SKIP, size);
134: }
135:
136: @Override
137: protected void setUp() throws Exception {
138: /*
139: * Since the RNG available in web mode cannot accept a seed for reproducible
140: * reports we use a simple pseudorandom sequence here. Its only purpose is
141: * to reasonably shuffle the data.
142: */
143: super .setUp();
144: initByteArray = new byte[MAX_ARRAY_SIZE + SUBARRAY_SKIP];
145: for (int i = 0; i < MAX_ARRAY_SIZE + SUBARRAY_SKIP; i++) {
146: initByteArray[i] = (byte) (i * 31 + 17);
147: }
148: initIntArray = new int[MAX_ARRAY_SIZE];
149: for (int i = 0; i < MAX_ARRAY_SIZE; i++) {
150: initIntArray[i] = (int) (i * 3151017 + 17);
151: }
152: initObjectArray = new TestObject[MAX_ARRAY_SIZE + SUBARRAY_SKIP];
153: for (int i = 0; i < MAX_ARRAY_SIZE + SUBARRAY_SKIP; i++) {
154: initObjectArray[i] = new TestObject((i * 31 + 17) % 500, i);
155: }
156: }
157:
158: }
|