[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Using Dynamic Proxies?



I put together a simple comparison test between regular invocation and
invocation using dynamic proxies (test code below). The proxies ranged from
0x - 4x slower than regular invocation. The cost of proxies will come in
when the handler does significant work. Would it be possible to support both
post processed proxies for the speed needy and dynamic proxies for the lazy?
;)

Thanks,
Scott

-----Original Message-----
From: David Li [mailto:david@digitalsesame.com]
Sent: Friday, July 27, 2001 3:34 AM
To: ozone-dev@ozone-db.org
Subject: Re: Using Dynamic Proxies?


Falko Braeutigam wrote:

> On Wed, 25 Jul 2001, you wrote:
> 
>>I was wondering if/when ozone was moving to dynamic proxies? 
>>
> 
> AFAIK nobody is working on this. We don't even have the benchmark numbers
that
> compare dynamic against generated proxies, as we discussed it earlier.
Shouldn't
> be a real problem but unfortunately nobody is willing/able/whatever to
actually
> tackle this one. Would you like to help us out here?
> 


No solid number yet but I think this can be taken as a reference number.

For a null method call, DynamicProxy call is about 5 times slower than a 
  non final method call.

I haven't gotten arround to finish up the dynamic proxy benchmark. Will 
try again this weekend.

David Li
DigitalSesame


---Test Code---

import java.lang.reflect.*;

public class DynamicTest {
    
    public static void main(String[] args) {
        DynamicTest test = new DynamicTest();
        echo("First run");
        test.runRegular(50);
        test.runDynamic(50);
        echo("Second run");
        test.runRegular(100);
        test.runDynamic(100);             
        echo("Third Run");
        test.runRegular(1000);
        test.runDynamic(1000);   
        echo("Tests done!");
        
    }
    
    public void runRegular(int iterations) {
        long startTime = System.currentTimeMillis();
        echo("Regular - " + startTime);        
        Fi regular = new Fi();
        String in = "in";
        for (int i=0; i<iterations; i++) {
            ((Fo)regular).outSomething();
            ((Fum)regular).inSomething(in);            
        }
        echo("Regular invocation with " + iterations + " iterations took " 
            + (System.currentTimeMillis() - startTime) + "ms");
    }
    
    public void runDynamic(int iterations) {
        long startTime = System.currentTimeMillis();
        echo("Dynamic - " + startTime);
        Proxy dynamic = (Proxy) Fee.newInstance(new Fi());
        String in = "in";
        for (int i=0; i<iterations; i++) {
            ((Fo)dynamic).outSomething();
            ((Fum)dynamic).inSomething(in);            
        }
        echo("Dynamic invocation with " + iterations + " iterations took " 
            + (System.currentTimeMillis() - startTime) + "ms");    }
    
    public static void echo(Object obj) {
        System.out.println(obj);
    }
}

class Fee implements InvocationHandler {
    private Object obj;
    
    private Fee(Object obj) {
        this.obj = obj;
    }
    
    public static Object newInstance(Object obj) {
        return Proxy.newProxyInstance(
            obj.getClass().getClassLoader(),
            obj.getClass().getInterfaces(),
            new Fee(obj));
    }
    
    public Object invoke(Object proxy, Method m, Object[] args) throws
Throwable {
        try {
            return m.invoke(obj, args);
        } catch (Exception e) {
        }
        return null;
    }
}

class Fi implements Fo, Fum {
    private String something = "something";
    public String outSomething() {
        return something;
    }
    
    public void inSomething(String s) {
        something = s;
    }
    
}

interface Fo {
    public String outSomething();
}

interface Fum {
    public void inSomething(String s);
}
----------------------------------------------------------------------
Post a message:         mailto:ozone-dev@ozone-db.org
Unsubscribe:            mailto:ozone-dev-request@ozone-db.org?body=unsubscribe
Contact adminstrator:   mailto:ozone-dev-owner@ozone-db.org
Read archived messages: http://www.ozone-db.org/
----------------------------------------------------------------------