Skip to content

Instantly share code, notes, and snippets.

@Giszmo
Created April 7, 2018 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Giszmo/27d484cb02ef759de51d8beeebedb12d to your computer and use it in GitHub Desktop.
Save Giszmo/27d484cb02ef759de51d8beeebedb12d to your computer and use it in GitHub Desktop.
package org.bitcoinj.core;
import org.bitcoinj.testing.TestWithWallet;
import org.bitcoinj.utils.ListenerRegistration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.Stopwatch;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
public class TransactionConfidenceTest extends TestWithWallet {
@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void queueListenersIteratorPerformance() {
CopyOnWriteArrayList<ListenerRegistration<TransactionConfidence.Listener>> listeners = new CopyOnWriteArrayList<>();
AbstractExecutorService executorService = new AbstractExecutorService() {
@Override
public void shutdown() {
}
@Override
public List<Runnable> shutdownNow() {
return null;
}
@Override
public boolean isShutdown() {
return false;
}
@Override
public boolean isTerminated() {
return false;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return false;
}
@Override
public void execute(Runnable command) {
}
};
for(int i = 0; i < 100000; i++) {
listeners.add(new ListenerRegistration<TransactionConfidence.Listener>(new TransactionConfidence.Listener() {
int rnd = (int) (Math.random() * 100);
@Override
public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) {
if(reason.ordinal() + 1000 < rnd) {
throw new RuntimeException();
}
}
}, executorService
));
}
final TransactionConfidence.Listener.ChangeReason reason = TransactionConfidence.Listener.ChangeReason.DEPTH;
final TransactionConfidence transactionConfidence = new TransactionConfidence(Sha256Hash.ZERO_HASH);
long start = System.currentTimeMillis();
for (final ListenerRegistration<TransactionConfidence.Listener> registration : listeners) {
registration.executor.execute(new Runnable() {
@Override
public void run() {
registration.listener.onConfidenceChanged(transactionConfidence, reason);
}
});
}
long middle = System.currentTimeMillis();
for (int counter = 0; counter < listeners.size(); counter++) {
final ListenerRegistration<TransactionConfidence.Listener> registration = listeners.get(counter);
registration.executor.execute(new Runnable() {
@Override
public void run() {
registration.listener.onConfidenceChanged(transactionConfidence, reason);
}
});
}
long stop = System.currentTimeMillis();
System.out.println("First took " + (middle - start) + "ms. Second " + (stop - middle) + "ms.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment