Skip to content

Commit

Permalink
Use Guava Stopwatch for measuring code execution time.
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Dec 5, 2015
1 parent 90be181 commit be794e8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ protected int discoverPeers() throws PeerDiscoveryException {
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;
Expand All @@ -902,8 +902,8 @@ public void run() {
});
}
}
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();
}

Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/bitcoinj/crypto/MnemonicCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -128,9 +130,10 @@ public static byte[] toSeed(List<String> words, String passphrase) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Stopwatch;

import org.bitcoinj.core.BitcoinSerializer;

/**
Expand Down Expand Up @@ -76,7 +79,7 @@ public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block

// 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) {
Expand All @@ -86,9 +89,9 @@ public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block
}
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,6 +26,7 @@
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;
Expand Down Expand Up @@ -1173,7 +1175,7 @@ private List<DeterministicKey> maybeLookAhead(DeterministicKey parent, int issue
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);
Expand All @@ -1182,7 +1184,8 @@ private List<DeterministicKey> maybeLookAhead(DeterministicKey parent, int issue
result.add(key);
nextChild = key.getChildNumber().num() + 1;
}
log.info("Took {} msec", System.currentTimeMillis() - now);
watch.stop();
log.info("Took {}", watch);
return result;
}

Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/bitcoinj/wallet/WalletFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.bitcoinj.utils.*;
import org.slf4j.*;

import com.google.common.base.Stopwatch;

import javax.annotation.*;
import java.io.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -106,7 +108,7 @@ public void saveNow() throws IOException {
}

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;
Expand All @@ -115,7 +117,8 @@ private void saveNowInternal() throws IOException {
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. */
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/org/bitcoinj/core/PeerGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -507,7 +508,7 @@ public void onPeerDisconnected(Peer peer, int peerCount) {
}
});
// 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 {
Expand All @@ -516,9 +517,9 @@ public void onPeerDisconnected(Peer peer, int peerCount) {
// 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
}

Expand Down

0 comments on commit be794e8

Please sign in to comment.