Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/SmallObjTrees.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 small objects.36*/37public class SmallObjTrees implements Benchmark {3839static class Node implements Serializable {40Object parent, left, right;4142Node(Object parent, int depth) {43this.parent = parent;44if (depth > 0) {45left = new Node(this, depth - 1);46right = new Node(this, depth - 1);47}48}49}5051/**52* Write and read a tree of small objects from a stream. The benchmark is53* run in batches: each "batch" consists of a fixed number of read/write54* cycles, and the stream is flushed (and underlying stream buffer cleared)55* in between each batch.56* Arguments: <tree depth> <# batches> <# cycles per batch>57*/58public long run(String[] args) throws Exception {59int depth = Integer.parseInt(args[0]);60int nbatches = Integer.parseInt(args[1]);61int ncycles = Integer.parseInt(args[2]);62Node[] trees = genTrees(depth, ncycles);63StreamBuffer sbuf = new StreamBuffer();64ObjectOutputStream oout =65new ObjectOutputStream(sbuf.getOutputStream());66ObjectInputStream oin =67new ObjectInputStream(sbuf.getInputStream());6869doReps(oout, oin, sbuf, trees, 1); // warmup7071long start = System.currentTimeMillis();72doReps(oout, oin, sbuf, trees, nbatches);73return System.currentTimeMillis() - start;74}7576/**77* Generate object trees.78*/79Node[] genTrees(int depth, int ntrees) {80Node[] trees = new Node[ntrees];81for (int i = 0; i < ntrees; i++) {82trees[i] = new Node(null, depth);83}84return trees;85}8687/**88* Run benchmark for given number of batches, with each batch containing the89* given number of cycles.90*/91void doReps(ObjectOutputStream oout, ObjectInputStream oin,92StreamBuffer sbuf, Node[] trees, int nbatches)93throws Exception94{95int ncycles = trees.length;96for (int i = 0; i < nbatches; i++) {97sbuf.reset();98oout.reset();99for (int j = 0; j < ncycles; j++) {100oout.writeObject(trees[j]);101}102oout.flush();103for (int j = 0; j < ncycles; j++) {104oin.readObject();105}106}107}108}109110111