Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/GetPutFieldTrees.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 explicit writeObject() and readObject() methods which use the38* GetField()/PutField() API.39*/40public class GetPutFieldTrees 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 {63ObjectOutputStream.PutField fields = out.putFields();64fields.put("z", z);65fields.put("b", b);66fields.put("c", c);67fields.put("s", s);68fields.put("i", i);69fields.put("f", f);70fields.put("j", j);71fields.put("d", d);72fields.put("str", str);73fields.put("parent", parent);74fields.put("left", left);75fields.put("right", right);76out.writeFields();77}7879private void readObject(ObjectInputStream in)80throws IOException, ClassNotFoundException81{82ObjectInputStream.GetField fields = in.readFields();83z = fields.get("z", false);84b = fields.get("b", (byte) 0);85c = fields.get("c", (char) 0);86s = fields.get("s", (short) 0);87i = fields.get("i", (int) 0);88f = fields.get("f", (float) 0.0);89j = fields.get("j", (long) 0);90d = fields.get("d", (double) 0.0);91str = (String) fields.get("str", null);92parent = fields.get("parent", null);93left = fields.get("left", null);94right = fields.get("right", null);95}96}9798/**99* Write and read a tree of objects from a stream. The benchmark is run in100* batches: each "batch" consists of a fixed number of read/write cycles,101* and the stream is flushed (and underlying stream buffer cleared) in102* between each batch.103* Arguments: <tree depth> <# batches> <# cycles per batch>104*/105public long run(String[] args) throws Exception {106int depth = Integer.parseInt(args[0]);107int nbatches = Integer.parseInt(args[1]);108int ncycles = Integer.parseInt(args[2]);109Node[] trees = genTrees(depth, ncycles);110StreamBuffer sbuf = new StreamBuffer();111ObjectOutputStream oout =112new ObjectOutputStream(sbuf.getOutputStream());113ObjectInputStream oin =114new ObjectInputStream(sbuf.getInputStream());115116doReps(oout, oin, sbuf, trees, 1); // warmup117118long start = System.currentTimeMillis();119doReps(oout, oin, sbuf, trees, nbatches);120return System.currentTimeMillis() - start;121}122123/**124* Generate object trees.125*/126Node[] genTrees(int depth, int ntrees) {127Node[] trees = new Node[ntrees];128for (int i = 0; i < ntrees; i++) {129trees[i] = new Node(null, depth);130}131return trees;132}133134/**135* Run benchmark for given number of batches, with each batch containing136* the given number of cycles.137*/138void doReps(ObjectOutputStream oout, ObjectInputStream oin,139StreamBuffer sbuf, Node[] trees, int nbatches)140throws Exception141{142int ncycles = trees.length;143for (int i = 0; i < nbatches; i++) {144sbuf.reset();145oout.reset();146for (int j = 0; j < ncycles; j++) {147oout.writeObject(trees[j]);148}149oout.flush();150for (int j = 0; j < ncycles; j++) {151oin.readObject();152}153}154}155}156157158