Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/Bytes.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 reads/writes.35*/36public class Bytes implements Benchmark {3738/**39* Write and read byte values to/from a stream. The benchmark is run in40* batches: each "batch" consists of a fixed number of read/write cycles,41* and the stream is flushed (and underlying stream buffer cleared) in42* between each batch.43* Arguments: <# batches> <# cycles per batch>44*/45public long run(String[] args) throws Exception {46int nbatches = Integer.parseInt(args[0]);47int ncycles = Integer.parseInt(args[1]);48StreamBuffer sbuf = new StreamBuffer();49ObjectOutputStream oout =50new ObjectOutputStream(sbuf.getOutputStream());51ObjectInputStream oin =52new ObjectInputStream(sbuf.getInputStream());5354doReps(oout, oin, sbuf, 1, ncycles); // warmup5556long start = System.currentTimeMillis();57doReps(oout, oin, sbuf, nbatches, ncycles);58return System.currentTimeMillis() - start;59}6061/**62* Run benchmark for given number of batches, with given number of cycles63* for each batch.64*/65void doReps(ObjectOutputStream oout, ObjectInputStream oin,66StreamBuffer sbuf, int nbatches, int ncycles)67throws Exception68{69for (int i = 0; i < nbatches; i++) {70sbuf.reset();71for (int j = 0; j < ncycles; j++) {72oout.writeByte(0);73}74oout.flush();75for (int j = 0; j < ncycles; j++) {76oin.readByte();77}78}79}80}818283