1/*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * 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 an object tree.
37 */
38public class ObjTrees implements Benchmark {
39
40    static class Node implements Serializable {
41        boolean z;
42        byte b;
43        char c;
44        short s;
45        int i;
46        float f;
47        long j;
48        double d;
49        String str = "bodega";
50        Object parent, left, right;
51
52        Node(Object parent, int depth) {
53            this.parent = parent;
54            if (depth > 0) {
55                left = new Node(this, depth - 1);
56                right = new Node(this, depth - 1);
57            }
58        }
59    }
60
61    /**
62     * Write and read a tree of objects from a stream.  The benchmark is run in
63     * batches: each "batch" consists of a fixed number of read/write cycles,
64     * and the stream is flushed (and underlying stream buffer cleared) in
65     * between each batch.
66     * Arguments: <tree depth> <# batches> <# cycles per batch>
67     */
68    public long run(String[] args) throws Exception {
69        int depth = Integer.parseInt(args[0]);
70        int nbatches = Integer.parseInt(args[1]);
71        int ncycles = Integer.parseInt(args[2]);
72        Node[] trees = genTrees(depth, ncycles);
73        StreamBuffer sbuf = new StreamBuffer();
74        ObjectOutputStream oout =
75            new ObjectOutputStream(sbuf.getOutputStream());
76        ObjectInputStream oin =
77            new ObjectInputStream(sbuf.getInputStream());
78
79        doReps(oout, oin, sbuf, trees, 1);      // warmup
80
81        long start = System.currentTimeMillis();
82        doReps(oout, oin, sbuf, trees, nbatches);
83        return System.currentTimeMillis() - start;
84    }
85
86    /**
87     * Generate object trees.
88     */
89    Node[] genTrees(int depth, int ntrees) {
90        Node[] trees = new Node[ntrees];
91        for (int i = 0; i < ntrees; i++) {
92            trees[i] = new Node(null, depth);
93        }
94        return trees;
95    }
96
97    /**
98     * Run benchmark for given number of batches, with each batch containing
99     * the given number of cycles.
100     */
101    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
102                StreamBuffer sbuf, Node[] trees, int nbatches)
103        throws Exception
104    {
105        int ncycles = trees.length;
106        for (int i = 0; i < nbatches; i++) {
107            sbuf.reset();
108            oout.reset();
109            for (int j = 0; j < ncycles; j++) {
110                oout.writeObject(trees[j]);
111            }
112            oout.flush();
113            for (int j = 0; j < ncycles; j++) {
114                oin.readObject();
115            }
116        }
117    }
118}
119