Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/CustomObjTrees.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.IOException;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.io.Serializable;3435/**36* Benchmark for testing speed of writes and reads of an object tree, where37* nodes contain custom writeObject() and readObject() methods.38*/39public class CustomObjTrees implements Benchmark {4041static class Node implements Serializable {42boolean z;43byte b;44char c;45short s;46int i;47float f;48long j;49double d;50String str = "bodega";51Object parent, left, right;5253Node(Object parent, int depth) {54this.parent = parent;55if (depth > 0) {56left = new Node(this, depth - 1);57right = new Node(this, depth - 1);58}59}6061private void writeObject(ObjectOutputStream out) throws IOException {62out.writeBoolean(z);63out.writeByte(b);64out.writeChar(c);65out.writeShort(s);66out.writeInt(i);67out.writeFloat(f);68out.writeLong(j);69out.writeDouble(d);70out.writeObject(str);71out.writeObject(parent);72out.writeObject(left);73out.writeObject(right);74}7576private void readObject(ObjectInputStream in)77throws IOException, ClassNotFoundException78{79z = in.readBoolean();80b = in.readByte();81c = in.readChar();82s = in.readShort();83i = in.readInt();84f = in.readFloat();85j = in.readLong();86d = in.readDouble();87str = (String) in.readObject();88parent = in.readObject();89left = in.readObject();90right = in.readObject();91}92}9394/**95* Write and read a tree of objects from a stream. The benchmark is run in96* batches: each "batch" consists of a fixed number of read/write cycles,97* and the stream is flushed (and underlying stream buffer cleared) in98* between each batch.99* Arguments: <tree depth> <# batches> <# cycles per batch>100*/101public long run(String[] args) throws Exception {102int depth = Integer.parseInt(args[0]);103int nbatches = Integer.parseInt(args[1]);104int ncycles = Integer.parseInt(args[2]);105Node[] trees = genTrees(depth, ncycles);106StreamBuffer sbuf = new StreamBuffer();107ObjectOutputStream oout =108new ObjectOutputStream(sbuf.getOutputStream());109ObjectInputStream oin =110new ObjectInputStream(sbuf.getInputStream());111112doReps(oout, oin, sbuf, trees, 1); // warmup113114long start = System.currentTimeMillis();115doReps(oout, oin, sbuf, trees, nbatches);116return System.currentTimeMillis() - start;117}118119/**120* Generate object trees.121*/122Node[] genTrees(int depth, int ntrees) {123Node[] trees = new Node[ntrees];124for (int i = 0; i < ntrees; i++) {125trees[i] = new Node(null, depth);126}127return trees;128}129130/**131* Run benchmark for given number of batches, with each batch containing132* the given number of cycles.133*/134void doReps(ObjectOutputStream oout, ObjectInputStream oin,135StreamBuffer sbuf, Node[] trees, int nbatches)136throws Exception137{138int ncycles = trees.length;139for (int i = 0; i < nbatches; i++) {140sbuf.reset();141oout.reset();142for (int j = 0; j < ncycles; j++) {143oout.writeObject(trees[j]);144}145oout.flush();146for (int j = 0; j < ncycles; j++) {147oin.readObject();148}149}150}151}152153154