1 package utxostore_test
2 3 import (
4 "encoding/binary"
5 "os"
6 "sort"
7 "testing"
8 9 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
10 11 "git.smesh.lol/btcd-lite/internal/utxo"
12 utxostore "git.smesh.lol/btcd-lite/internal/utxo/store"
13 "git.smesh.lol/btcd-lite/internal/wire"
14 )
15 16 func sortLikeStore(set []utxo.Entry) {
17 sort.Slice(set, func(i, j int) bool { return utxo.LessEntry(set[i], set[j]) })
18 }
19 20 // benchSet builds an in-memory set of n entries keyed like real outpoints.
21 func benchSet(n int) []utxo.Entry {
22 set := make([]utxo.Entry, n)
23 for i := 0; i < n; i++ {
24 var h [32]byte
25 binary.BigEndian.PutUint64(h[0:8], uint64(i)*0x9E3779B97F4A7C15)
26 binary.BigEndian.PutUint64(h[24:32], uint64(i))
27 set[i] = utxo.Entry{OutPoint: wire.OutPoint{Hash: h}, Value: int64(i), Height: uint32(i % 1000)}
28 }
29 // The generator is not monotonic in the hash's byte order, so sort it the
30 // way the store's sets are sorted.
31 sortLikeStore(set)
32 return set
33 }
34 35 // TestDiskMergeRate measures what the plan's rebuild figure rests on: the
36 // bytes a diff merge streams, and the rate it streams them at from disk.
37 //
38 // The numbers go into docs/PLAN.md, not into an assertion: a 70 million entry
39 // set is not a test fixture, so the probe runs a slice of it and reports the
40 // rate per entry.
41 func TestDiskMergeRate(t *testing.T) {
42 if testing.Short() {
43 t.Skip("long")
44 }
45 // The probe writes to the real disk when BTCD_LITE_PROBE_DIR names a
46 // directory on it: a temp directory on tmpfs measures memory, and the
47 // number the plan needs is a disk's.
48 dir := t.TempDir()
49 if d := os.Getenv("BTCD_LITE_PROBE_DIR"); d != "" {
50 dir = d
51 }
52 set := benchSet(2_000_000)
53 if _, err := utxostore.WriteSet(dir, 0, set); err != nil {
54 t.Fatal(err)
55 }
56 if _, err := utxostore.Promote(dir, 0); err != nil {
57 t.Fatal(err)
58 }
59 // A diff that touches one outpoint in a thousand, half spent from the set
60 // and half created.
61 var ts []utxo.Touch
62 for i := 0; i < len(set); i += 1000 {
63 ts = append(ts, utxo.Touch{OutPoint: set[i].OutPoint, Height: 1, Spent: true})
64 }
65 for i := 0; i < len(set)/1000; i++ {
66 var h [32]byte
67 binary.BigEndian.PutUint64(h[0:8], uint64(i)*0x2545F4914F6CDD1D)
68 binary.BigEndian.PutUint64(h[24:32], ^uint64(i))
69 op := wire.OutPoint{Hash: h}
70 ts = append(ts, utxo.Touch{OutPoint: op, Height: 1, New: utxo.Entry{OutPoint: op, Value: 1, Height: 1}})
71 }
72 if _, err := utxostore.CutRange(dir, 0, 1, ts); err != nil {
73 t.Fatal(err)
74 }
75 start := timeNow()
76 d, err := utxostore.LoadDiff(dir, 0, 1)
77 if err != nil {
78 t.Fatal(err)
79 }
80 name, err := utxostore.MergeRange(dir, 0, 1, d, chainhash.Hash{})
81 if err != nil {
82 t.Fatal(err)
83 }
84 elapsed := timeSince(start)
85 t.Logf("merged %d entries and %d touches from disk in %s: %.1f M entries/s, %.0f MB/s; result %s",
86 len(set), len(ts), elapsed.Round(millisecond),
87 float64(len(set)+len(ts))/elapsed.Seconds()/1e6,
88 float64(len(set)+len(ts))*utxo.EntryEncodeSize/elapsed.Seconds()/1e6, name)
89 }
90