From d212535ddd5bf63a0c0b194525246480ae46c537 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Thu, 7 Feb 2019 14:38:32 +0100 Subject: [PATCH] cmd/swarm/swarm-smoke: refactor generateEndpoints (#19006) --- cmd/swarm/swarm-smoke/feed_upload_and_sync.go | 67 ++++++++----- cmd/swarm/swarm-smoke/main.go | 70 +++++-------- cmd/swarm/swarm-smoke/sliding_window.go | 56 +++++++---- cmd/swarm/swarm-smoke/upload_and_sync.go | 71 +++++++++----- cmd/swarm/swarm-smoke/upload_speed.go | 43 +++++--- cmd/swarm/swarm-smoke/util.go | 98 +++++++------------ 6 files changed, 214 insertions(+), 191 deletions(-) diff --git a/cmd/swarm/swarm-smoke/feed_upload_and_sync.go b/cmd/swarm/swarm-smoke/feed_upload_and_sync.go index a322ba89c882..6b3fed0c7e03 100644 --- a/cmd/swarm/swarm-smoke/feed_upload_and_sync.go +++ b/cmd/swarm/swarm-smoke/feed_upload_and_sync.go @@ -3,7 +3,6 @@ package main import ( "bytes" "crypto/md5" - crand "crypto/rand" "fmt" "io" "io/ioutil" @@ -16,7 +15,9 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/swarm/storage/feed" + "github.com/ethereum/go-ethereum/swarm/testutil" "github.com/pborman/uuid" cli "gopkg.in/urfave/cli.v1" ) @@ -25,13 +26,28 @@ const ( feedRandomDataLength = 8 ) -// TODO: retrieve with manifest + extract repeating code -func feedUploadAndSync(c *cli.Context) error { - defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size (kb)", filesize) }(time.Now()) +func feedUploadAndSyncCmd(ctx *cli.Context, tuid string) error { + errc := make(chan error) - generateEndpoints(scheme, cluster, appName, from, to) + go func() { + errc <- feedUploadAndSync(ctx, tuid) + }() - log.Info("generating and uploading feeds to " + endpoints[0] + " and syncing") + select { + case err := <-errc: + if err != nil { + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1) + } + return err + case <-time.After(time.Duration(timeout) * time.Second): + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1) + + return fmt.Errorf("timeout after %v sec", timeout) + } +} + +func feedUploadAndSync(c *cli.Context, tuid string) error { + log.Info("generating and uploading feeds to " + httpEndpoint(hosts[0]) + " and syncing") // create a random private key to sign updates with and derive the address pkFile, err := ioutil.TempFile("", "swarm-feed-smoke-test") @@ -85,7 +101,7 @@ func feedUploadAndSync(c *cli.Context) error { // create feed manifest, topic only var out bytes.Buffer - cmd := exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--topic", topicHex, "--user", userHex) + cmd := exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--topic", topicHex, "--user", userHex) cmd.Stdout = &out log.Debug("create feed manifest topic cmd", "cmd", cmd) err = cmd.Run() @@ -100,7 +116,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // create feed manifest, subtopic only - cmd = exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--name", subTopicHex, "--user", userHex) + cmd = exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--name", subTopicHex, "--user", userHex) cmd.Stdout = &out log.Debug("create feed manifest subtopic cmd", "cmd", cmd) err = cmd.Run() @@ -115,7 +131,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // create feed manifest, merged topic - cmd = exec.Command("swarm", "--bzzapi", endpoints[0], "feed", "create", "--topic", topicHex, "--name", subTopicHex, "--user", userHex) + cmd = exec.Command("swarm", "--bzzapi", httpEndpoint(hosts[0]), "feed", "create", "--topic", topicHex, "--name", subTopicHex, "--user", userHex) cmd.Stdout = &out log.Debug("create feed manifest mergetopic cmd", "cmd", cmd) err = cmd.Run() @@ -141,7 +157,7 @@ func feedUploadAndSync(c *cli.Context) error { dataHex := hexutil.Encode(data) // update with topic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, dataHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, dataHex) cmd.Stdout = &out log.Debug("update feed manifest topic cmd", "cmd", cmd) err = cmd.Run() @@ -152,7 +168,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // update with subtopic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--name", subTopicHex, dataHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--name", subTopicHex, dataHex) cmd.Stdout = &out log.Debug("update feed manifest subtopic cmd", "cmd", cmd) err = cmd.Run() @@ -163,7 +179,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // update with merged topic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, "--name", subTopicHex, dataHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, "--name", subTopicHex, dataHex) cmd.Stdout = &out log.Debug("update feed manifest merged topic cmd", "cmd", cmd) err = cmd.Run() @@ -177,14 +193,14 @@ func feedUploadAndSync(c *cli.Context) error { // retrieve the data wg := sync.WaitGroup{} - for _, endpoint := range endpoints { + for _, host := range hosts { // raw retrieve, topic only for _, hex := range []string{topicHex, subTopicOnlyHex, mergedSubTopicHex} { wg.Add(1) ruid := uuid.New()[:8] go func(hex string, endpoint string, ruid string) { for { - err := fetchFeed(hex, userHex, endpoint, dataHash, ruid) + err := fetchFeed(hex, userHex, httpEndpoint(host), dataHash, ruid) if err != nil { continue } @@ -192,21 +208,18 @@ func feedUploadAndSync(c *cli.Context) error { wg.Done() return } - }(hex, endpoint, ruid) - + }(hex, httpEndpoint(host), ruid) } } wg.Wait() log.Info("all endpoints synced random data successfully") // upload test file - seed := int(time.Now().UnixNano() / 1e6) - log.Info("feed uploading to "+endpoints[0]+" and syncing", "seed", seed) + log.Info("feed uploading to "+httpEndpoint(hosts[0])+" and syncing", "seed", seed) - h = md5.New() - r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h) + randomBytes := testutil.RandomBytes(seed, filesize*1000) - hash, err := upload(r, filesize*1000, endpoints[0]) + hash, err := upload(randomBytes, httpEndpoint(hosts[0])) if err != nil { return err } @@ -220,7 +233,7 @@ func feedUploadAndSync(c *cli.Context) error { log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fileHash)) // update file with topic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, multihashHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, multihashHex) cmd.Stdout = &out err = cmd.Run() if err != nil { @@ -230,7 +243,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // update file with subtopic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--name", subTopicHex, multihashHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--name", subTopicHex, multihashHex) cmd.Stdout = &out err = cmd.Run() if err != nil { @@ -240,7 +253,7 @@ func feedUploadAndSync(c *cli.Context) error { out.Reset() // update file with merged topic - cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", endpoints[0], "feed", "update", "--topic", topicHex, "--name", subTopicHex, multihashHex) + cmd = exec.Command("swarm", "--bzzaccount", pkFile.Name(), "--bzzapi", httpEndpoint(hosts[0]), "feed", "update", "--topic", topicHex, "--name", subTopicHex, multihashHex) cmd.Stdout = &out err = cmd.Run() if err != nil { @@ -251,7 +264,7 @@ func feedUploadAndSync(c *cli.Context) error { time.Sleep(3 * time.Second) - for _, endpoint := range endpoints { + for _, host := range hosts { // manifest retrieve, topic only for _, url := range []string{manifestWithTopic, manifestWithSubTopic, manifestWithMergedTopic} { @@ -259,7 +272,7 @@ func feedUploadAndSync(c *cli.Context) error { ruid := uuid.New()[:8] go func(url string, endpoint string, ruid string) { for { - err := fetch(url, endpoint, fileHash, ruid) + err := fetch(url, endpoint, fileHash, ruid, "") if err != nil { continue } @@ -267,7 +280,7 @@ func feedUploadAndSync(c *cli.Context) error { wg.Done() return } - }(url, endpoint, ruid) + }(url, httpEndpoint(host), ruid) } } diff --git a/cmd/swarm/swarm-smoke/main.go b/cmd/swarm/swarm-smoke/main.go index 55967146052e..e9380813a38a 100644 --- a/cmd/swarm/swarm-smoke/main.go +++ b/cmd/swarm/swarm-smoke/main.go @@ -37,18 +37,15 @@ var ( ) var ( - endpoints []string - includeLocalhost bool - cluster string - appName string - scheme string - filesize int - syncDelay int - from int - to int - verbosity int - timeout int - single bool + allhosts string + hosts []string + filesize int + syncDelay int + httpPort int + wsPort int + verbosity int + timeout int + single bool ) func main() { @@ -59,39 +56,22 @@ func main() { app.Flags = []cli.Flag{ cli.StringFlag{ - Name: "cluster-endpoint", - Value: "prod", - Usage: "cluster to point to (prod or a given namespace)", - Destination: &cluster, - }, - cli.StringFlag{ - Name: "app", - Value: "swarm", - Usage: "application to point to (swarm or swarm-private)", - Destination: &appName, + Name: "hosts", + Value: "", + Usage: "comma-separated list of swarm hosts", + Destination: &allhosts, }, cli.IntFlag{ - Name: "cluster-from", - Value: 8501, - Usage: "swarm node (from)", - Destination: &from, + Name: "http-port", + Value: 80, + Usage: "http port", + Destination: &httpPort, }, cli.IntFlag{ - Name: "cluster-to", - Value: 8512, - Usage: "swarm node (to)", - Destination: &to, - }, - cli.StringFlag{ - Name: "cluster-scheme", - Value: "http", - Usage: "http or https", - Destination: &scheme, - }, - cli.BoolFlag{ - Name: "include-localhost", - Usage: "whether to include localhost:8500 as an endpoint", - Destination: &includeLocalhost, + Name: "ws-port", + Value: 8546, + Usage: "ws port", + Destination: &wsPort, }, cli.IntFlag{ Name: "filesize", @@ -140,25 +120,25 @@ func main() { Name: "upload_and_sync", Aliases: []string{"c"}, Usage: "upload and sync", - Action: wrapCliCommand("upload-and-sync", true, uploadAndSync), + Action: wrapCliCommand("upload-and-sync", uploadAndSyncCmd), }, { Name: "feed_sync", Aliases: []string{"f"}, Usage: "feed update generate, upload and sync", - Action: wrapCliCommand("feed-and-sync", true, feedUploadAndSync), + Action: wrapCliCommand("feed-and-sync", feedUploadAndSyncCmd), }, { Name: "upload_speed", Aliases: []string{"u"}, Usage: "measure upload speed", - Action: wrapCliCommand("upload-speed", true, uploadSpeed), + Action: wrapCliCommand("upload-speed", uploadSpeedCmd), }, { Name: "sliding_window", Aliases: []string{"s"}, Usage: "measure network aggregate capacity", - Action: wrapCliCommand("sliding-window", false, slidingWindow), + Action: wrapCliCommand("sliding-window", slidingWindowCmd), }, } diff --git a/cmd/swarm/swarm-smoke/sliding_window.go b/cmd/swarm/swarm-smoke/sliding_window.go index 3c4b1c79add2..d313bbc37a54 100644 --- a/cmd/swarm/swarm-smoke/sliding_window.go +++ b/cmd/swarm/swarm-smoke/sliding_window.go @@ -17,50 +17,62 @@ package main import ( - "crypto/md5" - crand "crypto/rand" + "bytes" "fmt" - "io" "math/rand" "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/swarm/testutil" "github.com/pborman/uuid" cli "gopkg.in/urfave/cli.v1" ) -var seed = time.Now().UTC().UnixNano() - -func init() { - rand.Seed(seed) -} - type uploadResult struct { hash string digest []byte } -func slidingWindow(c *cli.Context) error { - generateEndpoints(scheme, cluster, appName, from, to) +func slidingWindowCmd(ctx *cli.Context, tuid string) error { + errc := make(chan error) + + go func() { + errc <- slidingWindow(ctx, tuid) + }() + + select { + case err := <-errc: + if err != nil { + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1) + } + return err + case <-time.After(time.Duration(timeout) * time.Second): + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1) + + return fmt.Errorf("timeout after %v sec", timeout) + } +} + +func slidingWindow(ctx *cli.Context, tuid string) error { hashes := []uploadResult{} //swarm hashes of the uploads - nodes := to - from + nodes := len(hosts) const iterationTimeout = 30 * time.Second - log.Info("sliding window test started", "nodes", nodes, "filesize(kb)", filesize, "timeout", timeout) + log.Info("sliding window test started", "tuid", tuid, "nodes", nodes, "filesize(kb)", filesize, "timeout", timeout) uploadedBytes := 0 networkDepth := 0 errored := false outer: for { - log.Info("uploading to "+endpoints[0]+" and syncing", "seed", seed) + log.Info("uploading to "+httpEndpoint(hosts[0])+" and syncing", "seed", seed) - h := md5.New() - r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h) t1 := time.Now() - hash, err := upload(r, filesize*1000, endpoints[0]) + randomBytes := testutil.RandomBytes(seed, filesize*1000) + + hash, err := upload(randomBytes, httpEndpoint(hosts[0])) if err != nil { log.Error(err.Error()) return err @@ -68,7 +80,11 @@ outer: metrics.GetOrRegisterResettingTimer("sliding-window.upload-time", nil).UpdateSince(t1) - fhash := h.Sum(nil) + fhash, err := digest(bytes.NewReader(randomBytes)) + if err != nil { + log.Error(err.Error()) + return err + } log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash), "sleeping", syncDelay) hashes = append(hashes, uploadResult{hash: hash, digest: fhash}) @@ -88,10 +104,10 @@ outer: metrics.GetOrRegisterCounter("sliding-window.single.error", nil).Inc(1) break inner default: - randIndex := 1 + rand.Intn(len(endpoints)-1) + idx := 1 + rand.Intn(len(hosts)-1) ruid := uuid.New()[:8] start := time.Now() - err := fetch(v.hash, endpoints[randIndex], v.digest, ruid) + err := fetch(v.hash, httpEndpoint(hosts[idx]), v.digest, ruid, "") if err != nil { continue inner } diff --git a/cmd/swarm/swarm-smoke/upload_and_sync.go b/cmd/swarm/swarm-smoke/upload_and_sync.go index c67989651928..b2858e227564 100644 --- a/cmd/swarm/swarm-smoke/upload_and_sync.go +++ b/cmd/swarm/swarm-smoke/upload_and_sync.go @@ -17,84 +17,109 @@ package main import ( - "crypto/md5" - crand "crypto/rand" + "bytes" "fmt" - "io" "math/rand" "sync" "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/swarm/testutil" "github.com/pborman/uuid" cli "gopkg.in/urfave/cli.v1" ) -func uploadAndSync(c *cli.Context) error { - generateEndpoints(scheme, cluster, appName, from, to) - seed := int(time.Now().UnixNano() / 1e6) +func uploadAndSyncCmd(ctx *cli.Context, tuid string) error { + randomBytes := testutil.RandomBytes(seed, filesize*1000) - log.Info("uploading to "+endpoints[0]+" and syncing", "seed", seed) + errc := make(chan error) - h := md5.New() - r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h) + go func() { + errc <- uplaodAndSync(ctx, randomBytes, tuid) + }() + + select { + case err := <-errc: + if err != nil { + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1) + } + return err + case <-time.After(time.Duration(timeout) * time.Second): + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1) + + // trigger debug functionality on randomBytes + + return fmt.Errorf("timeout after %v sec", timeout) + } +} + +func uplaodAndSync(c *cli.Context, randomBytes []byte, tuid string) error { + log.Info("uploading to "+httpEndpoint(hosts[0])+" and syncing", "tuid", tuid, "seed", seed) t1 := time.Now() - hash, err := upload(r, filesize*1000, endpoints[0]) + hash, err := upload(randomBytes, httpEndpoint(hosts[0])) if err != nil { log.Error(err.Error()) return err } - metrics.GetOrRegisterResettingTimer("upload-and-sync.upload-time", nil).UpdateSince(t1) + t2 := time.Since(t1) + metrics.GetOrRegisterResettingTimer("upload-and-sync.upload-time", nil).Update(t2) - fhash := h.Sum(nil) + fhash, err := digest(bytes.NewReader(randomBytes)) + if err != nil { + log.Error(err.Error()) + return err + } - log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash)) + log.Info("uploaded successfully", "tuid", tuid, "hash", hash, "took", t2, "digest", fmt.Sprintf("%x", fhash)) time.Sleep(time.Duration(syncDelay) * time.Second) wg := sync.WaitGroup{} if single { - rand.Seed(time.Now().UTC().UnixNano()) - randIndex := 1 + rand.Intn(len(endpoints)-1) + randIndex := 1 + rand.Intn(len(hosts)-1) ruid := uuid.New()[:8] wg.Add(1) go func(endpoint string, ruid string) { for { start := time.Now() - err := fetch(hash, endpoint, fhash, ruid) + err := fetch(hash, endpoint, fhash, ruid, tuid) if err != nil { continue } + ended := time.Since(start) - metrics.GetOrRegisterResettingTimer("upload-and-sync.single.fetch-time", nil).UpdateSince(start) + metrics.GetOrRegisterResettingTimer("upload-and-sync.single.fetch-time", nil).Update(ended) + log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint) wg.Done() return } - }(endpoints[randIndex], ruid) + }(httpEndpoint(hosts[randIndex]), ruid) } else { - for _, endpoint := range endpoints[1:] { + for _, endpoint := range hosts[1:] { ruid := uuid.New()[:8] wg.Add(1) go func(endpoint string, ruid string) { for { start := time.Now() - err := fetch(hash, endpoint, fhash, ruid) + err := fetch(hash, endpoint, fhash, ruid, tuid) if err != nil { continue } + ended := time.Since(start) - metrics.GetOrRegisterResettingTimer("upload-and-sync.each.fetch-time", nil).UpdateSince(start) + metrics.GetOrRegisterResettingTimer("upload-and-sync.each.fetch-time", nil).Update(ended) + log.Info("fetch successful", "tuid", tuid, "ruid", ruid, "took", ended, "endpoint", endpoint) wg.Done() return } - }(endpoint, ruid) + }(httpEndpoint(endpoint), ruid) } } wg.Wait() - log.Info("all endpoints synced random file successfully") + log.Info("all hosts synced random file successfully") return nil } diff --git a/cmd/swarm/swarm-smoke/upload_speed.go b/cmd/swarm/swarm-smoke/upload_speed.go index 1123f24496bb..20bf7b86ca25 100644 --- a/cmd/swarm/swarm-smoke/upload_speed.go +++ b/cmd/swarm/swarm-smoke/upload_speed.go @@ -17,35 +17,56 @@ package main import ( - "crypto/md5" - crand "crypto/rand" + "bytes" "fmt" - "io" "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/swarm/testutil" cli "gopkg.in/urfave/cli.v1" ) -func uploadSpeed(c *cli.Context) error { - endpoint := generateEndpoint(scheme, cluster, appName, from) - seed := int(time.Now().UnixNano() / 1e6) - log.Info("uploading to "+endpoint, "seed", seed) +func uploadSpeedCmd(ctx *cli.Context, tuid string) error { + log.Info("uploading to "+hosts[0], "tuid", tuid, "seed", seed) + randomBytes := testutil.RandomBytes(seed, filesize*1000) - h := md5.New() - r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h) + errc := make(chan error) + go func() { + errc <- uploadSpeed(ctx, tuid, randomBytes) + }() + + select { + case err := <-errc: + if err != nil { + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1) + } + return err + case <-time.After(time.Duration(timeout) * time.Second): + metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1) + + // trigger debug functionality on randomBytes + + return fmt.Errorf("timeout after %v sec", timeout) + } +} + +func uploadSpeed(c *cli.Context, tuid string, data []byte) error { t1 := time.Now() - hash, err := upload(r, filesize*1000, endpoint) + hash, err := upload(data, hosts[0]) if err != nil { log.Error(err.Error()) return err } metrics.GetOrRegisterCounter("upload-speed.upload-time", nil).Inc(int64(time.Since(t1))) - fhash := h.Sum(nil) + fhash, err := digest(bytes.NewReader(data)) + if err != nil { + log.Error(err.Error()) + return err + } log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash)) return nil diff --git a/cmd/swarm/swarm-smoke/util.go b/cmd/swarm/swarm-smoke/util.go index 003973d41f3c..87abb44b0b20 100644 --- a/cmd/swarm/swarm-smoke/util.go +++ b/cmd/swarm/swarm-smoke/util.go @@ -25,9 +25,11 @@ import ( "fmt" "io" "io/ioutil" + "math/rand" "net/http" "net/http/httptrace" "os" + "strings" "time" "github.com/ethereum/go-ethereum/log" @@ -36,83 +38,49 @@ import ( "github.com/ethereum/go-ethereum/swarm/api/client" "github.com/ethereum/go-ethereum/swarm/spancontext" opentracing "github.com/opentracing/opentracing-go" + "github.com/pborman/uuid" cli "gopkg.in/urfave/cli.v1" ) var ( commandName = "" + seed = int(time.Now().UTC().UnixNano()) ) -func wrapCliCommand(name string, killOnTimeout bool, command func(*cli.Context) error) func(*cli.Context) error { +func init() { + rand.Seed(int64(seed)) +} + +func httpEndpoint(host string) string { + return fmt.Sprintf("http://%s:%d", host, httpPort) +} + +func wsEndpoint(host string) string { + return fmt.Sprintf("ws://%s:%d", host, wsPort) +} + +func wrapCliCommand(name string, command func(*cli.Context, string) error) func(*cli.Context) error { return func(ctx *cli.Context) error { log.PrintOrigins(true) log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(verbosity), log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) + // test uuid + tuid := uuid.New()[:8] + + commandName = name + + hosts = strings.Split(allhosts, ",") + defer func(now time.Time) { totalTime := time.Since(now) - log.Info("total time", "time", totalTime, "kb", filesize) + log.Info("total time", "tuid", tuid, "time", totalTime, "kb", filesize) metrics.GetOrRegisterResettingTimer(name+".total-time", nil).Update(totalTime) }(time.Now()) - log.Info("smoke test starting", "task", name, "timeout", timeout) - commandName = name + log.Info("smoke test starting", "tuid", tuid, "task", name, "timeout", timeout) metrics.GetOrRegisterCounter(name, nil).Inc(1) - errc := make(chan error) - done := make(chan struct{}) - - if killOnTimeout { - go func() { - <-time.After(time.Duration(timeout) * time.Second) - close(done) - }() - } - - go func() { - errc <- command(ctx) - }() - - select { - case err := <-errc: - if err != nil { - metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", name), nil).Inc(1) - } - return err - case <-done: - metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", name), nil).Inc(1) - return fmt.Errorf("timeout after %v sec", timeout) - } - } -} - -func generateEndpoints(scheme string, cluster string, app string, from int, to int) { - if cluster == "prod" { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, port)) - } - } else if cluster == "private-internal" { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://swarm-private-internal-%v:8500", scheme, port)) - } - } else { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, port, cluster)) - } - } - - if includeLocalhost { - endpoints = append(endpoints, "http://localhost:8500") - } -} - -//just use the first endpoint -func generateEndpoint(scheme string, cluster string, app string, from int) string { - if cluster == "prod" { - return fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, from) - } else if cluster == "private-internal" { - return fmt.Sprintf("%s://swarm-private-internal-%v:8500", scheme, from) - } else { - return fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, from, cluster) + return command(ctx, tuid) } } @@ -174,11 +142,11 @@ func fetchFeed(topic string, user string, endpoint string, original []byte, ruid } // fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file -func fetch(hash string, endpoint string, original []byte, ruid string) error { +func fetch(hash string, endpoint string, original []byte, ruid string, tuid string) error { ctx, sp := spancontext.StartSpan(context.Background(), "upload-and-sync.fetch") defer sp.Finish() - log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash) + log.Info("http get request", "tuid", tuid, "ruid", ruid, "endpoint", endpoint, "hash", hash) var tn time.Time reqUri := endpoint + "/bzz:/" + hash + "/" @@ -202,7 +170,7 @@ func fetch(hash string, endpoint string, original []byte, ruid string) error { log.Error(err.Error(), "ruid", ruid) return err } - log.Trace("http get response", "ruid", ruid, "api", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength) + log.Info("http get response", "tuid", tuid, "ruid", ruid, "endpoint", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength) if res.StatusCode != 200 { err := fmt.Errorf("expected status code %d, got %v", 200, res.StatusCode) @@ -230,14 +198,14 @@ func fetch(hash string, endpoint string, original []byte, ruid string) error { } // upload an arbitrary byte as a plaintext file to `endpoint` using the api client -func upload(r io.Reader, size int, endpoint string) (string, error) { +func upload(data []byte, endpoint string) (string, error) { swarm := client.NewClient(endpoint) f := &client.File{ - ReadCloser: ioutil.NopCloser(r), + ReadCloser: ioutil.NopCloser(bytes.NewReader(data)), ManifestEntry: api.ManifestEntry{ ContentType: "text/plain", Mode: 0660, - Size: int64(size), + Size: int64(len(data)), }, }