1   /*
2    * IntrospectionPerformanceTest - Tests introspection on JavaBeans
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.beanintrospect;
20  
21  import org.apache.log4j.Logger;
22  import org.christianschenk.beanintrospect.beans.IntBean;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  public class PerformanceComparison {
28  
29  	private static final Logger log = Logger.getLogger(PerformanceComparison.class);
30  	// it's just fair to initialize this class once
31  	private static final BeanIntrospector beanIntrospector = new BeanIntrospector();
32  
33  	@Test
34  	public void testPerformance() {
35  		log.debug("Runs\tByHand\tIntrospector");
36  		final Runnable[] runners = new Runnable[] { byHandRunnable, beanIntrospectorRunnable };
37  		for (final int count : new int[] { 10, 100, 1000, 10000, 20000, 50000, 100000 }) {
38  //		for (int count = 1; count < 1000; count++) {
39  			final long times[] = new long[runners.length];
40  			int i = 0;
41  			for (final Runnable runner : runners) {
42  				long time = System.nanoTime();
43  				for (int runs = 0; runs < count; runs++) {
44  					runner.run();
45  				}
46  				time = System.nanoTime() - time;
47  				times[i++] = time / (1000 * 1000);
48  			}
49  			log.debug(count + "\t" + times[0] + "\t" + times[1] + "\t\t" + ((float) times[1] / (float) times[0]));
50  		}
51  	}
52  
53  	private static final Runnable beanIntrospectorRunnable = new Runnable() {
54  		public void run() {
55  			final IntBean intBean1 = new IntBean(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
56  			final IntBean intBean2 = new IntBean();
57  			beanIntrospector.fill(intBean2, intBean1);
58  			assertIntBeans(intBean1, intBean2);
59  		}
60  	};
61  
62  	private static final Runnable byHandRunnable = new Runnable() {
63  		public void run() {
64  			final IntBean intBean1 = new IntBean(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
65  			final IntBean intBean2 = new IntBean();
66  			intBean2.setInt1(intBean1.getInt1());
67  			intBean2.setInt2(intBean1.getInt2());
68  			intBean2.setInt3(intBean1.getInt3());
69  			intBean2.setInt4(intBean1.getInt4());
70  			intBean2.setInt5(intBean1.getInt5());
71  			intBean2.setInt6(intBean1.getInt6());
72  			intBean2.setInt7(intBean1.getInt7());
73  			intBean2.setInt8(intBean1.getInt8());
74  			intBean2.setInt9(intBean1.getInt9());
75  			intBean2.setInt10(intBean1.getInt10());
76  			assertIntBeans(intBean1, intBean2);
77  		}
78  	};
79  
80  	private static final void assertIntBeans(final IntBean intBean1, final IntBean intBean2) {
81  		assertEquals(1, intBean1.getInt1());
82  		assertEquals(1, intBean2.getInt1());
83  		assertEquals(2, intBean1.getInt2());
84  		assertEquals(2, intBean2.getInt2());
85  		assertEquals(3, intBean1.getInt3());
86  		assertEquals(3, intBean2.getInt3());
87  		assertEquals(4, intBean1.getInt4());
88  		assertEquals(4, intBean2.getInt4());
89  		assertEquals(5, intBean1.getInt5());
90  		assertEquals(5, intBean2.getInt5());
91  		assertEquals(6, intBean1.getInt6());
92  		assertEquals(6, intBean2.getInt6());
93  		assertEquals(7, intBean1.getInt7());
94  		assertEquals(7, intBean2.getInt7());
95  		assertEquals(8, intBean1.getInt8());
96  		assertEquals(8, intBean2.getInt8());
97  		assertEquals(9, intBean1.getInt9());
98  		assertEquals(9, intBean2.getInt9());
99  		assertEquals(10, intBean1.getInt10());
100 		assertEquals(10, intBean2.getInt10());
101 	}
102 }