/
test_utils.go
132 lines (117 loc) · 3.04 KB
/
test_utils.go
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
130
131
132
package benchmarks
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/nbd-wtf/go-nostr"
)
func checkParsedCorrectly(t *testing.T, jevt string, evt *Event) (isBad bool) {
var canonical nostr.Event
json.Unmarshal([]byte(jevt), &canonical)
if evt.ID != canonical.ID {
t.Errorf("id is wrong: %s != %s", evt.ID, canonical.ID)
isBad = true
}
if evt.PubKey != canonical.PubKey {
t.Errorf("pubkey is wrong: %s != %s", evt.PubKey, canonical.PubKey)
isBad = true
}
if evt.Sig != canonical.Sig {
t.Errorf("sig is wrong: %s != %s", evt.Sig, canonical.Sig)
isBad = true
}
if evt.Content != canonical.Content {
t.Errorf("content is wrong: %s != %s", evt.Content, canonical.Content)
isBad = true
}
if evt.Kind != canonical.Kind {
t.Errorf("kind is wrong: %d != %d", evt.Kind, canonical.Kind)
isBad = true
}
if evt.CreatedAt != Timestamp(canonical.CreatedAt.Unix()) {
t.Errorf("created_at is wrong: %v != %v", evt.CreatedAt, canonical.CreatedAt)
isBad = true
}
if len(evt.Tags) != len(canonical.Tags) {
t.Errorf("tag number is wrong: %v != %v", len(evt.Tags), len(canonical.Tags))
isBad = true
}
for i := range evt.Tags {
if len(evt.Tags[i]) != len(canonical.Tags[i]) {
t.Errorf("tag[%d] length is wrong: `%v` != `%v`", i, len(evt.Tags[i]), len(canonical.Tags[i]))
isBad = true
}
for j := range evt.Tags[i] {
if evt.Tags[i][j] != canonical.Tags[i][j] {
t.Errorf("tag[%d][%d] is wrong: `%s` != `%s`", i, j, evt.Tags[i][j], canonical.Tags[i][j])
isBad = true
}
}
}
return isBad
}
func loadLines() []string {
b, err := ioutil.ReadFile("data.json")
if err != nil {
panic(err)
}
text := string(b)
lines := strings.Split(text, "\n")
return lines[0 : len(lines)-1]
}
func loadEvents() []string {
lines := loadLines()
events := make([]string, 0, len(lines))
for _, line := range lines {
if strings.HasPrefix(line, "[\"EVENT") {
events = append(events, line[13:len(line)-1])
}
}
return events
}
type binaryEvent struct {
binary []byte
event *Event
}
func loadEventsTLV() []binaryEvent {
events := loadEvents()
payloads := make([]binaryEvent, len(events))
for i, evtstr := range events {
evt := &Event{}
json.Unmarshal([]byte(evtstr), evt)
payloads[i].event = evt
payloads[i].binary = encodeEventTLV(evt)
}
return payloads
}
func loadEventsLeaner() []binaryEvent {
events := loadEvents()
payloads := make([]binaryEvent, len(events))
for i, evtstr := range events {
evt := &Event{}
json.Unmarshal([]byte(evtstr), evt)
payloads[i].event = evt
payloads[i].binary = leanerEncode(evt.ToBinary())
}
return payloads
}
func loadEventsNson() []string {
events := loadEvents()
payloads := make([]string, len(events))
for i, evtstr := range events {
evt := &Event{}
json.Unmarshal([]byte(evtstr), evt)
payloads[i] = encodeNson(evt)
}
return payloads
}
func loadEnvelopes() []string {
events := loadEventsNson()
envelopes := make([]string, len(events))
for i, evtstr := range events {
envelopes[i] = fmt.Sprintf(`["EVENT","_",%s]`, evtstr)
}
return envelopes
}