This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Add benchmark for BlockHashOperation #203
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63bc0f1
Initial BlockHashOperationBenchmark.
ajsutton c2d75a3
Use an actual RocksDB key value storage for operation benchmarks.
ajsutton 90d478a
Extract out a reusable OperationBenchmarkHelper for the required setup.
ajsutton e17c7d8
Add a benchmark that does not leverage the BlockHashCache to report t…
ajsutton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
72 changes: 72 additions & 0 deletions
72
...rc/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/BlockHashOperationBenchmark.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2018 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.vm.operations; | ||
|
||
import tech.pegasys.pantheon.ethereum.mainnet.ConstantinopleGasCalculator; | ||
import tech.pegasys.pantheon.ethereum.vm.BlockHashLookup; | ||
import tech.pegasys.pantheon.ethereum.vm.MessageFrame; | ||
import tech.pegasys.pantheon.util.bytes.Bytes32; | ||
import tech.pegasys.pantheon.util.uint.UInt256; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
|
||
@State(Scope.Thread) | ||
public class BlockHashOperationBenchmark { | ||
|
||
@Param({ | ||
"1", // Worst-case scenario | ||
"125", // Must iterate up the chain | ||
"255" // Hash available directly via current header's parentHash | ||
}) | ||
public long blockNumber; | ||
|
||
private OperationBenchmarkHelper operationBenchmarkHelper; | ||
private BlockHashOperation operation; | ||
private MessageFrame frame; | ||
|
||
@Setup | ||
public void prepare() throws Exception { | ||
operationBenchmarkHelper = OperationBenchmarkHelper.create(); | ||
operation = new BlockHashOperation(new ConstantinopleGasCalculator()); | ||
frame = operationBenchmarkHelper.createMessageFrame(); | ||
} | ||
|
||
@TearDown | ||
public void cleanUp() throws Exception { | ||
operationBenchmarkHelper.cleanUp(); | ||
} | ||
|
||
@Benchmark | ||
public Bytes32 executeOperation() { | ||
frame.pushStackItem(UInt256.of(blockNumber).getBytes()); | ||
operation.execute(frame); | ||
return frame.popStackItem(); | ||
} | ||
|
||
@Benchmark | ||
public Bytes32 executeOperationWithEmptyHashCache() { | ||
final MessageFrame cleanFrame = | ||
operationBenchmarkHelper | ||
.createMessageFrameBuilder() | ||
.blockHashLookup(new BlockHashLookup(frame.getBlockHeader(), frame.getBlockchain())) | ||
.build(); | ||
cleanFrame.pushStackItem(UInt256.of(blockNumber).getBytes()); | ||
operation.execute(cleanFrame); | ||
return cleanFrame.popStackItem(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...e/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2018 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.vm.operations; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain; | ||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.core.BlockBody; | ||
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; | ||
import tech.pegasys.pantheon.ethereum.core.ExecutionContextTestFixture; | ||
import tech.pegasys.pantheon.ethereum.core.MessageFrameTestFixture; | ||
import tech.pegasys.pantheon.ethereum.vm.MessageFrame; | ||
import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; | ||
import tech.pegasys.pantheon.util.uint.UInt256; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import com.google.common.io.MoreFiles; | ||
import com.google.common.io.RecursiveDeleteOption; | ||
|
||
public class OperationBenchmarkHelper { | ||
|
||
private final Path storageDirectory; | ||
private final RocksDbKeyValueStorage keyValueStorage; | ||
private final MessageFrame messageFrame; | ||
|
||
private OperationBenchmarkHelper( | ||
final Path storageDirectory, | ||
final RocksDbKeyValueStorage keyValueStorage, | ||
final MessageFrame messageFrame) { | ||
this.storageDirectory = storageDirectory; | ||
this.keyValueStorage = keyValueStorage; | ||
this.messageFrame = messageFrame; | ||
} | ||
|
||
public static OperationBenchmarkHelper create() throws IOException { | ||
final Path storageDirectory = Files.createTempDirectory("benchmark"); | ||
final RocksDbKeyValueStorage keyValueStorage = RocksDbKeyValueStorage.create(storageDirectory); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use Provider and pass it into create? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My aim here is to make pull as much of the complexity out of the benchmark classes where it would have to be repeated into this helper class where it can be shared. This also isn't the type of benchmark where you'd test out multiple back-ends because the usage of storage is typically very light (even SSTORE wouldn't affect the key value storage in this scope). |
||
|
||
final ExecutionContextTestFixture executionContext = | ||
ExecutionContextTestFixture.builder().keyValueStorage(keyValueStorage).build(); | ||
final MutableBlockchain blockchain = executionContext.getBlockchain(); | ||
|
||
for (int i = 1; i < 256; i++) { | ||
blockchain.appendBlock( | ||
new Block( | ||
new BlockHeaderTestFixture() | ||
.parentHash(blockchain.getChainHeadHash()) | ||
.number(i) | ||
.difficulty(UInt256.ONE) | ||
.buildHeader(), | ||
new BlockBody(emptyList(), emptyList())), | ||
emptyList()); | ||
} | ||
final MessageFrame messageFrame = | ||
new MessageFrameTestFixture() | ||
.executionContextTestFixture(executionContext) | ||
.blockHeader( | ||
new BlockHeaderTestFixture() | ||
.parentHash(blockchain.getChainHeadHash()) | ||
.number(blockchain.getChainHeadBlockNumber() + 1) | ||
.difficulty(UInt256.ONE) | ||
.buildHeader()) | ||
.build(); | ||
return new OperationBenchmarkHelper(storageDirectory, keyValueStorage, messageFrame); | ||
} | ||
|
||
public MessageFrame createMessageFrame() { | ||
return createMessageFrameBuilder().build(); | ||
} | ||
|
||
public MessageFrame.Builder createMessageFrameBuilder() { | ||
return MessageFrame.builder() | ||
.type(MessageFrame.Type.MESSAGE_CALL) | ||
.messageFrameStack(messageFrame.getMessageFrameStack()) | ||
.blockchain(messageFrame.getBlockchain()) | ||
.worldState(messageFrame.getWorldState()) | ||
.initialGas(messageFrame.getRemainingGas()) | ||
.address(messageFrame.getContractAddress()) | ||
.originator(messageFrame.getOriginatorAddress()) | ||
.contract(messageFrame.getRecipientAddress()) | ||
.gasPrice(messageFrame.getGasPrice()) | ||
.inputData(messageFrame.getInputData()) | ||
.sender(messageFrame.getSenderAddress()) | ||
.value(messageFrame.getValue()) | ||
.apparentValue(messageFrame.getApparentValue()) | ||
.code(messageFrame.getCode()) | ||
.blockHeader(messageFrame.getBlockHeader()) | ||
.depth(messageFrame.getMessageStackDepth()) | ||
.isStatic(messageFrame.isStatic()) | ||
.completer(messageFrame -> {}) | ||
.miningBeneficiary(messageFrame.getMiningBeneficiary()) | ||
.blockHashLookup(messageFrame.getBlockHashLookup()); | ||
} | ||
|
||
public void cleanUp() throws IOException { | ||
keyValueStorage.close(); | ||
MoreFiles.deleteRecursively(storageDirectory, RecursiveDeleteOption.ALLOW_INSECURE); | ||
} | ||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be RocksDbKeyValueStorage? Can we use the abstract parent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need access to the
close
method which is only onRocksDbKeyValueStorage
. I think I might follow up with another PR to addclose
to theKeyValueStorage
interface which would improve this kind of thing in a few places, but outside this scope of this change.