Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ReplaceTrees.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 a tree of replaceable36* objects.37*/38public class ReplaceTrees implements Benchmark {3940static class Node implements Serializable {41Object parent, left, right;4243Node(Object parent, Object left, Object right) {44this.parent = parent;45this.left = left;46this.right = right;47}4849Node(Object parent, int depth) {50this.parent = parent;51if (depth > 0) {52left = new Node(this, depth - 1);53right = new Node(this, depth - 1);54}55}5657Object writeReplace() {58return new RepNode(parent, left, right);59}60}6162static class RepNode implements Serializable {63Object parent, left, right;6465RepNode(Object parent, Object left, Object right) {66this.parent = parent;67this.left = left;68this.right = right;69}7071Object readResolve() {72return new Node(parent, left, right);73}74}7576/**77* Write and read a tree of replaceable objects from a stream. The78* benchmark is run in batches: each "batch" consists of a fixed number of79* read/write cycles, and the stream is flushed (and underlying stream80* buffer cleared) in between each batch.81* Arguments: <tree depth> <# batches> <# cycles per batch>82*/83public long run(String[] args) throws Exception {84int depth = Integer.parseInt(args[0]);85int nbatches = Integer.parseInt(args[1]);86int ncycles = Integer.parseInt(args[2]);87Node[] trees = genTrees(depth, ncycles);88StreamBuffer sbuf = new StreamBuffer();89ObjectOutputStream oout =90new ObjectOutputStream(sbuf.getOutputStream());91ObjectInputStream oin =92new ObjectInputStream(sbuf.getInputStream());9394doReps(oout, oin, sbuf, trees, 1); // warmup9596long start = System.currentTimeMillis();97doReps(oout, oin, sbuf, trees, nbatches);98return System.currentTimeMillis() - start;99}100101/**102* Generate object trees.103*/104Node[] genTrees(int depth, int ntrees) {105Node[] trees = new Node[ntrees];106for (int i = 0; i < ntrees; i++) {107trees[i] = new Node(null, depth);108}109return trees;110}111112/**113* Run benchmark for given number of batches, with each batch containing114* the given number of cycles.115*/116void doReps(ObjectOutputStream oout, ObjectInputStream oin,117StreamBuffer sbuf, Node[] trees, int nbatches)118throws Exception119{120int ncycles = trees.length;121for (int i = 0; i < nbatches; i++) {122sbuf.reset();123oout.reset();124for (int j = 0; j < ncycles; j++) {125oout.writeObject(trees[j]);126}127oout.flush();128for (int j = 0; j < ncycles; j++) {129oin.readObject();130}131}132}133}134135136