Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/RepeatObjs.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;32import java.io.Serializable;3334/**35* Benchmark for testing speed of reads/writes of repeated objects.36*/37public class RepeatObjs implements Benchmark {3839static class Node implements Serializable {40}4142/**43* Write and read repeated objects to/from a stream. The benchmark is run44* for a given number of batches. Within each batch, a set of objects45* is written to and read from the stream. The set of objects remains the46* same between batches (and the serialization streams are not reset) in47* order to test the speed of object -> wire handle lookup, and vice versa.48* Arguments: <# objects> <# cycles>49*/50public long run(String[] args) throws Exception {51int size = Integer.parseInt(args[0]);52int nbatches = Integer.parseInt(args[1]);53Node[] objs = genObjs(size);54StreamBuffer sbuf = new StreamBuffer();55ObjectOutputStream oout =56new ObjectOutputStream(sbuf.getOutputStream());57ObjectInputStream oin =58new ObjectInputStream(sbuf.getInputStream());5960doReps(oout, oin, sbuf, objs, 1); // warmup6162long start = System.currentTimeMillis();63doReps(oout, oin, sbuf, objs, nbatches);64return System.currentTimeMillis() - start;65}6667/**68* Generate objects.69*/70Node[] genObjs(int nobjs) {71Node[] objs = new Node[nobjs];72for (int i = 0; i < nobjs; i++)73objs[i] = new Node();74return objs;75}7677/**78* Run benchmark for given number of batches.79*/80void doReps(ObjectOutputStream oout, ObjectInputStream oin,81StreamBuffer sbuf, Node[] objs, int nbatches)82throws Exception83{84int nobjs = objs.length;85for (int i = 0; i < nbatches; i++) {86sbuf.reset();87for (int j = 0; j < nobjs; j++) {88oout.writeObject(objs[j]);89}90oout.flush();91for (int j = 0; j < nobjs; j++) {92oin.readObject();93}94}95}96}979899