1/*
2 * Copyright (c) 2000, 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.rmi;
29
30import bench.Benchmark;
31import java.io.Serializable;
32import java.lang.reflect.Constructor;
33import java.lang.reflect.InvocationHandler;
34import java.lang.reflect.Method;
35import java.lang.reflect.Proxy;
36import java.rmi.Remote;
37import java.rmi.RemoteException;
38import java.rmi.server.UnicastRemoteObject;
39
40/**
41 * Benchmark for testing speed of calls with proxy array parameters and return
42 * values.
43 */
44public class ProxyArrayCalls implements Benchmark {
45
46    static class DummyHandler implements InvocationHandler, Serializable {
47        public Object invoke(Object proxy, Method method, Object[] args)
48            throws Throwable
49        {
50            return null;
51        }
52    }
53
54    public static interface DummyInterface {
55        public void foo();
56    }
57
58    interface Server extends Remote {
59        public Proxy[] call(Proxy[] a) throws RemoteException;
60    }
61
62    static class ServerImpl extends UnicastRemoteObject implements Server {
63        public ServerImpl() throws RemoteException {
64        }
65
66        public Proxy[] call(Proxy[] a) throws RemoteException {
67            return a;
68        }
69    }
70
71    static class ServerFactory implements BenchServer.RemoteObjectFactory {
72        public Remote create() throws RemoteException {
73            return new ServerImpl();
74        }
75    }
76
77    /**
78     * Generate proxy object array of the given size.
79     */
80    Proxy[] genProxies(int size) throws Exception {
81        Class proxyClass =
82            Proxy.getProxyClass(DummyInterface.class.getClassLoader(),
83                    new Class[] { DummyInterface.class });
84        Constructor proxyCons =
85            proxyClass.getConstructor(new Class[] { InvocationHandler.class });
86        Object[] consArgs = new Object[] { new DummyHandler() };
87        Proxy[] proxies = new Proxy[size];
88
89        for (int i = 0; i < size; i++)
90            proxies[i] = (Proxy) proxyCons.newInstance(consArgs);
91
92        return proxies;
93    }
94
95    /**
96     * Issue calls using arrays of proxies as parameters/return values.
97     * Arguments: <array size> <# calls>
98     */
99    public long run(String[] args) throws Exception {
100        int size = Integer.parseInt(args[0]);
101        int reps = Integer.parseInt(args[1]);
102        BenchServer bsrv = Main.getBenchServer();
103        Server stub = (Server) bsrv.create(new ServerFactory());
104        Proxy[] proxies = genProxies(size);
105
106        long start = System.currentTimeMillis();
107        for (int i = 0; i < reps; i++)
108            stub.call(proxies);
109        long time = System.currentTimeMillis() - start;
110
111        bsrv.unexport(stub, true);
112        return time;
113    }
114}
115