From 53cd96754116eede064310c2877eaa69f75562f2 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 1 Oct 2021 14:51:28 +0100 Subject: [PATCH] Added defrag (repository maintenance) tests. --- .../java/org/qortal/test/RepositoryTests.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/src/test/java/org/qortal/test/RepositoryTests.java b/src/test/java/org/qortal/test/RepositoryTests.java index 91dd03c2..33a4de7f 100644 --- a/src/test/java/org/qortal/test/RepositoryTests.java +++ b/src/test/java/org/qortal/test/RepositoryTests.java @@ -3,9 +3,12 @@ package org.qortal.test; import org.junit.Before; import org.junit.Test; import org.qortal.account.Account; +import org.qortal.account.PublicKeyAccount; import org.qortal.asset.Asset; import org.qortal.crosschain.BitcoinACCTv1; import org.qortal.crypto.Crypto; +import org.qortal.data.account.AccountBalanceData; +import org.qortal.data.account.AccountData; import org.qortal.repository.DataException; import org.qortal.repository.Repository; import org.qortal.repository.RepositoryManager; @@ -22,6 +25,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -440,6 +444,119 @@ public class RepositoryTests extends Common { } } + @Test + public void testDefrag() throws DataException { + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + + hsqldb.performPeriodicMaintenance(); + + } + } + + @Test + public void testDefragOnDisk() throws DataException { + Common.useSettingsAndDb(testSettingsFilename, false); + + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + + hsqldb.performPeriodicMaintenance(); + + } + } + + @Test + public void testMultipleDefrags() throws DataException { + // Mint some more blocks to populate the database + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + + for (int i = 0; i < 10; i++) { + hsqldb.performPeriodicMaintenance(); + } + } + } + + @Test + public void testMultipleDefragsOnDisk() throws DataException { + Common.useSettingsAndDb(testSettingsFilename, false); + + // Mint some more blocks to populate the database + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + + for (int i = 0; i < 10; i++) { + hsqldb.performPeriodicMaintenance(); + } + } + } + + @Test + public void testMultipleDefragsWithDifferentData() throws DataException { + for (int i=0; i<10; i++) { + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + hsqldb.performPeriodicMaintenance(); + } + } + } + + @Test + public void testMultipleDefragsOnDiskWithDifferentData() throws DataException { + Common.useSettingsAndDb(testSettingsFilename, false); + + for (int i=0; i<10; i++) { + try (final HSQLDBRepository hsqldb = (HSQLDBRepository) RepositoryManager.getRepository()) { + + this.populateWithRandomData(hsqldb); + hsqldb.performPeriodicMaintenance(); + } + } + } + + private void populateWithRandomData(HSQLDBRepository repository) throws DataException { + Random random = new Random(); + + System.out.println("Creating random accounts..."); + + // Generate some random accounts + List accounts = new ArrayList<>(); + for (int ai = 0; ai < 20; ++ai) { + byte[] publicKey = new byte[32]; + random.nextBytes(publicKey); + + PublicKeyAccount account = new PublicKeyAccount(repository, publicKey); + accounts.add(account); + + AccountData accountData = new AccountData(account.getAddress()); + repository.getAccountRepository().ensureAccount(accountData); + } + repository.saveChanges(); + + System.out.println("Creating random balances..."); + + // Fill with lots of random balances + for (int i = 0; i < 100000; ++i) { + Account account = accounts.get(random.nextInt(accounts.size())); + int assetId = random.nextInt(2); + long balance = random.nextInt(100000); + + AccountBalanceData accountBalanceData = new AccountBalanceData(account.getAddress(), assetId, balance); + repository.getAccountRepository().save(accountBalanceData); + + // Maybe mint a block to change height + if (i > 0 && (i % 1000) == 0) + BlockUtils.mintBlock(repository); + } + repository.saveChanges(); + } + public static void hsqldbSleep(int millis) throws SQLException { System.out.println(String.format("HSQLDB sleep() thread ID: %s", Thread.currentThread().getId()));