View Javadoc
1   package ejava.jpa.examples.tuning;
2   
3   import java.io.IOException;
4   
5   import java.lang.reflect.Method;
6   import java.util.LinkedList;
7   import java.util.List;
8   
9   import javax.persistence.EntityManager;
10  import javax.persistence.EntityManagerFactory;
11  import javax.persistence.Persistence;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  import org.junit.After;
16  import org.junit.AfterClass;
17  import org.junit.Before;
18  import org.junit.BeforeClass;
19  import org.junit.Rule;
20  
21  import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
22  import com.carrotsearch.junitbenchmarks.BenchmarkOptionsSystemProperties;
23  import com.carrotsearch.junitbenchmarks.BenchmarkRule;
24  import com.carrotsearch.junitbenchmarks.IResultsConsumer;
25  import com.carrotsearch.junitbenchmarks.Result;
26  import com.carrotsearch.junitbenchmarks.WriterConsumer;
27  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
28  import com.carrotsearch.junitbenchmarks.h2.H2Consumer;
29  
30  import ejava.jpa.examples.tuning.dao.MovieDAOImpl;
31  
32  @BenchmarkOptions(warmupRounds=0, benchmarkRounds=1)
33  @BenchmarkHistoryChart
34  public class TestBase {
35      protected static Logger log = LoggerFactory.getLogger(TestBase.class);
36      protected static String PERSISTENCE_UNIT = "movietune-test-thin";
37      protected static EntityManagerFactory emf;
38      private static EntityManager em_;
39  	private static MovieDAOImpl dao;
40  	private static H2Consumer h2;
41  	private static final ResultsConsumer resultsConsumer;
42  	static {
43  		System.setProperty("jub.db.file", "target/benchmarks");
44  		System.setProperty(BenchmarkOptionsSystemProperties.CHARTS_DIR_PROPERTY, "target/charts");
45  		h2=new H2Consumer();
46  		resultsConsumer=new ResultsConsumer();
47  	}
48  	
49  	public static class ResultsConsumer implements IResultsConsumer {
50  		private List<Result> results = new LinkedList<Result>();
51  		@Override
52  		public void accept(Result result) throws IOException {
53  			results.add(result);
54  		}
55  		public List<Result> getResults() { return results; }
56  	};
57  
58  	public @Rule BenchmarkRule benchmarkRun;
59  	public static ResultsConsumer getResultsConsumer() { return resultsConsumer; }
60  	protected static String getLabel(Object o) {
61  		if (o instanceof Class<?>) {
62  			Class<?> type = (Class<?>)o;
63  			TestLabel label = type.getAnnotation(TestLabel.class);
64  			return label != null ? label.label() : type.getSimpleName();
65  		} else if (o instanceof Method) {
66  			Method m = (Method)o;
67  			TestLabel label = m.getAnnotation(TestLabel.class);
68  			return label != null ? label.label() : m.getName();
69  		}
70  		return o.toString();
71  	}
72  	public static void printResults() {
73  		StringBuilder text = new StringBuilder();
74  		text.append("\n=========================================================================\n\n");
75  		for (Result r : TestBase.getResultsConsumer().getResults()) {
76  			text.append(String.format("%s.%s:", getLabel(r.getTestClass()), getLabel(r.getTestMethod())));
77  			text.append(String.format("warmups=%d",r.warmupRounds));
78  			text.append(String.format(", rounds=%d",r.benchmarkRounds));
79  			text.append(String.format(", ave=%s",r.roundAverage.toString()));
80  			text.append("\n");
81  		}
82  		text.append("\n=========================================================================");
83  		log.info(text.toString());
84  	}
85  	
86  	protected static EntityManagerFactory getEMF() {
87  		if (emf==null) {
88  	        emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
89  		}
90  		return emf;
91  	}
92  	protected static MovieDAOImpl getDAO() {
93  		if (dao == null) {
94  			dao = new MovieDAOImpl();
95  			em_ = getEMF().createEntityManager();
96  			dao.setEntityManager(em_);
97  		}
98  		return dao;
99  	}
100 	
101 	@BeforeClass
102     public static void setUpBaseClass() {
103         EntityManager em = getEMF().createEntityManager();
104         cleanup(em);
105         em.close();
106     }
107 	
108 	public TestBase() {
109 		benchmarkRun=new BenchmarkRule(resultsConsumer, new WriterConsumer(), h2);
110 	}
111 	
112 	@Before
113 	public void setUpBase() {
114         EntityManager em = getEMF().createEntityManager();
115 		new MovieFactory().setEntityManager(em).flush();
116 		em.close();
117 		if (em_!=null) {
118 			em_.clear();
119 		}
120 	}
121 	@After
122 	public void tearDownBase() {
123 		log.debug("");
124 	}
125 	
126 	@AfterClass
127 	public static void tearDownBaseClass() {
128 		if (em_ != null) {
129 			em_.close(); 
130 			em_ = null;
131 			dao = null;
132 		}
133 		if (emf!=null) {
134 			emf.close(); emf=null;
135 		}
136 	}
137 
138 	/**
139 	 * Get back to a known starting point
140 	 * @param em
141 	 */
142 	public static void cleanup(EntityManager em) {
143     	new MovieFactory().setEntityManager(em).cleanup();
144     }
145 }