1+ package edu .harvard .iq .dataverse .somepackage ;
2+
3+ import edu .harvard .iq .dataverse .util .testing .performance .JpaEntityManagerService ;
4+ import edu .harvard .iq .dataverse .util .testing .performance .JpaPerformanceTest ;
5+ import net .ttddyy .dsproxy .QueryCount ;
6+ import net .ttddyy .dsproxy .QueryCountHolder ;
7+ import org .junit .jupiter .api .BeforeAll ;
8+ import org .junit .jupiter .api .Test ;
9+
10+ import java .time .Instant ;
11+ import java .time .temporal .ChronoUnit ;
12+ import jakarta .persistence .EntityManager ;
13+
14+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
15+
16+ // Single annotation for automatic setup of
17+ // 1) basic tags for JUnit groups,
18+ // 2) shared PostgreSQL server via Testcontainers, and
19+ // 3) creation and injection of JPA entity manager service.
20+ @ JpaPerformanceTest
21+ class SamplePerformanceIT {
22+
23+ static JpaEntityManagerService jpa ;
24+
25+ @ BeforeAll
26+ static void setUp () {
27+ // A manual start is necessary to allow you to selectively enable service features as necessary
28+ jpa .start ();
29+
30+ // inTransactionVoid: Use this when you only need to execute database operations
31+ // (e.g., persisting test fixtures) without returning a value.
32+ jpa .inTransactionVoid (em -> {
33+ // EntityManager em is provided here.
34+ // em.persist(myEntity);
35+ });
36+ }
37+
38+ @ Test
39+ void shouldMeasureOperationPerformance () {
40+ // Clear any previous query statistics
41+ QueryCountHolder .clear ();
42+ Instant start = Instant .now ();
43+
44+ // inTransaction: Use this when your operation returns a result that needs
45+ // to be asserted or measured.
46+ Object result = jpa .inTransaction (em -> {
47+ // Execute your performance-critical operation using the EntityManager.
48+ // return result;
49+ return null ; // Placeholder
50+ });
51+
52+ Instant end = Instant .now ();
53+ assertNotNull (result );
54+
55+ // Retrieve and log ORM statistics
56+ QueryCount count = QueryCountHolder .getGrandTotal ();
57+ System .out .println ("Elapsed ms: " + start .until (end , ChronoUnit .MILLIS ));
58+ System .out .println ("Total queries: " + count .getTotal ());
59+ System .out .println ("Select queries: " + count .getSelect ());
60+ System .out .println ("Insert queries: " + count .getInsert ());
61+ System .out .println ("Update queries: " + count .getUpdate ());
62+ System .out .println ("Delete queries: " + count .getDelete ());
63+ }
64+ }
0 commit comments