blob: f52f0438a590d0a529b76184575e705f7774997e [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
28package bench.serial;
29
30import bench.Benchmark;
31import java.io.ObjectInputStream;
32import java.io.ObjectOutputStream;
33import java.io.Serializable;
34
35/**
36 * Benchmark for testing speed of writes and reads of a tree of replaceable
37 * objects.
38 */
39public class ReplaceTrees implements Benchmark {
40
41 static class Node implements Serializable {
42 Object parent, left, right;
43
44 Node(Object parent, Object left, Object right) {
45 this.parent = parent;
46 this.left = left;
47 this.right = right;
48 }
49
50 Node(Object parent, int depth) {
51 this.parent = parent;
52 if (depth > 0) {
53 left = new Node(this, depth - 1);
54 right = new Node(this, depth - 1);
55 }
56 }
57
58 Object writeReplace() {
59 return new RepNode(parent, left, right);
60 }
61 }
62
63 static class RepNode implements Serializable {
64 Object parent, left, right;
65
66 RepNode(Object parent, Object left, Object right) {
67 this.parent = parent;
68 this.left = left;
69 this.right = right;
70 }
71
72 Object readResolve() {
73 return new Node(parent, left, right);
74 }
75 }
76
77 /**
78 * Write and read a tree of replaceable objects from a stream. The
79 * benchmark is run in batches: each "batch" consists of a fixed number of
80 * read/write cycles, and the stream is flushed (and underlying stream
81 * buffer cleared) in between each batch.
82 * Arguments: <tree depth> <# batches> <# cycles per batch>
83 */
84 public long run(String[] args) throws Exception {
85 int depth = Integer.parseInt(args[0]);
86 int nbatches = Integer.parseInt(args[1]);
87 int ncycles = Integer.parseInt(args[2]);
88 Node[] trees = genTrees(depth, ncycles);
89 StreamBuffer sbuf = new StreamBuffer();
90 ObjectOutputStream oout =
91 new ObjectOutputStream(sbuf.getOutputStream());
92 ObjectInputStream oin =
93 new ObjectInputStream(sbuf.getInputStream());
94
95 doReps(oout, oin, sbuf, trees, 1); // warmup
96
97 long start = System.currentTimeMillis();
98 doReps(oout, oin, sbuf, trees, nbatches);
99 return System.currentTimeMillis() - start;
100 }
101
102 /**
103 * Generate object trees.
104 */
105 Node[] genTrees(int depth, int ntrees) {
106 Node[] trees = new Node[ntrees];
107 for (int i = 0; i < ntrees; i++) {
108 trees[i] = new Node(null, depth);
109 }
110 return trees;
111 }
112
113 /**
114 * Run benchmark for given number of batches, with each batch containing
115 * the given number of cycles.
116 */
117 void doReps(ObjectOutputStream oout, ObjectInputStream oin,
118 StreamBuffer sbuf, Node[] trees, int nbatches)
119 throws Exception
120 {
121 int ncycles = trees.length;
122 for (int i = 0; i < nbatches; i++) {
123 sbuf.reset();
124 oout.reset();
125 for (int j = 0; j < ncycles; j++) {
126 oout.writeObject(trees[j]);
127 }
128 oout.flush();
129 for (int j = 0; j < ncycles; j++) {
130 oin.readObject();
131 }
132 }
133 }
134}
135