|
| 1 | +// Copyright 2018 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// go-ethereum is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/ethereum/go-ethereum/log" |
| 26 | + "github.com/ethereum/go-ethereum/metrics" |
| 27 | + "github.com/ethereum/go-ethereum/swarm/testutil" |
| 28 | + |
| 29 | + cli "gopkg.in/urfave/cli.v1" |
| 30 | +) |
| 31 | + |
| 32 | +var endpoint string |
| 33 | + |
| 34 | +//just use the first endpoint |
| 35 | +func generateEndpoint(scheme string, cluster string, app string, from int) { |
| 36 | + if cluster == "prod" { |
| 37 | + endpoint = fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, from) |
| 38 | + } else { |
| 39 | + endpoint = fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, from, cluster) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func cliUploadSpeed(c *cli.Context) error { |
| 44 | + log.PrintOrigins(true) |
| 45 | + log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(verbosity), log.StreamHandler(os.Stdout, log.TerminalFormat(true)))) |
| 46 | + |
| 47 | + metrics.GetOrRegisterCounter("upload-speed", nil).Inc(1) |
| 48 | + |
| 49 | + errc := make(chan error) |
| 50 | + go func() { |
| 51 | + errc <- uploadSpeed(c) |
| 52 | + }() |
| 53 | + |
| 54 | + select { |
| 55 | + case err := <-errc: |
| 56 | + if err != nil { |
| 57 | + metrics.GetOrRegisterCounter("upload-speed.fail", nil).Inc(1) |
| 58 | + } |
| 59 | + return err |
| 60 | + case <-time.After(time.Duration(timeout) * time.Second): |
| 61 | + metrics.GetOrRegisterCounter("upload-speed.timeout", nil).Inc(1) |
| 62 | + return fmt.Errorf("timeout after %v sec", timeout) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func uploadSpeed(c *cli.Context) error { |
| 67 | + defer func(now time.Time) { |
| 68 | + totalTime := time.Since(now) |
| 69 | + |
| 70 | + log.Info("total time", "time", totalTime, "kb", filesize) |
| 71 | + metrics.GetOrRegisterCounter("upload-speed.total-time", nil).Inc(int64(totalTime)) |
| 72 | + }(time.Now()) |
| 73 | + |
| 74 | + generateEndpoint(scheme, cluster, appName, from) |
| 75 | + seed := int(time.Now().UnixNano() / 1e6) |
| 76 | + log.Info("uploading to "+endpoint, "seed", seed) |
| 77 | + |
| 78 | + randomBytes := testutil.RandomBytes(seed, filesize*1000) |
| 79 | + |
| 80 | + t1 := time.Now() |
| 81 | + hash, err := upload(&randomBytes, endpoint) |
| 82 | + if err != nil { |
| 83 | + log.Error(err.Error()) |
| 84 | + return err |
| 85 | + } |
| 86 | + metrics.GetOrRegisterCounter("upload-speed.upload-time", nil).Inc(int64(time.Since(t1))) |
| 87 | + |
| 88 | + fhash, err := digest(bytes.NewReader(randomBytes)) |
| 89 | + if err != nil { |
| 90 | + log.Error(err.Error()) |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash)) |
| 95 | + return nil |
| 96 | +} |
0 commit comments