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 a tree of small objects.
37 */
38public class SmallObjTrees implements Benchmark {
39
40    static class Node implements Serializable {
41        Object parent, left, right;
42
43        Node(Object parent, int depth) {
44            this.parent = parent;
45            if (depth > 0) {
46                left = new Node(this, depth - 1);
47                right = new Node(this, depth - 1);
48            }
49        }
50    }
51
52    /**
53     * Write and read a tree of small objects from a stream.  The benchmark is
54     * run in batches: each "batch" consists of a fixed number of read/write
55     * cycles, and the stream is flushed (and underlying stream buffer cleared)
56     * in between each batch.
57     * Arguments: <tree depth> <# batches> <# cycles per batch>
58     */
59    public long run(String[] args) throws Exception {
60        int depth = Integer.parseInt(args[0]);
61        int nbatches = Integer.parseInt(args[1]);
62        int ncycles = Integer.parseInt(args[2]);
63        Node[] trees = genTrees(depth, ncycles);
64        StreamBuffer sbuf = new StreamBuffer();
65        ObjectOutputStream oout =
66            new ObjectOutputStream(sbuf.getOutputStream());
67        ObjectInputStream oin =
68            new ObjectInputStream(sbuf.getInputStream());
69
70        doReps(oout, oin, sbuf, trees, 1);      // warmup
71
72        long start = System.currentTimeMillis();
73        doReps(oout, oin, sbuf, trees, nbatches);
74        return System.currentTimeMillis() - start;
75    }
76
77    /**
78     * Generate object trees.
79     */
80    Node[] genTrees(int depth, int ntrees) {
81        Node[] trees = new Node[ntrees];
82        for (int i = 0; i < ntrees; i++) {
83            trees[i] = new Node(null, depth);
84        }
85        return trees;
86    }
87
88    /**
89     * Run benchmark for given number of batches, with each batch containing the
90     * given number of cycles.
91     */
92    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
93                StreamBuffer sbuf, Node[] trees, int nbatches)
94        throws Exception
95    {
96        int ncycles = trees.length;
97        for (int i = 0; i < nbatches; i++) {
98            sbuf.reset();
99            oout.reset();
100            for (int j = 0; j < ncycles; j++) {
101                oout.writeObject(trees[j]);
102            }
103            oout.flush();
104            for (int j = 0; j < ncycles; j++) {
105                oin.readObject();
106            }
107        }
108    }
109}
110