probe_test.go raw

   1  package utxo_test
   2  
   3  import (
   4  	"encoding/binary"
   5  	"sort"
   6  	"testing"
   7  	"time"
   8  
   9  	"git.smesh.lol/btcd-lite/internal/utxo"
  10  	"git.smesh.lol/btcd-lite/internal/wire"
  11  )
  12  
  13  // TestHavocProbe measures the cost of a set rebuild against the shape of the
  14  // Bitcoin pre-fork set: 70 million entries, keyed by real-looking hashes, with
  15  // the touches of a 10,000-block range spread over it.
  16  //
  17  // It is the merge half of the arithmetic that decides the checkpoint spacing,
  18  // run on one core of the machine the work is done on. It does not model the
  19  // disk: the plan's rebuild figure is the stream, and this number is the part
  20  // that is not I/O.
  21  func TestHavocProbe(t *testing.T) {
  22  	if testing.Short() {
  23  		t.Skip("probe is long")
  24  	}
  25  	// The pre-fork set is about 70 million entries and a 10,000-block range
  26  	// touches about half a million of them. The probe scales both by the same
  27  	// factor on a small machine: the ratio is what decides the design, and the
  28  	// rate it reports is per visited entry.
  29  	const setSize = 7_000_000
  30  	const touches = 49_000
  31  	// entryBytes is the size of one entry as the store holds it, rounded up
  32  	// from the encoded 57 to what an in-memory slice costs for the same record.
  33  	const entryBytes = 70
  34  
  35  	set := make([]utxo.Entry, setSize)
  36  	for i := 0; i < setSize; i++ {
  37  		var op wire.OutPoint
  38  		binary.BigEndian.PutUint64(op.Hash[:8], uint64(i)*0x9E3779B97F4A7C15)
  39  		binary.BigEndian.PutUint64(op.Hash[8:16], uint64(i)<<1|1)
  40  		op.Index = uint32(i % 7)
  41  		set[i] = utxo.Entry{OutPoint: op, Value: int64(i), Height: 1}
  42  	}
  43  	// Sort the way the store will: the generator above is not monotonic in the
  44  	// hash's byte order.
  45  	sortEntries(set)
  46  
  47  	ts := make([]utxo.Touch, 0, touches)
  48  	for i := 0; i < touches; i++ {
  49  		idx := (i * 7919) % setSize
  50  		ts = append(ts, utxo.Touch{OutPoint: set[idx].OutPoint, Height: 2, Spent: true})
  51  	}
  52  	for i := 0; i < touches; i++ {
  53  		var op wire.OutPoint
  54  		binary.BigEndian.PutUint64(op.Hash[16:24], uint64(i)*0x100000001B3)
  55  		op.Index = uint32(i)
  56  		ts = append(ts, utxo.Touch{OutPoint: op, Height: 2, New: utxo.Entry{OutPoint: op, Value: 1, Height: 2}})
  57  	}
  58  
  59  	start := time.Now()
  60  	out, err := utxo.ApplyRange(set, ts)
  61  	if err != nil {
  62  		t.Fatal(err)
  63  	}
  64  	elapsed := time.Since(start)
  65  	visited := len(set) + len(ts)
  66  	perSec := float64(visited) / elapsed.Seconds()
  67  	t.Logf("set %d, touches %d, touched/set %.0fx, result %d", setSize, len(ts), float64(len(set))/float64(len(ts)), len(out))
  68  	t.Logf("merge of %d entries in %s: %.1f M entries/s, %.0f MB/s of entries", visited, elapsed.Round(time.Millisecond), perSec/1e6, perSec*entryBytes/1e6)
  69  	// At the pre-fork size the same ratio is 70.98 M entries and 980,000
  70  	// touches.
  71  	atScale := (70.98e6 + 0.98e6) / perSec
  72  	t.Logf("extrapolated to the pre-fork set (70.98 M entries + 0.98 M touches): %.1f s per rebuild, one core", atScale)
  73  }
  74  
  75  func sortEntries(s []utxo.Entry) {
  76  	sort.Slice(s, func(i, j int) bool { return utxo.LessEntry(s[i], s[j]) })
  77  }
  78