Skip to content

Commit d212535

Browse files
authored
cmd/swarm/swarm-smoke: refactor generateEndpoints (ethereum#19006)
1 parent 7f55b0c commit d212535

File tree

6 files changed

+214
-191
lines changed

6 files changed

+214
-191
lines changed

cmd/swarm/swarm-smoke/feed_upload_and_sync.go

+40-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bytes"
55
"crypto/md5"
6-
crand "crypto/rand"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -16,7 +15,9 @@ import (
1615
"github.com/ethereum/go-ethereum/common/hexutil"
1716
"github.com/ethereum/go-ethereum/crypto"
1817
"github.com/ethereum/go-ethereum/log"
18+
"github.com/ethereum/go-ethereum/metrics"
1919
"github.com/ethereum/go-ethereum/swarm/storage/feed"
20+
"github.com/ethereum/go-ethereum/swarm/testutil"
2021
"github.com/pborman/uuid"
2122
cli "gopkg.in/urfave/cli.v1"
2223
)
@@ -25,13 +26,28 @@ const (
2526
feedRandomDataLength = 8
2627
)
2728

28-
// TODO: retrieve with manifest + extract repeating code
29-
func feedUploadAndSync(c *cli.Context) error {
30-
defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size (kb)", filesize) }(time.Now())
29+
func feedUploadAndSyncCmd(ctx *cli.Context, tuid string) error {
30+
errc := make(chan error)
3131

32-
generateEndpoints(scheme, cluster, appName, from, to)
32+
go func() {
33+
errc <- feedUploadAndSync(ctx, tuid)
34+
}()
3335

34-
log.Info("generating and uploading feeds to " + endpoints[0] + " and syncing")
36+
select {
37+
case err := <-errc:
38+
if err != nil {
39+
metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1)
40+
}
41+
return err
42+
case <-time.After(time.Duration(timeout) * time.Second):
43+
metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1)
44+
45+
return fmt.Errorf("timeout after %v sec", timeout)
46+
}
47+
}
48+
49+
func feedUploadAndSync(c *cli.Context, tuid string) error {
50+
log.Info("generating and uploading feeds to " + httpEndpoint(hosts[0]) + " and syncing")
3551

3652
// create a random private key to sign updates with and derive the address
3753
pkFile, err := ioutil.TempFile("", "swarm-feed-smoke-test")
@@ -85,7 +101,7 @@ func feedUploadAndSync(c *cli.Context) error {
85101

86102
// create feed manifest, topic only
87103
var out bytes.Buffer
88-
cmd := exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--topic", topicHex, "--user", userHex)
104+
cmd := exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--topic", topicHex, "--user", userHex)
89105
cmd.Stdout = &out
90106
log.Debug("create feed manifest topic cmd", "cmd", cmd)
91107
err = cmd.Run()
@@ -100,7 +116,7 @@ func feedUploadAndSync(c *cli.Context) error {
100116
out.Reset()
101117

102118
// create feed manifest, subtopic only
103-
cmd = exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--name", subTopicHex, "--user", userHex)
119+
cmd = exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--name", subTopicHex, "--user", userHex)
104120
cmd.Stdout = &out
105121
log.Debug("create feed manifest subtopic cmd", "cmd", cmd)
106122
err = cmd.Run()
@@ -115,7 +131,7 @@ func feedUploadAndSync(c *cli.Context) error {
115131
out.Reset()
116132

117133
// create feed manifest, merged topic
118-
cmd = exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--topic", topicHex, "--name", subTopicHex, "--user", userHex)
134+
cmd = exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--topic", topicHex, "--name", subTopicHex, "--user", userHex)
119135
cmd.Stdout = &out
120136
log.Debug("create feed manifest mergetopic cmd", "cmd", cmd)
121137
err = cmd.Run()
@@ -141,7 +157,7 @@ func feedUploadAndSync(c *cli.Context) error {
141157
dataHex := hexutil.Encode(data)
142158

143159
// update with topic
144-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, dataHex)
160+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, dataHex)
145161
cmd.Stdout = &out
146162
log.Debug("update feed manifest topic cmd", "cmd", cmd)
147163
err = cmd.Run()
@@ -152,7 +168,7 @@ func feedUploadAndSync(c *cli.Context) error {
152168
out.Reset()
153169

154170
// update with subtopic
155-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--name", subTopicHex, dataHex)
171+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--name", subTopicHex, dataHex)
156172
cmd.Stdout = &out
157173
log.Debug("update feed manifest subtopic cmd", "cmd", cmd)
158174
err = cmd.Run()
@@ -163,7 +179,7 @@ func feedUploadAndSync(c *cli.Context) error {
163179
out.Reset()
164180

165181
// update with merged topic
166-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, "--name", subTopicHex, dataHex)
182+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, "--name", subTopicHex, dataHex)
167183
cmd.Stdout = &out
168184
log.Debug("update feed manifest merged topic cmd", "cmd", cmd)
169185
err = cmd.Run()
@@ -177,36 +193,33 @@ func feedUploadAndSync(c *cli.Context) error {
177193

178194
// retrieve the data
179195
wg := sync.WaitGroup{}
180-
for _, endpoint := range endpoints {
196+
for _, host := range hosts {
181197
// raw retrieve, topic only
182198
for _, hex := range []string{topicHex, subTopicOnlyHex, mergedSubTopicHex} {
183199
wg.Add(1)
184200
ruid := uuid.New()[:8]
185201
go func(hex string, endpoint string, ruid string) {
186202
for {
187-
err := fetchFeed(hex, userHex, endpoint, dataHash, ruid)
203+
err := fetchFeed(hex, userHex, httpEndpoint(host), dataHash, ruid)
188204
if err != nil {
189205
continue
190206
}
191207

192208
wg.Done()
193209
return
194210
}
195-
}(hex, endpoint, ruid)
196-
211+
}(hex, httpEndpoint(host), ruid)
197212
}
198213
}
199214
wg.Wait()
200215
log.Info("all endpoints synced random data successfully")
201216

202217
// upload test file
203-
seed := int(time.Now().UnixNano() / 1e6)
204-
log.Info("feed uploading to "+endpoints[0]+" and syncing", "seed", seed)
218+
log.Info("feed uploading to "+httpEndpoint(hosts[0])+" and syncing", "seed", seed)
205219

206-
h = md5.New()
207-
r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h)
220+
randomBytes := testutil.RandomBytes(seed, filesize*1000)
208221

209-
hash, err := upload(r, filesize*1000, endpoints[0])
222+
hash, err := upload(randomBytes, httpEndpoint(hosts[0]))
210223
if err != nil {
211224
return err
212225
}
@@ -220,7 +233,7 @@ func feedUploadAndSync(c *cli.Context) error {
220233
log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fileHash))
221234

222235
// update file with topic
223-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, multihashHex)
236+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, multihashHex)
224237
cmd.Stdout = &out
225238
err = cmd.Run()
226239
if err != nil {
@@ -230,7 +243,7 @@ func feedUploadAndSync(c *cli.Context) error {
230243
out.Reset()
231244

232245
// update file with subtopic
233-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--name", subTopicHex, multihashHex)
246+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--name", subTopicHex, multihashHex)
234247
cmd.Stdout = &out
235248
err = cmd.Run()
236249
if err != nil {
@@ -240,7 +253,7 @@ func feedUploadAndSync(c *cli.Context) error {
240253
out.Reset()
241254

242255
// update file with merged topic
243-
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, "--name", subTopicHex, multihashHex)
256+
cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, "--name", subTopicHex, multihashHex)
244257
cmd.Stdout = &out
245258
err = cmd.Run()
246259
if err != nil {
@@ -251,23 +264,23 @@ func feedUploadAndSync(c *cli.Context) error {
251264

252265
time.Sleep(3 * time.Second)
253266

254-
for _, endpoint := range endpoints {
267+
for _, host := range hosts {
255268

256269
// manifest retrieve, topic only
257270
for _, url := range []string{manifestWithTopic, manifestWithSubTopic, manifestWithMergedTopic} {
258271
wg.Add(1)
259272
ruid := uuid.New()[:8]
260273
go func(url string, endpoint string, ruid string) {
261274
for {
262-
err := fetch(url, endpoint, fileHash, ruid)
275+
err := fetch(url, endpoint, fileHash, ruid, "")
263276
if err != nil {
264277
continue
265278
}
266279

267280
wg.Done()
268281
return
269282
}
270-
}(url, endpoint, ruid)
283+
}(url, httpEndpoint(host), ruid)
271284
}
272285

273286
}

cmd/swarm/swarm-smoke/main.go

+25-45
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,15 @@ var (
3737
)
3838

3939
var (
40-
endpoints []string
41-
includeLocalhost bool
42-
cluster string
43-
appName string
44-
scheme string
45-
filesize int
46-
syncDelay int
47-
from int
48-
to int
49-
verbosity int
50-
timeout int
51-
single bool
40+
allhosts string
41+
hosts []string
42+
filesize int
43+
syncDelay int
44+
httpPort int
45+
wsPort int
46+
verbosity int
47+
timeout int
48+
single bool
5249
)
5350

5451
func main() {
@@ -59,39 +56,22 @@ func main() {
5956

6057
app.Flags = []cli.Flag{
6158
cli.StringFlag{
62-
Name: "cluster-endpoint",
63-
Value: "prod",
64-
Usage: "cluster to point to (prod or a given namespace)",
65-
Destination: &cluster,
66-
},
67-
cli.StringFlag{
68-
Name: "app",
69-
Value: "swarm",
70-
Usage: "application to point to (swarm or swarm-private)",
71-
Destination: &appName,
59+
Name: "hosts",
60+
Value: "",
61+
Usage: "comma-separated list of swarm hosts",
62+
Destination: &allhosts,
7263
},
7364
cli.IntFlag{
74-
Name: "cluster-from",
75-
Value: 8501,
76-
Usage: "swarm node (from)",
77-
Destination: &from,
65+
Name: "http-port",
66+
Value: 80,
67+
Usage: "http port",
68+
Destination: &httpPort,
7869
},
7970
cli.IntFlag{
80-
Name: "cluster-to",
81-
Value: 8512,
82-
Usage: "swarm node (to)",
83-
Destination: &to,
84-
},
85-
cli.StringFlag{
86-
Name: "cluster-scheme",
87-
Value: "http",
88-
Usage: "http or https",
89-
Destination: &scheme,
90-
},
91-
cli.BoolFlag{
92-
Name: "include-localhost",
93-
Usage: "whether to include localhost:8500 as an endpoint",
94-
Destination: &includeLocalhost,
71+
Name: "ws-port",
72+
Value: 8546,
73+
Usage: "ws port",
74+
Destination: &wsPort,
9575
},
9676
cli.IntFlag{
9777
Name: "filesize",
@@ -140,25 +120,25 @@ func main() {
140120
Name: "upload_and_sync",
141121
Aliases: []string{"c"},
142122
Usage: "upload and sync",
143-
Action: wrapCliCommand("upload-and-sync", true, uploadAndSync),
123+
Action: wrapCliCommand("upload-and-sync", uploadAndSyncCmd),
144124
},
145125
{
146126
Name: "feed_sync",
147127
Aliases: []string{"f"},
148128
Usage: "feed update generate, upload and sync",
149-
Action: wrapCliCommand("feed-and-sync", true, feedUploadAndSync),
129+
Action: wrapCliCommand("feed-and-sync", feedUploadAndSyncCmd),
150130
},
151131
{
152132
Name: "upload_speed",
153133
Aliases: []string{"u"},
154134
Usage: "measure upload speed",
155-
Action: wrapCliCommand("upload-speed", true, uploadSpeed),
135+
Action: wrapCliCommand("upload-speed", uploadSpeedCmd),
156136
},
157137
{
158138
Name: "sliding_window",
159139
Aliases: []string{"s"},
160140
Usage: "measure network aggregate capacity",
161-
Action: wrapCliCommand("sliding-window", false, slidingWindow),
141+
Action: wrapCliCommand("sliding-window", slidingWindowCmd),
162142
},
163143
}
164144

0 commit comments

Comments
 (0)