kaspad/domain/blockdag/bench_test.go
oudeis 3839767aed
[NOD-815] Refactor all UTXO-diff algebra methods (#857)
* UTXOSet: Refactoring.

* Use set operations for utxo collection rules.
* diffFrom Refactoring.
* withDiffInPlace Refactoring.

* [NOD-815] Refactor all UTXO-diff algebra methods

* Use set operations for utxo collection rules.
* diffFrom Refactoring.
* withDiffInPlace Refactoring.

* Stylistic fixes.

* UTXOSet: Refactoring. Add benchmarks. Optimizations.

* Add UTXOSet diffFrom, withDiff benchmarks.
* Add performance optimizations.
* Add both in-place and value-return methods for set operations.
* Remove redundant blue score condition checking in withDiffInPlace
  second error rule.
* Improve naming.
* PR fixes.

* After-merge build fixes.

* Typo fixes.

* Stylistic fixes.

* After-merge build fixes.

* Typo fixes.

Co-authored-by: Septen <gammerxpower@gmail.com>
2020-09-01 09:30:08 +03:00

138 lines
3.0 KiB
Go

package blockdag
import (
"strconv"
"testing"
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/util/daghash"
)
func generateNewUTXOEntry(index uint64) (appmessage.Outpoint, *UTXOEntry) {
txSuffix := strconv.FormatUint(index, 10)
txStr := "0000000000000000000000000000000000000000000000000000000000000000"
txID, _ := daghash.NewTxIDFromStr(txStr[0:len(txStr)-len(txSuffix)] + txSuffix)
outpoint := *appmessage.NewOutpoint(txID, 0)
utxoEntry := NewUTXOEntry(&appmessage.TxOut{ScriptPubKey: []byte{}, Value: index}, true, index)
return outpoint, utxoEntry
}
func generateUtxoCollection(startIndex uint64, numItems uint64) utxoCollection {
uc := make(utxoCollection)
for i := uint64(0); i < numItems; i++ {
outpoint, utxoEntry := generateNewUTXOEntry(startIndex + i)
uc.add(outpoint, utxoEntry)
}
return uc
}
// BenchmarkDiffFrom performs a benchmark on how long it takes to calculate
// the difference between this utxoDiff and another one
func BenchmarkDiffFrom(b *testing.B) {
var numOfEntries uint64 = 100
var startIndex uint64 = 0
uc1 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc2 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc3 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc4 := generateUtxoCollection(startIndex, numOfEntries)
tests := []struct {
this *UTXODiff
other *UTXODiff
}{
{
this: &UTXODiff{
toAdd: uc1,
toRemove: uc2,
},
other: &UTXODiff{
toAdd: uc3,
toRemove: uc4,
},
},
{
this: &UTXODiff{
toAdd: uc1,
toRemove: uc2,
},
other: &UTXODiff{
toAdd: uc3,
toRemove: uc1,
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, test := range tests {
test.this.diffFrom(test.other)
}
}
}
// BenchmarkWithDiff performs a benchmark on how long it takes to apply provided diff to this diff
func BenchmarkWithDiff(b *testing.B) {
var numOfEntries uint64 = 100
var startIndex uint64 = 0
uc1 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc2 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc3 := generateUtxoCollection(startIndex, numOfEntries)
startIndex = startIndex + numOfEntries
uc4 := generateUtxoCollection(startIndex, numOfEntries)
tests := []struct {
this *UTXODiff
other *UTXODiff
}{
{
this: &UTXODiff{
toAdd: uc1,
toRemove: uc2,
},
other: &UTXODiff{
toAdd: uc3,
toRemove: uc4,
},
},
{
this: &UTXODiff{
toAdd: uc1,
toRemove: uc2,
},
other: &UTXODiff{
toAdd: uc3,
toRemove: uc2,
},
},
{
this: &UTXODiff{
toAdd: uc1,
toRemove: uc2,
},
other: &UTXODiff{
toAdd: uc1,
toRemove: uc3,
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, test := range tests {
test.this.WithDiff(test.other)
}
}
}