1/* 2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * 26 */ 27 28package bench.serial; 29 30import bench.Benchmark; 31import java.io.IOException; 32import java.io.ObjectInputStream; 33import java.io.ObjectOutputStream; 34import java.io.Serializable; 35 36/** 37 * Benchmark for testing speed of writes and reads of an object tree, where 38 * nodes contain custom writeObject() and readObject() methods. 39 */ 40public class CustomObjTrees implements Benchmark { 41 42 static class Node implements Serializable { 43 boolean z; 44 byte b; 45 char c; 46 short s; 47 int i; 48 float f; 49 long j; 50 double d; 51 String str = "bodega"; 52 Object parent, left, right; 53 54 Node(Object parent, int depth) { 55 this.parent = parent; 56 if (depth > 0) { 57 left = new Node(this, depth - 1); 58 right = new Node(this, depth - 1); 59 } 60 } 61 62 private void writeObject(ObjectOutputStream out) throws IOException { 63 out.writeBoolean(z); 64 out.writeByte(b); 65 out.writeChar(c); 66 out.writeShort(s); 67 out.writeInt(i); 68 out.writeFloat(f); 69 out.writeLong(j); 70 out.writeDouble(d); 71 out.writeObject(str); 72 out.writeObject(parent); 73 out.writeObject(left); 74 out.writeObject(right); 75 } 76 77 private void readObject(ObjectInputStream in) 78 throws IOException, ClassNotFoundException 79 { 80 z = in.readBoolean(); 81 b = in.readByte(); 82 c = in.readChar(); 83 s = in.readShort(); 84 i = in.readInt(); 85 f = in.readFloat(); 86 j = in.readLong(); 87 d = in.readDouble(); 88 str = (String) in.readObject(); 89 parent = in.readObject(); 90 left = in.readObject(); 91 right = in.readObject(); 92 } 93 } 94 95 /** 96 * Write and read a tree of objects from a stream. The benchmark is run in 97 * batches: each "batch" consists of a fixed number of read/write cycles, 98 * and the stream is flushed (and underlying stream buffer cleared) in 99 * between each batch. 100 * Arguments: <tree depth> <# batches> <# cycles per batch> 101 */ 102 public long run(String[] args) throws Exception { 103 int depth = Integer.parseInt(args[0]); 104 int nbatches = Integer.parseInt(args[1]); 105 int ncycles = Integer.parseInt(args[2]); 106 Node[] trees = genTrees(depth, ncycles); 107 StreamBuffer sbuf = new StreamBuffer(); 108 ObjectOutputStream oout = 109 new ObjectOutputStream(sbuf.getOutputStream()); 110 ObjectInputStream oin = 111 new ObjectInputStream(sbuf.getInputStream()); 112 113 doReps(oout, oin, sbuf, trees, 1); // warmup 114 115 long start = System.currentTimeMillis(); 116 doReps(oout, oin, sbuf, trees, nbatches); 117 return System.currentTimeMillis() - start; 118 } 119 120 /** 121 * Generate object trees. 122 */ 123 Node[] genTrees(int depth, int ntrees) { 124 Node[] trees = new Node[ntrees]; 125 for (int i = 0; i < ntrees; i++) { 126 trees[i] = new Node(null, depth); 127 } 128 return trees; 129 } 130 131 /** 132 * Run benchmark for given number of batches, with each batch containing 133 * the given number of cycles. 134 */ 135 void doReps(ObjectOutputStream oout, ObjectInputStream oin, 136 StreamBuffer sbuf, Node[] trees, int nbatches) 137 throws Exception 138 { 139 int ncycles = trees.length; 140 for (int i = 0; i < nbatches; i++) { 141 sbuf.reset(); 142 oout.reset(); 143 for (int j = 0; j < ncycles; j++) { 144 oout.writeObject(trees[j]); 145 } 146 oout.flush(); 147 for (int j = 0; j < ncycles; j++) { 148 oin.readObject(); 149 } 150 } 151 } 152} 153