Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/Cons.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.InputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.io.OutputStream;34import java.io.Serializable;3536/**37* Benchmark for testing speed of ObjectOutputStream/ObjectInputStream38* construction.39*/40public class Cons implements Benchmark {4142/**43* Dummy object to write to newly constructed serialization stream.44*/45static class Dummy implements Serializable {46}4748/**49* Repeatedly construct ObjectOutputStream and ObjectInputStream objects.50* Arguments: <# repetitions>51*/52public long run(String[] args) throws Exception {53int reps = Integer.parseInt(args[0]);54Dummy dummy = new Dummy();55StreamBuffer sbuf = new StreamBuffer();5657doReps(sbuf, dummy, 1); // warmup5859long start = System.currentTimeMillis();60doReps(sbuf, dummy, reps);61return System.currentTimeMillis() - start;62}6364/**65* Run benchmark for given number of cycles.66*/67void doReps(StreamBuffer sbuf, Dummy dummy, int reps) throws Exception {68OutputStream out = sbuf.getOutputStream();69InputStream in = sbuf.getInputStream();70for (int i = 0; i < reps; i++) {71sbuf.reset();72ObjectOutputStream oout = new ObjectOutputStream(out);73oout.writeObject(dummy);74oout.flush();75ObjectInputStream oin = new ObjectInputStream(in);76oin.readObject();77}78}79}808182