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 reads/writes of repeated objects.
37 */
38public class RepeatObjs implements Benchmark {
39
40    static class Node implements Serializable {
41    }
42
43    /**
44     * Write and read repeated objects to/from a stream.  The benchmark is run
45     * for a given number of batches.  Within each batch, a set of objects
46     * is written to and read from the stream.  The set of objects remains the
47     * same between batches (and the serialization streams are not reset) in
48     * order to test the speed of object -> wire handle lookup, and vice versa.
49     * Arguments: <# objects> <# cycles>
50     */
51    public long run(String[] args) throws Exception {
52        int size = Integer.parseInt(args[0]);
53        int nbatches = Integer.parseInt(args[1]);
54        Node[] objs = genObjs(size);
55        StreamBuffer sbuf = new StreamBuffer();
56        ObjectOutputStream oout =
57            new ObjectOutputStream(sbuf.getOutputStream());
58        ObjectInputStream oin =
59            new ObjectInputStream(sbuf.getInputStream());
60
61        doReps(oout, oin, sbuf, objs, 1);       // warmup
62
63        long start = System.currentTimeMillis();
64        doReps(oout, oin, sbuf, objs, nbatches);
65        return System.currentTimeMillis() - start;
66    }
67
68    /**
69     * Generate objects.
70     */
71    Node[] genObjs(int nobjs) {
72        Node[] objs = new Node[nobjs];
73        for (int i = 0; i < nobjs; i++)
74            objs[i] = new Node();
75        return objs;
76    }
77
78    /**
79     * Run benchmark for given number of batches.
80     */
81    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
82                StreamBuffer sbuf, Node[] objs, int nbatches)
83        throws Exception
84    {
85        int nobjs = objs.length;
86        for (int i = 0; i < nbatches; i++) {
87            sbuf.reset();
88            for (int j = 0; j < nobjs; j++) {
89                oout.writeObject(objs[j]);
90            }
91            oout.flush();
92            for (int j = 0; j < nobjs; j++) {
93                oin.readObject();
94            }
95        }
96    }
97}
98