-
Notifications
You must be signed in to change notification settings - Fork 383
/
main.go
256 lines (225 loc) · 7.01 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// +build !windows,!darwin
package main
import (
"context"
"encoding/base64"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"time"
"github.com/zcalusic/sysinfo"
"go.uber.org/zap"
"moul.io/u"
"berty.tech/berty/v2/go/internal/config"
"berty.tech/berty/v2/go/internal/initutil"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
func main() {
i := &integration{
startedAt: time.Now(),
previousStep: time.Now(),
benchmarks: []benchmark{},
}
i.checkErr(i.init(), "init")
i.checkErr(i.testbotAdd(), "testbot.add")
// i.checkErr(i.testbotFoo(), "testbot.echo")
// i.checkErr(i.betabotAdd(), "betabot.add")
// i.checkErr(i.testbotBar(), "testbot.bar")
// i.checkErr(i.testbotBar(), "services.register")
// i.checkErr(i.testbotBar(), "services.use")
// i.checkErr(i.rdvpConnect(), "connect-to-rdvp")
i.cleanup()
totalDuration := time.Since(i.startedAt)
fmt.Fprintf(os.Stderr, "[+] finished - %s\n", u.ShortDuration(totalDuration))
i.addBenchmark("total", totalDuration)
if i.opts.benchmark {
fmt.Println(i.benchmarkResult())
}
}
type integration struct {
tempdir string
startedAt time.Time
previousStep time.Time
manager initutil.Manager
logger *zap.Logger
ctx context.Context
client messengertypes.MessengerServiceClient
opts struct {
betabotAddr string
testbotAddr string
benchmark bool
}
benchmarks []benchmark
}
func (i *integration) init() error {
var err error
i.tempdir, err = ioutil.TempDir("", "berty-integration")
if err != nil {
return err
}
i.manager.Datastore.Dir = i.tempdir
i.manager.Logging.Format = "light-color"
i.manager.Logging.Filters = "warn:*,-ipfs.* error+:*" // (level==warn for everything except ipfs.*) || (levels >= error)
i.manager.Logging.Service = "berty-integration"
fs := flag.NewFlagSet("integration", flag.ExitOnError)
fs.StringVar(&i.opts.betabotAddr, "integration.betabot", config.Config.Berty.Contacts["betabot-dev"].Link, "betabot addr")
fs.StringVar(&i.opts.testbotAddr, "integration.testbot", config.Config.Berty.Contacts["testbot-dev"].Link, "testbot addr")
fs.BoolVar(&i.opts.benchmark, "integration.benchmark", false, "print benchmark result in JSON")
i.manager.SetupLoggingFlags(fs)
i.manager.SetupLocalMessengerServerFlags(fs)
i.manager.SetupEmptyGRPCListenersFlags(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
return err
}
logger, err := i.manager.GetLogger()
if err != nil {
return err
}
i.logger = logger.Named("inte")
i.logger.Debug("init", zap.Any("opts", i.opts))
i.ctx = context.Background()
i.client, err = i.manager.GetMessengerClient()
if err != nil {
return err
}
return nil
}
func (i *integration) testbotAdd() error {
ctx, cancel := context.WithTimeout(i.ctx, 60*time.Second)
defer cancel()
// parse invite URL
parsed, err := i.client.ParseDeepLink(ctx, &messengertypes.ParseDeepLink_Request{Link: i.opts.testbotAddr})
if err != nil {
return err
}
// subscribe to events
s, err := i.client.EventStream(ctx, &messengertypes.EventStream_Request{})
if err != nil {
return err
}
defer func() {
if err := s.CloseSend(); err != nil {
i.logger.Warn("CloseSend failed", zap.Error(err))
}
}()
// send contact request
_, err = i.client.SendContactRequest(ctx, &messengertypes.SendContactRequest_Request{
BertyID: parsed.GetLink().GetBertyID(),
})
if err != nil {
return err
}
var contactAccepted, conversationReady, welcomeReceived bool
for {
if contactAccepted && conversationReady && welcomeReceived {
// this test can be racy, i.e., if we receive more events,
// but it's not important in the context of this integration test.
time.Sleep(100 * time.Millisecond)
// success
return nil
}
ret, err := s.Recv()
if err != nil {
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(ctx.Err(), context.DeadlineExceeded) {
return fmt.Errorf("timeout: %w", err)
}
return fmt.Errorf("streaming error: %w", err)
}
event := ret.GetEvent()
payload, err := event.UnmarshalPayload()
if err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
i.logger.Debug("new event", zap.Any("event", event))
switch event.GetType() {
case messengertypes.StreamEvent_TypeContactUpdated:
contact := payload.(*messengertypes.StreamEvent_ContactUpdated).Contact
if contact.State == messengertypes.Contact_Accepted {
contactAccepted = true
}
case messengertypes.StreamEvent_TypeConversationUpdated:
conversation := payload.(*messengertypes.StreamEvent_ConversationUpdated).Conversation
if base64.RawURLEncoding.EncodeToString(parsed.GetLink().GetBertyID().GetAccountPK()) != conversation.ContactPublicKey {
continue
}
conversationReady = true
case messengertypes.StreamEvent_TypeInteractionUpdated:
interaction := payload.(*messengertypes.StreamEvent_InteractionUpdated).Interaction
if interaction.GetType() == messengertypes.AppMessage_TypeUserMessage {
payload, err := interaction.UnmarshalPayload()
if err != nil {
return err
}
body := payload.(*messengertypes.AppMessage_UserMessage).Body
if base64.RawURLEncoding.EncodeToString(parsed.GetLink().GetBertyID().GetAccountPK()) != interaction.Conversation.ContactPublicKey {
continue
}
if body != "welcome to testbot1" {
continue
}
welcomeReceived = true
}
default:
// ignore other messages
}
}
}
func (i *integration) cleanup() {
i.manager.Close(nil)
time.Sleep(100 * time.Millisecond)
if i.tempdir != "" {
os.RemoveAll(i.tempdir)
}
}
func (i *integration) checkErr(err error, step string) {
if err != nil {
fmt.Fprintf(os.Stderr, "[-] %s: %v - %s\n", step, err, u.ShortDuration(time.Since(i.startedAt)))
i.cleanup()
os.Exit(1)
} else {
i.addBenchmark(step, time.Since(i.previousStep))
i.previousStep = time.Now()
fmt.Fprintf(os.Stderr, "[+] %s - %s\n", step, u.ShortDuration(time.Since(i.startedAt)))
}
}
func (i *integration) addBenchmark(name string, duration time.Duration) {
dur := duration.Nanoseconds()
i.benchmarks = append(i.benchmarks, benchmark{
Name: name,
Iterations: 1,
TimeUnit: "ns",
RealTime: dur,
CPUTime: dur, // FIXME: compute real CPU time
})
}
type benchmark struct {
Name string `json:"name"`
Iterations int `json:"iterations"`
RealTime int64 `json:"real_time"`
CPUTime int64 `json:"cpu_time"`
TimeUnit string `json:"time_unit"`
}
func (i *integration) benchmarkResult() string {
type result struct {
Context struct {
Date string `json:"date"`
NumCPUs int `json:"num_cpus"`
MhzPerCPU uint `json:"mhz_per_cpu"`
CPUScalingEnabled bool `json:"cpu_scaling_enabled"`
LibraryBuildType string `json:"library_build_type"`
} `json:"context"`
Benchmarks []benchmark `json:"benchmarks"`
}
res := result{Benchmarks: i.benchmarks}
res.Context.Date = time.Now().String()
res.Context.NumCPUs = runtime.NumCPU()
var si sysinfo.SysInfo
si.GetSysInfo()
res.Context.MhzPerCPU = si.CPU.Speed
res.Context.LibraryBuildType = "debug"
res.Context.CPUScalingEnabled = false // FIXME: dynamic
return u.PrettyJSON(res)
}