1 /* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package benchmarks.regression; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.ObjectInputStream; 22 import java.io.ObjectOutputStream; 23 import java.io.ObjectStreamClass; 24 import java.io.Serializable; 25 import java.util.ArrayList; 26 27 public class SerializationBenchmark { 28 private static byte[] bytes(Object o) throws Exception { 29 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 30 ObjectOutputStream out = new ObjectOutputStream(baos); 31 out.writeObject(o); 32 out.close(); 33 return baos.toByteArray(); 34 } 35 36 public void timeReadIntArray(int reps) throws Exception { 37 int[] intArray = new int[256]; 38 readSingleObject(reps, intArray); 39 } 40 41 public void timeWriteIntArray(int reps) throws Exception { 42 int[] intArray = new int[256]; 43 writeSingleObject(reps, intArray); 44 } 45 public void timeReadArrayListInteger(int reps) throws Exception { 46 ArrayList<Integer> object = new ArrayList<Integer>(); 47 for (int i = 0; i < 256; ++i) { 48 object.add(i); 49 } 50 readSingleObject(reps, object); 51 } 52 53 public void timeWriteArrayListInteger(int reps) throws Exception { 54 ArrayList<Integer> object = new ArrayList<Integer>(); 55 for (int i = 0; i < 256; ++i) { 56 object.add(i); 57 } 58 writeSingleObject(reps, object); 59 } 60 61 public void timeReadString(int reps) throws Exception { 62 readSingleObject(reps, "hello"); 63 } 64 65 public void timeReadObjectStreamClass(int reps) throws Exception { 66 // A special case because serialization itself requires this class. 67 // (This should really be a unit test.) 68 ObjectStreamClass osc = ObjectStreamClass.lookup(String.class); 69 readSingleObject(reps, osc); 70 } 71 72 public void timeWriteString(int reps) throws Exception { 73 // String is a special case that avoids JNI. 74 writeSingleObject(reps, "hello"); 75 } 76 77 public void timeWriteObjectStreamClass(int reps) throws Exception { 78 // A special case because serialization itself requires this class. 79 // (This should really be a unit test.) 80 ObjectStreamClass osc = ObjectStreamClass.lookup(String.class); 81 writeSingleObject(reps, osc); 82 } 83 84 // This is a baseline for the others. 85 public void timeWriteNoObjects(int reps) throws Exception { 86 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 87 ObjectOutputStream out = new ObjectOutputStream(baos); 88 for (int rep = 0; rep < reps; ++rep) { 89 out.reset(); 90 baos.reset(); 91 } 92 out.close(); 93 } 94 95 private void readSingleObject(int reps, Object object) throws Exception { 96 byte[] bytes = bytes(object); 97 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 98 for (int rep = 0; rep < reps; ++rep) { 99 ObjectInputStream in = new ObjectInputStream(bais); 100 in.readObject(); 101 in.close(); 102 bais.reset(); 103 } 104 } 105 106 private void writeSingleObject(int reps, Object o) throws Exception { 107 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 108 ObjectOutputStream out = new ObjectOutputStream(baos); 109 for (int rep = 0; rep < reps; ++rep) { 110 out.writeObject(o); 111 out.reset(); 112 baos.reset(); 113 } 114 out.close(); 115 } 116 117 public void timeWriteEveryKindOfField(int reps) throws Exception { 118 writeSingleObject(reps, new LittleBitOfEverything()); 119 } 120 public void timeWriteSerializableBoolean(int reps) throws Exception { 121 writeSingleObject(reps, new SerializableBoolean()); 122 } 123 public void timeWriteSerializableByte(int reps) throws Exception { 124 writeSingleObject(reps, new SerializableByte()); 125 } 126 public void timeWriteSerializableChar(int reps) throws Exception { 127 writeSingleObject(reps, new SerializableChar()); 128 } 129 public void timeWriteSerializableDouble(int reps) throws Exception { 130 writeSingleObject(reps, new SerializableDouble()); 131 } 132 public void timeWriteSerializableFloat(int reps) throws Exception { 133 writeSingleObject(reps, new SerializableFloat()); 134 } 135 public void timeWriteSerializableInt(int reps) throws Exception { 136 writeSingleObject(reps, new SerializableInt()); 137 } 138 public void timeWriteSerializableLong(int reps) throws Exception { 139 writeSingleObject(reps, new SerializableLong()); 140 } 141 public void timeWriteSerializableShort(int reps) throws Exception { 142 writeSingleObject(reps, new SerializableShort()); 143 } 144 public void timeWriteSerializableReference(int reps) throws Exception { 145 writeSingleObject(reps, new SerializableReference()); 146 } 147 148 public void timeReadEveryKindOfField(int reps) throws Exception { 149 readSingleObject(reps, new LittleBitOfEverything()); 150 } 151 public void timeReadSerializableBoolean(int reps) throws Exception { 152 readSingleObject(reps, new SerializableBoolean()); 153 } 154 public void timeReadSerializableByte(int reps) throws Exception { 155 readSingleObject(reps, new SerializableByte()); 156 } 157 public void timeReadSerializableChar(int reps) throws Exception { 158 readSingleObject(reps, new SerializableChar()); 159 } 160 public void timeReadSerializableDouble(int reps) throws Exception { 161 readSingleObject(reps, new SerializableDouble()); 162 } 163 public void timeReadSerializableFloat(int reps) throws Exception { 164 readSingleObject(reps, new SerializableFloat()); 165 } 166 public void timeReadSerializableInt(int reps) throws Exception { 167 readSingleObject(reps, new SerializableInt()); 168 } 169 public void timeReadSerializableLong(int reps) throws Exception { 170 readSingleObject(reps, new SerializableLong()); 171 } 172 public void timeReadSerializableShort(int reps) throws Exception { 173 readSingleObject(reps, new SerializableShort()); 174 } 175 public void timeReadSerializableReference(int reps) throws Exception { 176 readSingleObject(reps, new SerializableReference()); 177 } 178 179 public static class SerializableBoolean implements Serializable { 180 boolean z; 181 } 182 public static class SerializableByte implements Serializable { 183 byte b; 184 } 185 public static class SerializableChar implements Serializable { 186 char c; 187 } 188 public static class SerializableDouble implements Serializable { 189 double d; 190 } 191 public static class SerializableFloat implements Serializable { 192 float f; 193 } 194 public static class SerializableInt implements Serializable { 195 int i; 196 } 197 public static class SerializableLong implements Serializable { 198 long j; 199 } 200 public static class SerializableShort implements Serializable { 201 short s; 202 } 203 public static class SerializableReference implements Serializable { 204 Object l; 205 } 206 207 public static class LittleBitOfEverything implements Serializable { 208 boolean z; 209 byte b; 210 char c; 211 double d; 212 float f; 213 int i; 214 long j; 215 short s; 216 Object l; 217 } 218 } 219