1/*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 *
26 */
27
28package bench.serial;
29
30import bench.Benchmark;
31import java.io.ObjectInputStream;
32import java.io.ObjectOutputStream;
33import java.io.ObjectStreamClass;
34import java.lang.reflect.InvocationHandler;
35import java.lang.reflect.Proxy;
36
37/**
38 * Benchmark for testing speed of proxy class descriptor reads/writes.
39 */
40public class ProxyClassDesc implements Benchmark {
41
42    static interface A1 {};
43    static interface A2 {};
44    static interface A3 {};
45    static interface A4 {};
46    static interface A5 {};
47    static interface B1 {};
48    static interface B2 {};
49    static interface B3 {};
50    static interface B4 {};
51    static interface B5 {};
52    static interface C1 {};
53    static interface C2 {};
54    static interface C3 {};
55    static interface C4 {};
56    static interface C5 {};
57
58    /**
59     * Write and read proxy class descriptors to/from a stream.
60     * Arguments: <# cycles>
61     */
62    public long run(String[] args) throws Exception {
63        int ncycles = Integer.parseInt(args[0]);
64        StreamBuffer sbuf = new StreamBuffer();
65        ObjectOutputStream oout =
66            new ObjectOutputStream(sbuf.getOutputStream());
67        ObjectInputStream oin =
68            new ObjectInputStream(sbuf.getInputStream());
69        ObjectStreamClass[] descs = genDescs();
70
71        doReps(oout, oin, sbuf, descs, 1);      // warmup
72
73        long start = System.currentTimeMillis();
74        doReps(oout, oin, sbuf, descs, ncycles);
75        return System.currentTimeMillis() - start;
76    }
77
78    /**
79     * Generate proxy class descriptors.
80     */
81    ObjectStreamClass[] genDescs() {
82        ClassLoader ldr = ProxyClassDesc.class.getClassLoader();
83        Class[] ifaces = new Class[3];
84        Class[] a =
85            new Class[] { A1.class, A2.class, A3.class, A4.class, A5.class };
86        Class[] b =
87            new Class[] { B1.class, B2.class, B3.class, B4.class, B5.class };
88        Class[] c =
89            new Class[] { C1.class, C2.class, C3.class, C4.class, C5.class };
90        ObjectStreamClass[] descs =
91            new ObjectStreamClass[a.length * b.length * c.length];
92        int n = 0;
93        for (int i = 0; i < a.length; i++) {
94            ifaces[0] = a[i];
95            for (int j = 0; j < b.length; j++) {
96                ifaces[1] = b[j];
97                for (int k = 0; k < c.length; k++) {
98                    ifaces[2] = c[k];
99                    Class proxyClass = Proxy.getProxyClass(ldr, ifaces);
100                    descs[n++] = ObjectStreamClass.lookup(proxyClass);
101                }
102            }
103        }
104        return descs;
105    }
106
107    /**
108     * Run benchmark for given number of cycles.
109     */
110    void doReps(ObjectOutputStream oout, ObjectInputStream oin,
111                StreamBuffer sbuf, ObjectStreamClass[] descs, int ncycles)
112        throws Exception
113    {
114        int ndescs = descs.length;
115        for (int i = 0; i < ncycles; i++) {
116            sbuf.reset();
117            oout.reset();
118            for (int j = 0; j < ndescs; j++) {
119                oout.writeObject(descs[j]);
120            }
121            oout.flush();
122            for (int j = 0; j < ndescs; j++) {
123                oin.readObject();
124            }
125        }
126    }
127}
128