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.IOException; |
| 32 | import java.io.ObjectInputStream; |
| 33 | import java.io.ObjectOutputStream; |
| 34 | import java.io.Serializable; |
| 35 | |
| 36 | /** |
| 37 | * Benchmark for testing speed of writes and reads of an object tree, where |
| 38 | * nodes contain explicit writeObject() and readObject() methods which use the |
| 39 | * GetField()/PutField() API. |
| 40 | */ |
| 41 | public class GetPutFieldTrees implements Benchmark { |
| 42 | |
| 43 | static class Node implements Serializable { |
| 44 | boolean z; |
| 45 | byte b; |
| 46 | char c; |
| 47 | short s; |
| 48 | int i; |
| 49 | float f; |
| 50 | long j; |
| 51 | double d; |
| 52 | String str = "bodega"; |
| 53 | Object parent, left, right; |
| 54 | |
| 55 | Node(Object parent, int depth) { |
| 56 | this.parent = parent; |
| 57 | if (depth > 0) { |
| 58 | left = new Node(this, depth - 1); |
| 59 | right = new Node(this, depth - 1); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private void writeObject(ObjectOutputStream out) throws IOException { |
| 64 | ObjectOutputStream.PutField fields = out.putFields(); |
| 65 | fields.put("z", z); |
| 66 | fields.put("b", b); |
| 67 | fields.put("c", c); |
| 68 | fields.put("s", s); |
| 69 | fields.put("i", i); |
| 70 | fields.put("f", f); |
| 71 | fields.put("j", j); |
| 72 | fields.put("d", d); |
| 73 | fields.put("str", str); |
| 74 | fields.put("parent", parent); |
| 75 | fields.put("left", left); |
| 76 | fields.put("right", right); |
| 77 | out.writeFields(); |
| 78 | } |
| 79 | |
| 80 | private void readObject(ObjectInputStream in) |
| 81 | throws IOException, ClassNotFoundException |
| 82 | { |
| 83 | ObjectInputStream.GetField fields = in.readFields(); |
| 84 | z = fields.get("z", false); |
| 85 | b = fields.get("b", (byte) 0); |
| 86 | c = fields.get("c", (char) 0); |
| 87 | s = fields.get("s", (short) 0); |
| 88 | i = fields.get("i", (int) 0); |
| 89 | f = fields.get("f", (float) 0.0); |
| 90 | j = fields.get("j", (long) 0); |
| 91 | d = fields.get("d", (double) 0.0); |
| 92 | str = (String) fields.get("str", null); |
| 93 | parent = fields.get("parent", null); |
| 94 | left = fields.get("left", null); |
| 95 | right = fields.get("right", null); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Write and read a tree of objects from a stream. The benchmark is run in |
| 101 | * batches: each "batch" consists of a fixed number of read/write cycles, |
| 102 | * and the stream is flushed (and underlying stream buffer cleared) in |
| 103 | * between each batch. |
| 104 | * Arguments: <tree depth> <# batches> <# cycles per batch> |
| 105 | */ |
| 106 | public long run(String[] args) throws Exception { |
| 107 | int depth = Integer.parseInt(args[0]); |
| 108 | int nbatches = Integer.parseInt(args[1]); |
| 109 | int ncycles = Integer.parseInt(args[2]); |
| 110 | Node[] trees = genTrees(depth, ncycles); |
| 111 | StreamBuffer sbuf = new StreamBuffer(); |
| 112 | ObjectOutputStream oout = |
| 113 | new ObjectOutputStream(sbuf.getOutputStream()); |
| 114 | ObjectInputStream oin = |
| 115 | new ObjectInputStream(sbuf.getInputStream()); |
| 116 | |
| 117 | doReps(oout, oin, sbuf, trees, 1); // warmup |
| 118 | |
| 119 | long start = System.currentTimeMillis(); |
| 120 | doReps(oout, oin, sbuf, trees, nbatches); |
| 121 | return System.currentTimeMillis() - start; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generate object trees. |
| 126 | */ |
| 127 | Node[] genTrees(int depth, int ntrees) { |
| 128 | Node[] trees = new Node[ntrees]; |
| 129 | for (int i = 0; i < ntrees; i++) { |
| 130 | trees[i] = new Node(null, depth); |
| 131 | } |
| 132 | return trees; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Run benchmark for given number of batches, with each batch containing |
| 137 | * the given number of cycles. |
| 138 | */ |
| 139 | void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
| 140 | StreamBuffer sbuf, Node[] trees, int nbatches) |
| 141 | throws Exception |
| 142 | { |
| 143 | int ncycles = trees.length; |
| 144 | for (int i = 0; i < nbatches; i++) { |
| 145 | sbuf.reset(); |
| 146 | oout.reset(); |
| 147 | for (int j = 0; j < ncycles; j++) { |
| 148 | oout.writeObject(trees[j]); |
| 149 | } |
| 150 | oout.flush(); |
| 151 | for (int j = 0; j < ncycles; j++) { |
| 152 | oin.readObject(); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |