ObjArrays.java revision 381:b6d6877c1155
1/*
2 * Copyright 1999-2008 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.ObjectInputStream;
32import java.io.ObjectOutputStream;
33import java.io.Serializable;
34
35/**
36 * Benchmark for testing speed of object array reads/writes.
37 */
38public class ObjArrays 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 object arrays to/from a stream.  The benchmark is run in
63     * batches, with each batch consisting of a fixed number of read/write
64     * cycles.  The ObjectOutputStream is reset after each batch of cycles has
65     * completed.
66     * Arguments: <array size> <# batches> <# cycles per batch>
67     */
68    public long run(String[] args) throws Exception {
69        int size = Integer.parseInt(args[0]);
70        int nbatches = Integer.parseInt(args[1]);
71        int ncycles = Integer.parseInt(args[2]);
72        Node[][] arrays = genArrays(size, 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, arrays, 1);     // warmup
80
81        long start = System.currentTimeMillis();
82        doReps(oout, oin, sbuf, arrays, nbatches);
83        return System.currentTimeMillis() - start;
84    }
85
86    /**
87     * Generate object arrays.
88     */
89    Node[][] genArrays(int size, int narrays) {
90        Node[][] arrays = new Node[narrays][size];
91        for (int i = 0; i < narrays; i++) {
92            for (int j = 0; j < size; j++) {
93                arrays[i][j] = new Node(null, 0);
94            }
95        }
96        return arrays;
97    }
98
99    /**
100     * Run benchmark for given number of batches, with given number of cycles
101     * for each batch.
102     */
103    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
104                StreamBuffer sbuf, Node[][] arrays, int nbatches)
105        throws Exception
106    {
107        int ncycles = arrays.length;
108        for (int i = 0; i < nbatches; i++) {
109            sbuf.reset();
110            oout.reset();
111            for (int j = 0; j < ncycles; j++) {
112                oout.writeObject(arrays[j]);
113            }
114            oout.flush();
115            for (int j = 0; j < ncycles; j++) {
116                oin.readObject();
117            }
118        }
119    }
120}
121