Skip to content

Commit 32fe76a

Browse files
holisticodedshulyak
authored andcommitted
Upload speed (ethereum#18442)
(cherry picked from commit 257bfff)
1 parent 55b0ab7 commit 32fe76a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cmd/swarm/swarm-smoke/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ func main() {
148148
Usage: "feed update generate, upload and sync",
149149
Action: cliFeedUploadAndSync,
150150
},
151+
{
152+
Name: "upload_speed",
153+
Aliases: []string{"u"},
154+
Usage: "measure upload speed",
155+
Action: cliUploadSpeed,
156+
},
151157
}
152158

153159
sort.Sort(cli.FlagsByName(app.Flags))
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)