Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ProxyArrays.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;33import java.lang.reflect.Constructor;34import java.lang.reflect.InvocationHandler;35import java.lang.reflect.Method;36import java.lang.reflect.Proxy;3738/**39* Benchmark for testing speed of proxy array reads/writes.40*/41public class ProxyArrays implements Benchmark {4243static class DummyHandler implements InvocationHandler, Serializable {44public Object invoke(Object proxy, Method method, Object[] args)45throws Throwable46{47return null;48}49}5051static interface DummyInterface {52public void foo();53}5455/**56* Write and read proxy arrays to/from a stream. The benchmark is run in57* batches, with each batch consisting of a fixed number of read/write58* cycles. The ObjectOutputStream is reset after each batch of cycles has59* completed.60* Arguments: <array size> <# batches> <# cycles per batch>61*/62public long run(String[] args) throws Exception {63int size = Integer.parseInt(args[0]);64int nbatches = Integer.parseInt(args[1]);65int ncycles = Integer.parseInt(args[2]);66Proxy[][] arrays = genArrays(size, ncycles);67StreamBuffer sbuf = new StreamBuffer();68ObjectOutputStream oout =69new ObjectOutputStream(sbuf.getOutputStream());70ObjectInputStream oin =71new ObjectInputStream(sbuf.getInputStream());7273doReps(oout, oin, sbuf, arrays, 1); // warmup7475long start = System.currentTimeMillis();76doReps(oout, oin, sbuf, arrays, nbatches);77return System.currentTimeMillis() - start;78}7980/**81* Generate proxy arrays.82*/83Proxy[][] genArrays(int size, int narrays) throws Exception {84Class proxyClass =85Proxy.getProxyClass(DummyInterface.class.getClassLoader(),86new Class[] { DummyInterface.class });87Constructor proxyCons =88proxyClass.getConstructor(new Class[] { InvocationHandler.class });89Object[] consArgs = new Object[] { new DummyHandler() };90Proxy[][] arrays = new Proxy[narrays][size];91for (int i = 0; i < narrays; i++) {92for (int j = 0; j < size; j++) {93arrays[i][j] = (Proxy) proxyCons.newInstance(consArgs);94}95}96return arrays;97}9899/**100* Run benchmark for given number of batches, with given number of cycles101* for each batch.102*/103void doReps(ObjectOutputStream oout, ObjectInputStream oin,104StreamBuffer sbuf, Proxy[][] arrays, int nbatches)105throws Exception106{107int ncycles = arrays.length;108for (int i = 0; i < nbatches; i++) {109sbuf.reset();110oout.reset();111for (int j = 0; j < ncycles; j++) {112oout.writeObject(arrays[j]);113}114oout.flush();115for (int j = 0; j < ncycles; j++) {116oin.readObject();117}118}119}120}121122123