Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ObjTrees.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 writes and reads of an object tree.36*/37public class ObjTrees implements Benchmark {3839static class Node implements Serializable {40boolean z;41byte b;42char c;43short s;44int i;45float f;46long j;47double d;48String str = "bodega";49Object parent, left, right;5051Node(Object parent, int depth) {52this.parent = parent;53if (depth > 0) {54left = new Node(this, depth - 1);55right = new Node(this, depth - 1);56}57}58}5960/**61* Write and read a tree of objects from a stream. The benchmark is run in62* batches: each "batch" consists of a fixed number of read/write cycles,63* and the stream is flushed (and underlying stream buffer cleared) in64* between each batch.65* Arguments: <tree depth> <# batches> <# cycles per batch>66*/67public long run(String[] args) throws Exception {68int depth = Integer.parseInt(args[0]);69int nbatches = Integer.parseInt(args[1]);70int ncycles = Integer.parseInt(args[2]);71Node[] trees = genTrees(depth, ncycles);72StreamBuffer sbuf = new StreamBuffer();73ObjectOutputStream oout =74new ObjectOutputStream(sbuf.getOutputStream());75ObjectInputStream oin =76new ObjectInputStream(sbuf.getInputStream());7778doReps(oout, oin, sbuf, trees, 1); // warmup7980long start = System.currentTimeMillis();81doReps(oout, oin, sbuf, trees, nbatches);82return System.currentTimeMillis() - start;83}8485/**86* Generate object trees.87*/88Node[] genTrees(int depth, int ntrees) {89Node[] trees = new Node[ntrees];90for (int i = 0; i < ntrees; i++) {91trees[i] = new Node(null, depth);92}93return trees;94}9596/**97* Run benchmark for given number of batches, with each batch containing98* the given number of cycles.99*/100void doReps(ObjectOutputStream oout, ObjectInputStream oin,101StreamBuffer sbuf, Node[] trees, int nbatches)102throws Exception103{104int ncycles = trees.length;105for (int i = 0; i < nbatches; i++) {106sbuf.reset();107oout.reset();108for (int j = 0; j < ncycles; j++) {109oout.writeObject(trees[j]);110}111oout.flush();112for (int j = 0; j < ncycles; j++) {113oin.readObject();114}115}116}117}118119120