37 * Benchmark for testing speed of writes and reads of an object tree, where |
37 * Benchmark for testing speed of writes and reads of an object tree, where |
38 * nodes contain custom writeObject() and readObject() methods that call |
38 * nodes contain custom writeObject() and readObject() methods that call |
39 * defaultWriteObject() and defaultReadObject(). |
39 * defaultWriteObject() and defaultReadObject(). |
40 */ |
40 */ |
41 public class CustomDefaultObjTrees implements Benchmark { |
41 public class CustomDefaultObjTrees implements Benchmark { |
42 |
42 |
43 static class Node implements Serializable { |
43 static class Node implements Serializable { |
44 boolean z; |
44 boolean z; |
45 byte b; |
45 byte b; |
46 char c; |
46 char c; |
47 short s; |
47 short s; |
48 int i; |
48 int i; |
49 float f; |
49 float f; |
50 long j; |
50 long j; |
51 double d; |
51 double d; |
52 String str = "bodega"; |
52 String str = "bodega"; |
53 Object parent, left, right; |
53 Object parent, left, right; |
54 |
54 |
55 Node(Object parent, int depth) { |
55 Node(Object parent, int depth) { |
56 this.parent = parent; |
56 this.parent = parent; |
57 if (depth > 0) { |
57 if (depth > 0) { |
58 left = new Node(this, depth - 1); |
58 left = new Node(this, depth - 1); |
59 right = new Node(this, depth - 1); |
59 right = new Node(this, depth - 1); |
60 } |
60 } |
61 } |
61 } |
62 |
62 |
63 private void writeObject(ObjectOutputStream out) throws IOException { |
63 private void writeObject(ObjectOutputStream out) throws IOException { |
64 out.defaultWriteObject(); |
64 out.defaultWriteObject(); |
65 } |
65 } |
66 |
66 |
67 private void readObject(ObjectInputStream in) |
67 private void readObject(ObjectInputStream in) |
68 throws IOException, ClassNotFoundException |
68 throws IOException, ClassNotFoundException |
69 { |
69 { |
70 in.defaultReadObject(); |
70 in.defaultReadObject(); |
71 } |
71 } |
72 } |
72 } |
73 |
73 |
74 /** |
74 /** |
75 * Write and read a tree of objects from a stream. The benchmark is run in |
75 * Write and read a tree of objects from a stream. The benchmark is run in |
77 * and the stream is flushed (and underlying stream buffer cleared) in |
77 * and the stream is flushed (and underlying stream buffer cleared) in |
78 * between each batch. |
78 * between each batch. |
79 * Arguments: <tree depth> <# batches> <# cycles per batch> |
79 * Arguments: <tree depth> <# batches> <# cycles per batch> |
80 */ |
80 */ |
81 public long run(String[] args) throws Exception { |
81 public long run(String[] args) throws Exception { |
82 int depth = Integer.parseInt(args[0]); |
82 int depth = Integer.parseInt(args[0]); |
83 int nbatches = Integer.parseInt(args[1]); |
83 int nbatches = Integer.parseInt(args[1]); |
84 int ncycles = Integer.parseInt(args[2]); |
84 int ncycles = Integer.parseInt(args[2]); |
85 Node[] trees = genTrees(depth, ncycles); |
85 Node[] trees = genTrees(depth, ncycles); |
86 StreamBuffer sbuf = new StreamBuffer(); |
86 StreamBuffer sbuf = new StreamBuffer(); |
87 ObjectOutputStream oout = |
87 ObjectOutputStream oout = |
88 new ObjectOutputStream(sbuf.getOutputStream()); |
88 new ObjectOutputStream(sbuf.getOutputStream()); |
89 ObjectInputStream oin = |
89 ObjectInputStream oin = |
90 new ObjectInputStream(sbuf.getInputStream()); |
90 new ObjectInputStream(sbuf.getInputStream()); |
91 |
|
92 doReps(oout, oin, sbuf, trees, 1); // warmup |
|
93 |
91 |
94 long start = System.currentTimeMillis(); |
92 doReps(oout, oin, sbuf, trees, 1); // warmup |
95 doReps(oout, oin, sbuf, trees, nbatches); |
93 |
|
94 long start = System.currentTimeMillis(); |
|
95 doReps(oout, oin, sbuf, trees, nbatches); |
96 return System.currentTimeMillis() - start; |
96 return System.currentTimeMillis() - start; |
97 } |
97 } |
98 |
98 |
99 /** |
99 /** |
100 * Generate object trees. |
100 * Generate object trees. |
101 */ |
101 */ |
102 Node[] genTrees(int depth, int ntrees) { |
102 Node[] genTrees(int depth, int ntrees) { |
103 Node[] trees = new Node[ntrees]; |
103 Node[] trees = new Node[ntrees]; |
104 for (int i = 0; i < ntrees; i++) { |
104 for (int i = 0; i < ntrees; i++) { |
105 trees[i] = new Node(null, depth); |
105 trees[i] = new Node(null, depth); |
106 } |
106 } |
107 return trees; |
107 return trees; |
108 } |
108 } |
109 |
109 |
110 /** |
110 /** |
111 * Run benchmark for given number of batches, with each batch containing |
111 * Run benchmark for given number of batches, with each batch containing |
112 * the given number of cycles. |
112 * the given number of cycles. |
113 */ |
113 */ |
114 void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
114 void doReps(ObjectOutputStream oout, ObjectInputStream oin, |
115 StreamBuffer sbuf, Node[] trees, int nbatches) |
115 StreamBuffer sbuf, Node[] trees, int nbatches) |
116 throws Exception |
116 throws Exception |
117 { |
117 { |
118 int ncycles = trees.length; |
118 int ncycles = trees.length; |
119 for (int i = 0; i < nbatches; i++) { |
119 for (int i = 0; i < nbatches; i++) { |
120 sbuf.reset(); |
120 sbuf.reset(); |
121 oout.reset(); |
121 oout.reset(); |
122 for (int j = 0; j < ncycles; j++) { |
122 for (int j = 0; j < ncycles; j++) { |
123 oout.writeObject(trees[j]); |
123 oout.writeObject(trees[j]); |
124 } |
124 } |
125 oout.flush(); |
125 oout.flush(); |
126 for (int j = 0; j < ncycles; j++) { |
126 for (int j = 0; j < ncycles; j++) { |
127 oin.readObject(); |
127 oin.readObject(); |
128 } |
128 } |
129 } |
129 } |
130 } |
130 } |
131 } |
131 } |
132 |
|