Skip to content

Commit

Permalink
SPVBlockStoreTest: Add a performance test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schildbach committed Feb 13, 2019
1 parent 4f5b2f2 commit 2bdc594
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/src/test/java/org/bitcoinj/store/SPVBlockStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@
package org.bitcoinj.store;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.math.BigInteger;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.params.UnitTestParams;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Stopwatch;

public class SPVBlockStoreTest {
private static final NetworkParameters UNITTEST = UnitTestParams.get();
private File blockStoreFile;
Expand Down Expand Up @@ -115,4 +124,25 @@ public void twoStores_sequentially_shrink() throws Exception {
store.close();
store = new SPVBlockStore(UNITTEST, blockStoreFile, 10, true);
}

@Test
public void performanceTest() throws BlockStoreException {
// On slow machines, this test could fail. Then either add @Ignore or adapt the threshold and please report to
// us.
final int ITERATIONS = 100000;
final long THRESHOLD_MS = 1500;
SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile);
Stopwatch watch = Stopwatch.createStarted();
for (int i = 0; i < ITERATIONS; i++) {
// Using i as the nonce so that the block hashes are different.
Block block = new Block(UNITTEST, 0, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 0, 0, i,
Collections.<Transaction> emptyList());
StoredBlock b = new StoredBlock(block, BigInteger.ZERO, i);
store.put(b);
store.setChainHead(b);
}
assertTrue("took " + watch + " for " + ITERATIONS + " iterations",
watch.elapsed(TimeUnit.MILLISECONDS) < THRESHOLD_MS);
store.close();
}
}

0 comments on commit 2bdc594

Please sign in to comment.