summaryrefslogtreecommitdiffstats
path: root/BENCHMARKS.md
blob: 14a861cf26060ec652a2c37289852c0be146c393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Nostr Library Benchmarks

This directory contains comprehensive benchmarks comparing three popular Go Nostr libraries:

- **NWIO** (`northwest.io/nostr`) - This library
- **NBD** (`github.com/nbd-wtf/go-nostr`) - Popular community library
- **Fiat** (`fiatjaf.com/nostr`) - Original implementation by Fiatjaf

## Benchmark Categories

### Event Operations
- **Unmarshal**: Parsing JSON into Event struct
- **Marshal**: Serializing Event struct to JSON
- **Serialize**: Canonical serialization for ID computation
- **ComputeID**: Computing event ID hash
- **Sign**: Signing events with private key
- **Verify**: Verifying event signatures

### Key Operations
- **GenerateKey**: Generating new private keys

### Filter Operations
- **FilterMatch**: Simple filter matching (kind, author)
- **FilterMatchComplex**: Complex filter matching (with tags, prefix matching)

## Running Benchmarks

**Important**: The comparison benchmarks require the `benchcmp` build tag to avoid polluting the module dependencies with competitor libraries.

### Quick Start

Run all benchmarks (automatically handles dependencies):
```bash
./run_benchmarks.sh
```

Or manually:
```bash
# First, get the comparison dependencies
go get -tags=benchcmp -t ./...

# Then run the benchmarks
go test -tags=benchcmp -bench=. -benchmem -benchtime=1s
```

### Specific Benchmark Groups

Event unmarshaling:
```bash
go test -tags=benchcmp -bench=BenchmarkEventUnmarshal -benchmem
```

Event signing:
```bash
go test -tags=benchcmp -bench=BenchmarkEventSign -benchmem
```

Event verification:
```bash
go test -tags=benchcmp -bench=BenchmarkEventVerify -benchmem
```

Filter matching:
```bash
go test -tags=benchcmp -bench=BenchmarkFilterMatch -benchmem
```

### Compare Single Library

NWIO only:
```bash
go test -tags=benchcmp -bench='.*_NWIO' -benchmem
```

NBD only:
```bash
go test -tags=benchcmp -bench='.*_NBD' -benchmem
```

Fiat only:
```bash
go test -tags=benchcmp -bench='.*_Fiat' -benchmem
```

## Analyzing Results

Use `benchstat` for statistical analysis:

```bash
# Install benchstat
go install golang.org/x/perf/cmd/benchstat@latest

# Run benchmarks multiple times and compare
go test -tags=benchcmp -bench=. -benchmem -count=10 > results.txt
benchstat results.txt
```

Compare two specific libraries:
```bash
go test -tags=benchcmp -bench='.*_NWIO' -benchmem -count=10 > nwio.txt
go test -tags=benchcmp -bench='.*_NBD' -benchmem -count=10 > nbd.txt
benchstat nwio.txt nbd.txt
```

## Understanding the Output

Example output:
```
BenchmarkEventSign_NWIO-24    50000    35421 ns/op    1024 B/op    12 allocs/op
```

- `50000`: Number of iterations
- `35421 ns/op`: Nanoseconds per operation (lower is better)
- `1024 B/op`: Bytes allocated per operation (lower is better)
- `12 allocs/op`: Memory allocations per operation (lower is better)

## Performance Tips

1. **Event Unmarshaling**: Critical for relay implementations
2. **Event Signing**: Important for client implementations
3. **Event Verification**: Critical for all implementations
4. **Filter Matching**: Important for relay implementations with many subscriptions

## Notes

- All benchmarks use realistic event data
- Benchmarks run with default Go test timeout
- Results may vary based on hardware and system load
- Use `-benchtime=5s` for more stable results on noisy systems