Created
April 7, 2018 02:14
-
-
Save Giszmo/27d484cb02ef759de51d8beeebedb12d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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