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 boolean reads/writes.
36 */
37public class Booleans implements Benchmark {
38
39    /**
40     * Write and read boolean values to/from a stream.  The benchmark is run in
41     * batches: each "batch" consists of a fixed number of read/write cycles,
42     * and the stream is flushed (and underlying stream buffer cleared) in
43     * between each batch.
44     * Arguments: <# batches> <# cycles per batch>
45     */
46    public long run(String[] args) throws Exception {
47        int nbatches = Integer.parseInt(args[0]);
48        int ncycles = Integer.parseInt(args[1]);
49        StreamBuffer sbuf = new StreamBuffer();
50        ObjectOutputStream oout =
51            new ObjectOutputStream(sbuf.getOutputStream());
52        ObjectInputStream oin =
53            new ObjectInputStream(sbuf.getInputStream());
54
55        doReps(oout, oin, sbuf, 1, ncycles);    // warmup
56
57        long start = System.currentTimeMillis();
58        doReps(oout, oin, sbuf, nbatches, ncycles);
59        return System.currentTimeMillis() - start;
60    }
61
62    /**
63     * Run benchmark for given number of batches, with given number of cycles
64     * for each batch.
65     */
66    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
67                StreamBuffer sbuf, int nbatches, int ncycles)
68        throws Exception
69    {
70        for (int i = 0; i < nbatches; i++) {
71            sbuf.reset();
72            for (int j = 0; j < ncycles; j++) {
73                oout.writeBoolean(false);
74            }
75            oout.flush();
76            for (int j = 0; j < ncycles; j++) {
77                oin.readBoolean();
78            }
79        }
80    }
81}
82