Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ExternObjTrees.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.Externalizable;31import java.io.IOException;32import java.io.ObjectInput;33import java.io.ObjectInputStream;34import java.io.ObjectOutput;35import java.io.ObjectOutputStream;3637/**38* Benchmark for testing speed of writes and reads of a tree of Externalizable39* objects.40*/41public class ExternObjTrees implements Benchmark {4243static class Node implements Externalizable {44boolean z;45byte b;46char c;47short s;48int i;49float f;50long j;51double d;52String str = "bodega";53Object parent, left, right;5455Node(Object parent, int depth) {56this.parent = parent;57if (depth > 0) {58left = new Node(this, depth - 1);59right = new Node(this, depth - 1);60}61}6263public Node() {64}6566public void writeExternal(ObjectOutput out) throws IOException {67out.writeBoolean(z);68out.writeByte(b);69out.writeChar(c);70out.writeShort(s);71out.writeInt(i);72out.writeFloat(f);73out.writeLong(j);74out.writeDouble(d);75out.writeObject(str);76out.writeObject(parent);77out.writeObject(left);78out.writeObject(right);79}8081public void readExternal(ObjectInput in)82throws IOException, ClassNotFoundException83{84z = in.readBoolean();85b = in.readByte();86c = in.readChar();87s = in.readShort();88i = in.readInt();89f = in.readFloat();90j = in.readLong();91d = in.readDouble();92str = (String) in.readObject();93parent = in.readObject();94left = in.readObject();95right = in.readObject();96}97}9899/**100* Write and read a tree of externalizable objects from a stream. The101* benchmark is run in batches: each "batch" consists of a fixed number of102* read/write cycles, and the stream is flushed (and underlying stream103* buffer cleared) in between each batch.104* Arguments: <tree depth> <# batches> <# cycles per batch>105*/106public long run(String[] args) throws Exception {107int depth = Integer.parseInt(args[0]);108int nbatches = Integer.parseInt(args[1]);109int ncycles = Integer.parseInt(args[2]);110Node[] trees = genTrees(depth, ncycles);111StreamBuffer sbuf = new StreamBuffer();112ObjectOutputStream oout =113new ObjectOutputStream(sbuf.getOutputStream());114ObjectInputStream oin =115new ObjectInputStream(sbuf.getInputStream());116117doReps(oout, oin, sbuf, trees, 1); // warmup118119long start = System.currentTimeMillis();120doReps(oout, oin, sbuf, trees, nbatches);121return System.currentTimeMillis() - start;122}123124/**125* Generate object trees.126*/127Node[] genTrees(int depth, int ntrees) {128Node[] trees = new Node[ntrees];129for (int i = 0; i < ntrees; i++) {130trees[i] = new Node(null, depth);131}132return trees;133}134135/**136* Run benchmark for given number of batches, with each batch containing137* the given number of cycles.138*/139void doReps(ObjectOutputStream oout, ObjectInputStream oin,140StreamBuffer sbuf, Node[] trees, int nbatches)141throws Exception142{143int ncycles = trees.length;144for (int i = 0; i < nbatches; i++) {145sbuf.reset();146oout.reset();147for (int j = 0; j < ncycles; j++) {148oout.writeObject(trees[j]);149}150oout.flush();151for (int j = 0; j < ncycles; j++) {152oin.readObject();153}154}155}156}157158159