package utxo_test import ( "encoding/binary" "sort" "testing" "time" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestHavocProbe measures the cost of a set rebuild against the shape of the // Bitcoin pre-fork set: 70 million entries, keyed by real-looking hashes, with // the touches of a 10,000-block range spread over it. // // It is the merge half of the arithmetic that decides the checkpoint spacing, // run on one core of the machine the work is done on. It does not model the // disk: the plan's rebuild figure is the stream, and this number is the part // that is not I/O. func TestHavocProbe(t *testing.T) { if testing.Short() { t.Skip("probe is long") } // The pre-fork set is about 70 million entries and a 10,000-block range // touches about half a million of them. The probe scales both by the same // factor on a small machine: the ratio is what decides the design, and the // rate it reports is per visited entry. const setSize = 7_000_000 const touches = 49_000 // entryBytes is the size of one entry as the store holds it, rounded up // from the encoded 57 to what an in-memory slice costs for the same record. const entryBytes = 70 set := make([]utxo.Entry, setSize) for i := 0; i < setSize; i++ { var op wire.OutPoint binary.BigEndian.PutUint64(op.Hash[:8], uint64(i)*0x9E3779B97F4A7C15) binary.BigEndian.PutUint64(op.Hash[8:16], uint64(i)<<1|1) op.Index = uint32(i % 7) set[i] = utxo.Entry{OutPoint: op, Value: int64(i), Height: 1} } // Sort the way the store will: the generator above is not monotonic in the // hash's byte order. sortEntries(set) ts := make([]utxo.Touch, 0, touches) for i := 0; i < touches; i++ { idx := (i * 7919) % setSize ts = append(ts, utxo.Touch{OutPoint: set[idx].OutPoint, Height: 2, Spent: true}) } for i := 0; i < touches; i++ { var op wire.OutPoint binary.BigEndian.PutUint64(op.Hash[16:24], uint64(i)*0x100000001B3) op.Index = uint32(i) ts = append(ts, utxo.Touch{OutPoint: op, Height: 2, New: utxo.Entry{OutPoint: op, Value: 1, Height: 2}}) } start := time.Now() out, err := utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } elapsed := time.Since(start) visited := len(set) + len(ts) perSec := float64(visited) / elapsed.Seconds() t.Logf("set %d, touches %d, touched/set %.0fx, result %d", setSize, len(ts), float64(len(set))/float64(len(ts)), len(out)) 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) // At the pre-fork size the same ratio is 70.98 M entries and 980,000 // touches. atScale := (70.98e6 + 0.98e6) / perSec t.Logf("extrapolated to the pre-fork set (70.98 M entries + 0.98 M touches): %.1f s per rebuild, one core", atScale) } func sortEntries(s []utxo.Entry) { sort.Slice(s, func(i, j int) bool { return utxo.LessEntry(s[i], s[j]) }) }