blob: 01d60c54a157ad665b68eb98fe92fdb351148320 [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.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 that call
39 * defaultWriteObject() and defaultReadObject().
40 */
41public class CustomDefaultObjTrees 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 out.defaultWriteObject();
65 }
66
67 private void readObject(ObjectInputStream in)
68 throws IOException, ClassNotFoundException
69 {
70 in.defaultReadObject();
71 }
72 }
73
74 /**
75 * Write and read a tree of objects from a stream. The benchmark is run in
76 * batches: each "batch" consists of a fixed number of read/write cycles,
77 * and the stream is flushed (and underlying stream buffer cleared) in
78 * between each batch.
79 * Arguments: <tree depth> <# batches> <# cycles per batch>
80 */
81 public long run(String[] args) throws Exception {
82 int depth = Integer.parseInt(args[0]);
83 int nbatches = Integer.parseInt(args[1]);
84 int ncycles = Integer.parseInt(args[2]);
85 Node[] trees = genTrees(depth, ncycles);
86 StreamBuffer sbuf = new StreamBuffer();
87 ObjectOutputStream oout =
88 new ObjectOutputStream(sbuf.getOutputStream());
89 ObjectInputStream oin =
90 new ObjectInputStream(sbuf.getInputStream());
91
92 doReps(oout, oin, sbuf, trees, 1); // warmup
93
94 long start = System.currentTimeMillis();
95 doReps(oout, oin, sbuf, trees, nbatches);
96 return System.currentTimeMillis() - start;
97 }
98
99 /**
100 * Generate object trees.
101 */
102 Node[] genTrees(int depth, int ntrees) {
103 Node[] trees = new Node[ntrees];
104 for (int i = 0; i < ntrees; i++) {
105 trees[i] = new Node(null, depth);
106 }
107 return trees;
108 }
109
110 /**
111 * Run benchmark for given number of batches, with each batch containing
112 * the given number of cycles.
113 */
114 void doReps(ObjectOutputStream oout, ObjectInputStream oin,
115 StreamBuffer sbuf, Node[] trees, int nbatches)
116 throws Exception
117 {
118 int ncycles = trees.length;
119 for (int i = 0; i < nbatches; i++) {
120 sbuf.reset();
121 oout.reset();
122 for (int j = 0; j < ncycles; j++) {
123 oout.writeObject(trees[j]);
124 }
125 oout.flush();
126 for (int j = 0; j < ncycles; j++) {
127 oin.readObject();
128 }
129 }
130 }
131}
132