Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/ProxyClassDesc.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.ObjectStreamClass;33import java.lang.reflect.InvocationHandler;34import java.lang.reflect.Proxy;3536/**37* Benchmark for testing speed of proxy class descriptor reads/writes.38*/39public class ProxyClassDesc implements Benchmark {4041static interface A1 {};42static interface A2 {};43static interface A3 {};44static interface A4 {};45static interface A5 {};46static interface B1 {};47static interface B2 {};48static interface B3 {};49static interface B4 {};50static interface B5 {};51static interface C1 {};52static interface C2 {};53static interface C3 {};54static interface C4 {};55static interface C5 {};5657/**58* Write and read proxy class descriptors to/from a stream.59* Arguments: <# cycles>60*/61public long run(String[] args) throws Exception {62int ncycles = Integer.parseInt(args[0]);63StreamBuffer sbuf = new StreamBuffer();64ObjectOutputStream oout =65new ObjectOutputStream(sbuf.getOutputStream());66ObjectInputStream oin =67new ObjectInputStream(sbuf.getInputStream());68ObjectStreamClass[] descs = genDescs();6970doReps(oout, oin, sbuf, descs, 1); // warmup7172long start = System.currentTimeMillis();73doReps(oout, oin, sbuf, descs, ncycles);74return System.currentTimeMillis() - start;75}7677/**78* Generate proxy class descriptors.79*/80ObjectStreamClass[] genDescs() {81ClassLoader ldr = ProxyClassDesc.class.getClassLoader();82Class[] ifaces = new Class[3];83Class[] a =84new Class[] { A1.class, A2.class, A3.class, A4.class, A5.class };85Class[] b =86new Class[] { B1.class, B2.class, B3.class, B4.class, B5.class };87Class[] c =88new Class[] { C1.class, C2.class, C3.class, C4.class, C5.class };89ObjectStreamClass[] descs =90new ObjectStreamClass[a.length * b.length * c.length];91int n = 0;92for (int i = 0; i < a.length; i++) {93ifaces[0] = a[i];94for (int j = 0; j < b.length; j++) {95ifaces[1] = b[j];96for (int k = 0; k < c.length; k++) {97ifaces[2] = c[k];98Class proxyClass = Proxy.getProxyClass(ldr, ifaces);99descs[n++] = ObjectStreamClass.lookup(proxyClass);100}101}102}103return descs;104}105106/**107* Run benchmark for given number of cycles.108*/109void doReps(ObjectOutputStream oout, ObjectInputStream oin,110StreamBuffer sbuf, ObjectStreamClass[] descs, int ncycles)111throws Exception112{113int ndescs = descs.length;114for (int i = 0; i < ncycles; i++) {115sbuf.reset();116oout.reset();117for (int j = 0; j < ndescs; j++) {118oout.writeObject(descs[j]);119}120oout.flush();121for (int j = 0; j < ndescs; j++) {122oin.readObject();123}124}125}126}127128129