1   /**
2    * Copyright (C) 2009 Erik Putrycz <erik.putrycz@gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.ep.db4o.activator;
18  
19  import static org.testng.Assert.assertEquals;
20  import static org.testng.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import net.ep.db4o.javassist.JVSTReflector;
27  import net.ep.db4o.javassist.testclasses.SensorPanelCTA;
28  import net.ep.db4o.javassist.testclasses.SensorPanelMTA;
29  
30  import org.testng.annotations.Test;
31  
32  import com.db4o.ObjectContainer;
33  import com.db4o.query.Query;
34  
35  public class PerformanceTest extends GenericProxyTest {
36  
37  	
38  	public void saveData(ObjectContainer db, int instances) {
39  		SensorPanelCTA sensor = SensorPanelCTA.createList(instances);
40  		db.store(sensor);
41  		SensorPanelMTA sensor2 = SensorPanelMTA.createList(instances);
42  		db.store(sensor2);
43  	}
44  		
45  	@Test
46  	public void proxyPerformanceAnalysisTest() throws IOException {
47  		proxyPerformanceAnalysisTest(10000);
48  		proxyPerformanceAnalysisTest(100000);
49  	}
50  	
51  	public static void main(String args[]) {
52  		PerformanceTest pf = new PerformanceTest();
53  		try {
54  			pf.proxyPerformanceAnalysisTest(10000);
55  		} catch (IOException e) {
56  			// TODO Auto-generated catch block
57  			e.printStackTrace();
58  		}
59  	}
60  
61  	private void proxyPerformanceAnalysisTest(int instances) throws IOException {
62  		proxyPerformanceAnalysisTest(instances, false);
63  		proxyPerformanceAnalysisTest(instances, true);
64  	}	
65  	
66  	private void proxyPerformanceAnalysisTest(int instances, boolean threadLocal) throws IOException {
67  		createDBIfNecessary(instances);
68  		openDBNoTA();
69  		Query qr = db.query();
70  		qr.constrain(SensorPanelCTA.class);
71  		qr.descend("_sensor").constrain(new Integer(1));
72  		List<SensorPanelCTA> list = qr.execute();
73  		Iterator<SensorPanelCTA> it = list.iterator();
74  		long startTime = System.currentTimeMillis();
75  		int total = 1;
76  		SensorPanelCTA panel = it.next();
77  		db.activate(panel, 1);
78  		while (panel.getNext() != null) {	
79  			panel = panel.getNext();
80  			db.activate(panel, 1);
81  			if (total==1)
82  				assertTrue(!JVSTReflector.isEnhanced(panel.getClass()));
83  			total++;
84  		}
85  		assertEquals(instances,total);
86  		long endTime = System.currentTimeMillis();
87  		long durationNoTA = endTime - startTime;
88  		openDBwTA();
89  		qr = db.query();
90  		qr.constrain(SensorPanelCTA.class);
91  		qr.descend("_sensor").constrain(new Integer(1));
92  		list = qr.execute();
93  		it = list.iterator();
94  		startTime = System.currentTimeMillis();
95  		total = 1;
96  		panel = it.next();
97  		while (panel.getNext() != null) {	
98  			panel = panel.getNext();
99  			if (total==1)
100 				assertTrue(JVSTReflector.isEnhanced(panel.getClass()));
101 			total++;
102 		}
103 		assertEquals(total, instances);
104 		endTime = System.currentTimeMillis();
105 		long durationTA = endTime - startTime;
106 		openDBwTA();
107 		qr = db.query();
108 		qr.constrain(SensorPanelMTA.class);
109 		qr.descend("_sensor").constrain(new Integer(1));
110 		List<SensorPanelMTA> list2 = qr.execute();
111 		Iterator<SensorPanelMTA> it2 = list2.iterator();
112 		startTime = System.currentTimeMillis();
113 		total = 1;
114 		SensorPanelMTA panel2 = it2.next();
115 		while (panel2.getNext() != null) {	
116 			panel2 = panel2.getNext();
117 			total++;
118 		}
119 		assertEquals(total, instances);
120 		endTime = System.currentTimeMillis();
121 		long durationMA = endTime - startTime;		
122 		
123 		System.out.println((threadLocal?"TL ":"") + "with TA= " + durationTA);
124 		System.out.println("with MA= " + durationMA);
125 		System.out.println("without TA= " + durationNoTA);	
126 		cleanup();
127 	}	
128 }