author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 309 | bda219d843f6 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
1 |
/* |
715 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 { |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
42 |
|
2 | 43 |
static class Node implements Serializable { |
44 |
boolean z; |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
45 |
byte b; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
46 |
char c; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
47 |
short s; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
48 |
int i; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
49 |
float f; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
50 |
long j; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
51 |
double d; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
52 |
String str = "bodega"; |
2 | 53 |
Object parent, left, right; |
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
54 |
|
2 | 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 |
} |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
79 |
|
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
80 |
private void readObject(ObjectInputStream in) |
2 | 81 |
throws IOException, ClassNotFoundException |
82 |
{ |
|
83 |
ObjectInputStream.GetField fields = in.readFields(); |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
84 |
z = fields.get("z", false); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
85 |
b = fields.get("b", (byte) 0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
86 |
c = fields.get("c", (char) 0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
87 |
s = fields.get("s", (short) 0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
88 |
i = fields.get("i", (int) 0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
89 |
f = fields.get("f", (float) 0.0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
90 |
j = fields.get("j", (long) 0); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
91 |
d = fields.get("d", (double) 0.0); |
2 | 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 { |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
107 |
int depth = Integer.parseInt(args[0]); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
108 |
int nbatches = Integer.parseInt(args[1]); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
109 |
int ncycles = Integer.parseInt(args[2]); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
110 |
Node[] trees = genTrees(depth, ncycles); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
111 |
StreamBuffer sbuf = new StreamBuffer(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
112 |
ObjectOutputStream oout = |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
113 |
new ObjectOutputStream(sbuf.getOutputStream()); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
114 |
ObjectInputStream oin = |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
115 |
new ObjectInputStream(sbuf.getInputStream()); |
2 | 116 |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
117 |
doReps(oout, oin, sbuf, trees, 1); // warmup |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
118 |
|
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
119 |
long start = System.currentTimeMillis(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
120 |
doReps(oout, oin, sbuf, trees, nbatches); |
2 | 121 |
return System.currentTimeMillis() - start; |
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Generate object trees. |
|
126 |
*/ |
|
127 |
Node[] genTrees(int depth, int ntrees) { |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
128 |
Node[] trees = new Node[ntrees]; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
129 |
for (int i = 0; i < ntrees; i++) { |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
130 |
trees[i] = new Node(null, depth); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
131 |
} |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
132 |
return trees; |
2 | 133 |
} |
134 |
||
135 |
/** |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
136 |
* Run benchmark for given number of batches, with each batch containing |
2 | 137 |
* the given number of cycles. |
138 |
*/ |
|
139 |
void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
|
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
140 |
StreamBuffer sbuf, Node[] trees, int nbatches) |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
141 |
throws Exception |
2 | 142 |
{ |
309
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
143 |
int ncycles = trees.length; |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
144 |
for (int i = 0; i < nbatches; i++) { |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
145 |
sbuf.reset(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
146 |
oout.reset(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
147 |
for (int j = 0; j < ncycles; j++) { |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
148 |
oout.writeObject(trees[j]); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
149 |
} |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
150 |
oout.flush(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
151 |
for (int j = 0; j < ncycles; j++) { |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
152 |
oin.readObject(); |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
153 |
} |
bda219d843f6
6627823: Missed whitespace normalization files: jdk/test/java/rmi
ohair
parents:
2
diff
changeset
|
154 |
} |
2 | 155 |
} |
156 |
} |