Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ByteArrays.java
41162 views
/*1* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*/2627package bench.serial;2829import bench.Benchmark;30import java.io.ObjectInputStream;31import java.io.ObjectOutputStream;3233/**34* Benchmark for testing speed of byte array reads/writes.35*/36public class ByteArrays implements Benchmark {3738/**39* Write and read byte arrays to/from a stream. The benchmark is run in40* batches, with each batch consisting of a fixed number of read/write41* cycles. The ObjectOutputStream is reset after each batch of cycles has42* completed.43* Arguments: <array size> <# batches> <# cycles per batch>44*/45public long run(String[] args) throws Exception {46int size = Integer.parseInt(args[0]);47int nbatches = Integer.parseInt(args[1]);48int ncycles = Integer.parseInt(args[2]);49byte[][] arrays = new byte[ncycles][size];50StreamBuffer sbuf = new StreamBuffer();51ObjectOutputStream oout =52new ObjectOutputStream(sbuf.getOutputStream());53ObjectInputStream oin =54new ObjectInputStream(sbuf.getInputStream());5556doReps(oout, oin, sbuf, arrays, 1); // warmup5758long start = System.currentTimeMillis();59doReps(oout, oin, sbuf, arrays, nbatches);60return System.currentTimeMillis() - start;61}6263/**64* Run benchmark for given number of batches, with given number of cycles65* for each batch.66*/67void doReps(ObjectOutputStream oout, ObjectInputStream oin,68StreamBuffer sbuf, byte[][] arrays, int nbatches)69throws Exception70{71int ncycles = arrays.length;72for (int i = 0; i < nbatches; i++) {73sbuf.reset();74oout.reset();75for (int j = 0; j < ncycles; j++) {76oout.writeObject(arrays[j]);77}78oout.flush();79for (int j = 0; j < ncycles; j++) {80oin.readObject();81}82}83}84}858687