-
Notifications
You must be signed in to change notification settings - Fork 383
/
main.go
204 lines (185 loc) · 4.87 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
package main
import (
"context"
"fmt"
"log"
"math"
"net/http"
"strconv"
"time"
"connectrpc.com/connect"
api_gen "github.com/gnolang/gnonative/api/gen/go"
"github.com/gnolang/gnonative/api/gen/go/_goconnect"
"github.com/gnolang/gnonative/service"
)
func main() {
// Start the Gno Native Kit gRPC service where the remote is gnoland.
options := []service.GnoNativeOption{
service.WithTcpAddr("localhost:0"),
service.WithUseTcpListener(),
}
service, err := service.NewGnoNativeService(options...)
if err != nil {
log.Fatalf("failed to run GnoNativeService: %v", err)
return
}
defer service.Close()
// Create a Gno Native Kit gRPC client.
client := _goconnect.NewGnoNativeServiceClient(
http.DefaultClient,
fmt.Sprintf("http://localhost:%d", service.GetTcpPort()),
)
if err := setup(client); err != nil {
log.Fatal(err)
return
}
if err := doAction(client); err != nil {
log.Fatal(err)
return
}
}
func setup(client _goconnect.GnoNativeServiceClient) error {
// gnoland already has coins for test_1. Recover the test_1 key in our temporary on-disk keybase.
_, err := client.CreateAccount(
context.Background(),
connect.NewRequest(&api_gen.CreateAccountRequest{
NameOrBech32: "test_1",
Mnemonic: "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast",
Password: "password",
}),
)
if err != nil {
return err
}
_, err = client.SelectAccount(
context.Background(),
connect.NewRequest(&api_gen.SelectAccountRequest{
NameOrBech32: "test_1",
}),
)
if err != nil {
return err
}
_, err = client.SetPassword(
context.Background(),
connect.NewRequest(&api_gen.SetPasswordRequest{
Password: "password",
}),
)
if err != nil {
return err
}
// Register test_1 with r/demo/users. Let this fail if it's already registered.
res, err := client.Call(
context.Background(),
connect.NewRequest(&api_gen.CallRequest{
GasFee: "1ugnot",
GasWanted: 10_000_000,
Msgs: []*api_gen.MsgCall{{
PackagePath: "gno.land/r/demo/users",
Fnc: "Register",
Args: []string{"", "test_1", "Profile description"},
Send: "200000000ugnot",
}}}),
)
if err != nil {
return err
}
for res.Receive() {
}
// Create the board. Let this fail if it's already created.
res, err = client.Call(
context.Background(),
connect.NewRequest(&api_gen.CallRequest{
GasFee: "1ugnot",
GasWanted: 10_000_000,
Msgs: []*api_gen.MsgCall{{
PackagePath: "gno.land/r/demo/boards",
Fnc: "CreateBoard",
Args: []string{"stresstest"},
}}}),
)
if err != nil {
return err
}
for res.Receive() {
}
// Create the thread to which we will reply many times. It's OK if a thread already exists.
res, err = client.Call(
context.Background(),
connect.NewRequest(&api_gen.CallRequest{
GasFee: "1ugnot",
GasWanted: 10_000_000,
Msgs: []*api_gen.MsgCall{{
PackagePath: "gno.land/r/demo/boards",
Fnc: "CreateThread",
Args: []string{"1", "Stress Test", "message"},
}}}),
)
if err != nil {
return err
}
for res.Receive() {
}
if res.Err() != nil {
return res.Err()
}
return nil
}
func doAction(client _goconnect.GnoNativeServiceClient) error {
postsPerCall := 50
totalPostsWanted := 1_000_000
// A script to call CreateReply postsPerCall times in one transaction.
code := `package main
import (
"gno.land/r/demo/boards"
)
func main() {
for i := 0; i < ` + strconv.Itoa(postsPerCall) + `; i++ {
boards.CreateReply(boards.BoardID(1), boards.PostID(1), boards.PostID(1), "reply")
}
}`
nCalls := 0
totalElapsed := 0.0
minElapsed := math.MaxFloat64
maxElapsed := 0.0
totalPosts := 0
fmt.Printf("nPosts, avg. for %d posts [s], min for %d posts [s], max for %d posts [s]\n", postsPerCall, postsPerCall, postsPerCall)
for totalPosts < totalPostsWanted {
start := time.Now()
res, err := client.Run(
context.Background(),
connect.NewRequest(&api_gen.RunRequest{
GasFee: "1ugnot",
// For the moment, must change MaxGas to 1_000_000_000 in
// https://github.com/gnolang/gno/blob/234a0a185c7e5ea9c74bfd1ea06deae4f656d0bb/gno.land/cmd/gnoland/start.go#L301
GasWanted: 100_000_000,
Msgs: []*api_gen.MsgRun{{
Package: code,
}}}),
)
if err != nil {
return err
}
for res.Receive() {
}
if res.Err() != nil {
return res.Err()
}
elapsed := time.Now().Sub(start)
elapsedSecs := float64(elapsed.Milliseconds()) / 1000.0
nCalls++
totalElapsed += elapsedSecs
minElapsed = math.Min(minElapsed, elapsedSecs)
maxElapsed = math.Max(maxElapsed, elapsedSecs)
totalPosts += postsPerCall
if totalPosts%1000 == 0 {
fmt.Printf("%d, %f, %f, %f\n", totalPosts, totalElapsed/float64(nCalls), minElapsed, maxElapsed)
nCalls = 0
totalElapsed = 0.0
minElapsed = 1000000.0
maxElapsed = 0.0
}
}
return nil
}