Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/CustomDefaultObjTrees.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 that call38* defaultWriteObject() and defaultReadObject().39*/40public class CustomDefaultObjTrees implements Benchmark {4142static class Node implements Serializable {43boolean z;44byte b;45char c;46short s;47int i;48float f;49long j;50double d;51String str = "bodega";52Object parent, left, right;5354Node(Object parent, int depth) {55this.parent = parent;56if (depth > 0) {57left = new Node(this, depth - 1);58right = new Node(this, depth - 1);59}60}6162private void writeObject(ObjectOutputStream out) throws IOException {63out.defaultWriteObject();64}6566private void readObject(ObjectInputStream in)67throws IOException, ClassNotFoundException68{69in.defaultReadObject();70}71}7273/**74* Write and read a tree of objects from a stream. The benchmark is run in75* batches: each "batch" consists of a fixed number of read/write cycles,76* and the stream is flushed (and underlying stream buffer cleared) in77* between each batch.78* Arguments: <tree depth> <# batches> <# cycles per batch>79*/80public long run(String[] args) throws Exception {81int depth = Integer.parseInt(args[0]);82int nbatches = Integer.parseInt(args[1]);83int ncycles = Integer.parseInt(args[2]);84Node[] trees = genTrees(depth, ncycles);85StreamBuffer sbuf = new StreamBuffer();86ObjectOutputStream oout =87new ObjectOutputStream(sbuf.getOutputStream());88ObjectInputStream oin =89new ObjectInputStream(sbuf.getInputStream());9091doReps(oout, oin, sbuf, trees, 1); // warmup9293long start = System.currentTimeMillis();94doReps(oout, oin, sbuf, trees, nbatches);95return System.currentTimeMillis() - start;96}9798/**99* Generate object trees.100*/101Node[] genTrees(int depth, int ntrees) {102Node[] trees = new Node[ntrees];103for (int i = 0; i < ntrees; i++) {104trees[i] = new Node(null, depth);105}106return trees;107}108109/**110* Run benchmark for given number of batches, with each batch containing111* the given number of cycles.112*/113void doReps(ObjectOutputStream oout, ObjectInputStream oin,114StreamBuffer sbuf, Node[] trees, int nbatches)115throws Exception116{117int ncycles = trees.length;118for (int i = 0; i < nbatches; i++) {119sbuf.reset();120oout.reset();121for (int j = 0; j < ncycles; j++) {122oout.writeObject(trees[j]);123}124oout.flush();125for (int j = 0; j < ncycles; j++) {126oin.readObject();127}128}129}130}131132133