From 5dbda75c0b953134e9686af6aeb44e7b515e47a4 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 13 Sep 2025 17:01:40 -0500 Subject: [PATCH] add a unit test for SQL update performance testing --- .../src/test/java/tests/DhRepoSqliteTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/core/src/test/java/tests/DhRepoSqliteTest.java b/core/src/test/java/tests/DhRepoSqliteTest.java index 4860d07fb..669429382 100644 --- a/core/src/test/java/tests/DhRepoSqliteTest.java +++ b/core/src/test/java/tests/DhRepoSqliteTest.java @@ -37,6 +37,7 @@ import java.io.File; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.concurrent.CompletableFuture; /** * Validates {@link AbstractDhRepo} is set up correctly. @@ -343,4 +344,52 @@ public class DhRepoSqliteTest } + // can be uncommented to compare different update methods performance + //@Test + public void testBulkUpdatePerformance() + { + TestPrimaryKeyRepo primaryKeyRepo = null; + try + { + primaryKeyRepo = new TestPrimaryKeyRepo(DATABASE_TYPE, new File(DB_FILE_NAME)); + final TestPrimaryKeyRepo finalRepoRef = primaryKeyRepo; + + + long startMs = System.currentTimeMillis(); + + // run two threads that try to update + // the same DTO to test locks + CompletableFuture f1 = CompletableFuture.runAsync(() -> bulkSameDtoUpdate(finalRepoRef, 0, 2_000)); + CompletableFuture f2 = CompletableFuture.runAsync(() -> bulkSameDtoUpdate(finalRepoRef, 2_000, 4_000)); + + CompletableFuture.allOf(f1, f2).join(); + + long endMs = System.currentTimeMillis(); + System.out.println("Bulk update took ["+(endMs - startMs)+"] ms"); + } + catch (SQLException e) + { + Assert.fail(e.getMessage()); + } + finally + { + if (primaryKeyRepo != null) + { + primaryKeyRepo.close(); + } + } + } + private static void bulkSameDtoUpdate(TestPrimaryKeyRepo repo, int startIndex, int endIndex) + { + TestSingleKeyDto dto = new TestSingleKeyDto(0, "a", 0L, (byte) 0); + for (int i = startIndex; i < endIndex; i++) + { + dto.longValue = i; + repo.save(dto); + } + } + + + + }