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;
33
34/**
35 * Benchmark for testing speed of short array reads/writes.
36 */
37public class ShortArrays implements Benchmark {
38
39    /**
40     * Write and read short arrays to/from a stream.  The benchmark is run in
41     * batches, with each batch consisting of a fixed number of read/write
42     * cycles.  The ObjectOutputStream is reset after each batch of cycles has
43     * completed.
44     * Arguments: <array size> <# batches> <# cycles per batch>
45     */
46    public long run(String[] args) throws Exception {
47        int size = Integer.parseInt(args[0]);
48        int nbatches = Integer.parseInt(args[1]);
49        int ncycles = Integer.parseInt(args[2]);
50        short[][] arrays = new short[ncycles][size];
51        StreamBuffer sbuf = new StreamBuffer();
52        ObjectOutputStream oout =
53            new ObjectOutputStream(sbuf.getOutputStream());
54        ObjectInputStream oin =
55            new ObjectInputStream(sbuf.getInputStream());
56
57        doReps(oout, oin, sbuf, arrays, 1);     // warmup
58
59        long start = System.currentTimeMillis();
60        doReps(oout, oin, sbuf, arrays, nbatches);
61        return System.currentTimeMillis() - start;
62    }
63
64    /**
65     * Run benchmark for given number of batches, with given number of cycles
66     * for each batch.
67     */
68    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
69                StreamBuffer sbuf, short[][] arrays, int nbatches)
70        throws Exception
71    {
72        int ncycles = arrays.length;
73        for (int i = 0; i < nbatches; i++) {
74            sbuf.reset();
75            oout.reset();
76            for (int j = 0; j < ncycles; j++) {
77                oout.writeObject(arrays[j]);
78            }
79            oout.flush();
80            for (int j = 0; j < ncycles; j++) {
81                oin.readObject();
82            }
83        }
84    }
85}
86