Use Guava Stopwatch for measuring code execution time.

This commit is contained in:
Andreas Schildbach
2015-11-20 00:10:51 +01:00
parent 90be18150f
commit be794e8a4c
6 changed files with 29 additions and 16 deletions

View File

@@ -880,7 +880,7 @@ public class PeerGroup implements TransactionBroadcaster {
checkState(!lock.isHeldByCurrentThread());
int maxPeersToDiscoverCount = this.vMaxPeersToDiscoverCount;
long peerDiscoveryTimeoutMillis = this.vPeerDiscoveryTimeoutMillis;
long start = System.currentTimeMillis();
final Stopwatch watch = Stopwatch.createStarted();
final List<PeerAddress> addressList = Lists.newLinkedList();
for (PeerDiscovery peerDiscovery : peerDiscoverers /* COW */) {
InetSocketAddress[] addresses;
@@ -902,8 +902,8 @@ public class PeerGroup implements TransactionBroadcaster {
});
}
}
log.info("Peer discovery took {}ms and returned {} items", System.currentTimeMillis() - start,
addressList.size());
watch.stop();
log.info("Peer discovery took {} and returned {} items", watch, addressList.size());
return addressList.size();
}

View File

@@ -22,6 +22,8 @@ import org.bitcoinj.core.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Stopwatch;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -128,9 +130,10 @@ public class MnemonicCode {
String pass = Utils.join(words);
String salt = "mnemonic" + passphrase;
long start = System.currentTimeMillis();
final Stopwatch watch = Stopwatch.createStarted();
byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64);
log.info("PBKDF2 took {}ms", System.currentTimeMillis() - start);
watch.stop();
log.info("PBKDF2 took {}", watch);
return seed;
}

View File

@@ -18,6 +18,7 @@
package org.bitcoinj.params;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Coin;
@@ -32,6 +33,8 @@ import org.bitcoinj.store.BlockStoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Stopwatch;
import org.bitcoinj.core.BitcoinSerializer;
/**
@@ -76,7 +79,7 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
// We need to find a block far back in the chain. It's OK that this is expensive because it only occurs every
// two weeks after the initial block chain download.
long now = System.currentTimeMillis();
final Stopwatch watch = Stopwatch.createStarted();
StoredBlock cursor = blockStore.get(prev.getHash());
for (int i = 0; i < this.getInterval() - 1; i++) {
if (cursor == null) {
@@ -86,9 +89,9 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
}
cursor = blockStore.get(cursor.getHeader().getPrevBlockHash());
}
long elapsed = System.currentTimeMillis() - now;
if (elapsed > 50)
log.info("Difficulty transition traversal took {}msec", elapsed);
watch.stop();
if (watch.elapsed(TimeUnit.MILLISECONDS) > 50)
log.info("Difficulty transition traversal took {}", watch);
Block blockIntervalAgo = cursor.getHeader();
int timespan = (int) (prev.getTimeSeconds() - blockIntervalAgo.getTimeSeconds());

View File

@@ -1,5 +1,6 @@
/**
* Copyright 2014 The bitcoinj developers.
* Copyright 2015 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +26,7 @@ import org.bitcoinj.script.Script;
import org.bitcoinj.store.UnreadableWalletException;
import org.bitcoinj.utils.Threading;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
@@ -1173,7 +1175,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
needed, parent.getPathAsString(), issued, lookaheadSize, lookaheadThreshold, numChildren);
List<DeterministicKey> result = new ArrayList<DeterministicKey>(needed);
long now = System.currentTimeMillis();
final Stopwatch watch = Stopwatch.createStarted();
int nextChild = numChildren;
for (int i = 0; i < needed; i++) {
DeterministicKey key = HDKeyDerivation.deriveThisOrNextChildKey(parent, nextChild);
@@ -1182,7 +1184,8 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
result.add(key);
nextChild = key.getChildNumber().num() + 1;
}
log.info("Took {} msec", System.currentTimeMillis() - now);
watch.stop();
log.info("Took {}", watch);
return result;
}

View File

@@ -21,6 +21,8 @@ import org.bitcoinj.core.*;
import org.bitcoinj.utils.*;
import org.slf4j.*;
import com.google.common.base.Stopwatch;
import javax.annotation.*;
import java.io.*;
import java.util.concurrent.*;
@@ -106,7 +108,7 @@ public class WalletFiles {
}
private void saveNowInternal() throws IOException {
long now = System.currentTimeMillis();
final Stopwatch watch = Stopwatch.createStarted();
File directory = file.getAbsoluteFile().getParentFile();
File temp = File.createTempFile("wallet", null, directory);
final Listener listener = vListener;
@@ -115,7 +117,8 @@ public class WalletFiles {
wallet.saveToFile(temp, file);
if (listener != null)
listener.onAfterAutoSave(file);
log.info("Save completed in {}msec", System.currentTimeMillis() - now);
watch.stop();
log.info("Save completed in {}", watch);
}
/** Queues up a save in the background. Useful for not very important wallet changes. */

View File

@@ -17,6 +17,7 @@
package org.bitcoinj.core;
import com.google.common.base.Stopwatch;
import com.google.common.collect.*;
import com.google.common.net.*;
import com.google.common.util.concurrent.*;
@@ -507,7 +508,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
}
});
// connect to peer but don't do handshake
long start = System.currentTimeMillis(); // before connection so we don't get elapsed < timeout
final Stopwatch watch = Stopwatch.createStarted(); // before connection so we don't get elapsed < timeout
connectPeerWithoutVersionExchange(0);
// wait for disconnect (plus a bit more, in case test server is overloaded)
try {
@@ -516,9 +517,9 @@ public class PeerGroupTest extends TestWithPeerGroup {
// the checks below suffice for this case too
}
// check things after disconnect
long end = System.currentTimeMillis();
watch.stop();
assertFalse(peerConnectedFuture.isDone()); // should never have connected
assertTrue(end - start >= timeout); // should not disconnect before timeout
assertTrue(watch.elapsed(TimeUnit.MILLISECONDS) >= timeout); // should not disconnect before timeout
assertTrue(peerDisconnectedFuture.isDone()); // but should disconnect eventually
}