J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | package bench.serial; |
| 29 | |
| 30 | import bench.Benchmark; |
| 31 | import java.io.Externalizable; |
| 32 | import java.io.IOException; |
| 33 | import java.io.ObjectInput; |
| 34 | import java.io.ObjectInputStream; |
| 35 | import java.io.ObjectOutput; |
| 36 | import java.io.ObjectOutputStream; |
| 37 | |
| 38 | /** |
| 39 | * Benchmark for testing speed of writes and reads of a tree of Externalizable |
| 40 | * objects. |
| 41 | */ |
| 42 | public class ExternObjTrees implements Benchmark { |
| 43 | |
| 44 | static class Node implements Externalizable { |
| 45 | boolean z; |
| 46 | byte b; |
| 47 | char c; |
| 48 | short s; |
| 49 | int i; |
| 50 | float f; |
| 51 | long j; |
| 52 | double d; |
| 53 | String str = "bodega"; |
| 54 | Object parent, left, right; |
| 55 | |
| 56 | Node(Object parent, int depth) { |
| 57 | this.parent = parent; |
| 58 | if (depth > 0) { |
| 59 | left = new Node(this, depth - 1); |
| 60 | right = new Node(this, depth - 1); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public Node() { |
| 65 | } |
| 66 | |
| 67 | public void writeExternal(ObjectOutput out) throws IOException { |
| 68 | out.writeBoolean(z); |
| 69 | out.writeByte(b); |
| 70 | out.writeChar(c); |
| 71 | out.writeShort(s); |
| 72 | out.writeInt(i); |
| 73 | out.writeFloat(f); |
| 74 | out.writeLong(j); |
| 75 | out.writeDouble(d); |
| 76 | out.writeObject(str); |
| 77 | out.writeObject(parent); |
| 78 | out.writeObject(left); |
| 79 | out.writeObject(right); |
| 80 | } |
| 81 | |
| 82 | public void readExternal(ObjectInput in) |
| 83 | throws IOException, ClassNotFoundException |
| 84 | { |
| 85 | z = in.readBoolean(); |
| 86 | b = in.readByte(); |
| 87 | c = in.readChar(); |
| 88 | s = in.readShort(); |
| 89 | i = in.readInt(); |
| 90 | f = in.readFloat(); |
| 91 | j = in.readLong(); |
| 92 | d = in.readDouble(); |
| 93 | str = (String) in.readObject(); |
| 94 | parent = in.readObject(); |
| 95 | left = in.readObject(); |
| 96 | right = in.readObject(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Write and read a tree of externalizable objects from a stream. The |
| 102 | * benchmark is run in batches: each "batch" consists of a fixed number of |
| 103 | * read/write cycles, and the stream is flushed (and underlying stream |
| 104 | * buffer cleared) in between each batch. |
| 105 | * Arguments: <tree depth> <# batches> <# cycles per batch> |
| 106 | */ |
| 107 | public long run(String[] args) throws Exception { |
| 108 | int depth = Integer.parseInt(args[0]); |
| 109 | int nbatches = Integer.parseInt(args[1]); |
| 110 | int ncycles = Integer.parseInt(args[2]); |
| 111 | Node[] trees = genTrees(depth, ncycles); |
| 112 | StreamBuffer sbuf = new StreamBuffer(); |
| 113 | ObjectOutputStream oout = |
| 114 | new ObjectOutputStream(sbuf.getOutputStream()); |
| 115 | ObjectInputStream oin = |
| 116 | new ObjectInputStream(sbuf.getInputStream()); |
| 117 | |
| 118 | doReps(oout, oin, sbuf, trees, 1); // warmup |
| 119 | |
| 120 | long start = System.currentTimeMillis(); |
| 121 | doReps(oout, oin, sbuf, trees, nbatches); |
| 122 | return System.currentTimeMillis() - start; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Generate object trees. |
| 127 | */ |
| 128 | Node[] genTrees(int depth, int ntrees) { |
| 129 | Node[] trees = new Node[ntrees]; |
| 130 | for (int i = 0; i < ntrees; i++) { |
| 131 | trees[i] = new Node(null, depth); |
| 132 | } |
| 133 | return trees; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Run benchmark for given number of batches, with each batch containing |
| 138 | * the given number of cycles. |
| 139 | */ |
| 140 | void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
| 141 | StreamBuffer sbuf, Node[] trees, int nbatches) |
| 142 | throws Exception |
| 143 | { |
| 144 | int ncycles = trees.length; |
| 145 | for (int i = 0; i < nbatches; i++) { |
| 146 | sbuf.reset(); |
| 147 | oout.reset(); |
| 148 | for (int j = 0; j < ncycles; j++) { |
| 149 | oout.writeObject(trees[j]); |
| 150 | } |
| 151 | oout.flush(); |
| 152 | for (int j = 0; j < ncycles; j++) { |
| 153 | oin.readObject(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |