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