1   /*
2    * Created on Oct 22, 2003
3    * 
4    * Copyright neotechnologies.org
5    */
6   package functional.performance;
7   
8   
9   import java.net.InetAddress;
10  
11  import junit.framework.TestCase;
12  
13  import org.neo.swarm.ApplicationContext;
14  import org.neo.swarm.SwarmContainer;
15  import org.neo.swarm.config.SimpleConfig;
16  
17  /***
18   * Provides facility to test various container configurations with relative performance stats within a single VM.
19   * @author neil.avery
20   */
21  public class PerformanceTest extends TestCase {
22  	SwarmContainer swarm1;
23  	SwarmContainer swarm2;
24  	int javaSpeed = 0;
25  	int limit = 1000;
26  	double localSpeed = 0;
27  
28  	// test scalability A->B in terms of 10 - 10000 (A) -> 1 (B) concurrent invocations upon a destination object (non synchronised)
29  	// test scalability A->B in terms of 1 (A) -> 10 - 10000 1 (B) multicast output
30  	// test scalability A->B in terms of 1 (A) -> 10 - 10000 1 (B) multicast output
31  	// test volumetric through put V scalability (1024K -> 10MB) -> 1-1000 x 1000-1
32  	// test overload resillience - (overload and graceful recover - measuring peak (requires Seda config))
33  	// test performance of various object-types - simple objects -> complex object (heirarchy 2 deep).
34  	
35  	protected void setUp() throws Exception {
36  		swarm1 = new SimpleConfig("swarm1", InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082, false).setup();
37  		swarm2 = new SimpleConfig("swarm2", InetAddress.getLocalHost(), 8082, InetAddress.getLocalHost(), 8081, false).setup();
38  		ApplicationContext ac = swarm1.getAppContext("swarm1");
39  //		ac.addCachedComponent("compA", ComponentA.class);
40  //        ac.addCachedComponent("proxyable", JellyBean.class);
41  
42  		//ac = swarm2.getAppContext("ac");
43  		ac.addCachedComponent("compB", ComponentB.class);		
44  		swarm1.start();
45  		swarm2.start();
46  		Thread.sleep(3 * 1000);
47  	}
48  	
49  	/* (non-Javadoc)
50  	 * @see junit.framework.TestCase#tearDown()
51  	 */
52  	protected void tearDown() throws Exception {
53  		if (swarm1 != null) swarm1.stop();
54  		if (swarm2 != null) swarm2.stop();
55  	}
56  	/***
57  	 * Compare Java, Local, and Remote invocation throughput.
58  	 */
59  	public void _testRawInvocationPerformance() throws Exception {
60  		
61  
62  		long startTime = System.currentTimeMillis();
63  
64  		ComponentBI compB1 = new ComponentB();
65  		startTime = System.currentTimeMillis();
66  		System.out.println(" ============== starting java test =================");
67  		for (int i = 0; i < limit; i++) {
68  			compB1.doFastStuff(i);
69  		}
70  		
71  		double elapsed = (System.currentTimeMillis() - startTime)/1000.0;
72  		System.out.println("elapsed time:" + elapsed);
73  		System.out.println(" rate:" + limit/elapsed + "invocations/sec");	
74  		javaSpeed = new Double(limit/elapsed).intValue();
75  	}
76  	
77  	public void _testLocalPerformance() throws Exception {
78  
79          ComponentBI compB2 = (ComponentBI) swarm1.getComponent("compB");
80  		long startTime = System.currentTimeMillis();
81  		System.out.println(" ============== starting local test =================");
82  		for (int i = 0; i < limit; i++) {
83  			compB2.doFastStuff(i);
84  		}
85  		double elapsed = (System.currentTimeMillis() - startTime)/1000.0;		
86  		System.out.println("elapsed time:" + elapsed);
87  		System.out.println(" rate:" +  limit/elapsed + "invocations/sec");	
88  		localSpeed = limit/elapsed;
89  		System.out.println("elapsed time:" + elapsed);
90  		System.out.println(" rate:" + limit/elapsed + "invocations/sec");	
91  		
92  	}
93  	
94  	public void testRemotingSpeed() throws Exception {
95  		// remoting as it looks up compB from swarm2->silc1AC->compB
96  		ComponentBI compB3 = (ComponentBI) swarm2.getComponent("compB");
97  		long startTime = System.currentTimeMillis();
98  		
99  		//expected at least 4000K/sec
100 		//
101 		System.out.println(" ============== starting remoting test =================");
102 		for (int i = 0; i < limit; i++) {
103 			compB3.doFastStuff(i);
104 		}
105 		double elapsed = (System.currentTimeMillis() - startTime)/1000.0;
106 		System.out.println("elapsed time:" + elapsed);
107 		System.out.println(" rate:" + limit/elapsed + "invocations/sec");	
108 		double remotingSpeed = limit/elapsed;
109 		
110 		// show java->container performance ratios
111 		System.out.println(" ============== relative performance to raw java =================");
112 		System.out.println(" Java:1 \t SILC Local: " + localSpeed/javaSpeed + " \t SILC Remote: " + remotingSpeed/javaSpeed);
113 
114 	}
115 
116 }