1717package main
1818
1919import (
20- "crypto/md5"
21- crand "crypto/rand"
20+ "bytes"
2221 "fmt"
23- "io"
2422 "math/rand"
2523 "time"
2624
2725 "github.com/ethereum/go-ethereum/log"
2826 "github.com/ethereum/go-ethereum/metrics"
27+ "github.com/ethereum/go-ethereum/swarm/testutil"
2928 "github.com/pborman/uuid"
3029
3130 cli "gopkg.in/urfave/cli.v1"
3231)
3332
34- var seed = time .Now ().UTC ().UnixNano ()
35-
36- func init () {
37- rand .Seed (seed )
38- }
39-
4033type uploadResult struct {
4134 hash string
4235 digest []byte
4336}
4437
45- func slidingWindow (c * cli.Context ) error {
38+ func slidingWindow (ctx * cli.Context , tuid string ) error {
39+ errc := make (chan error )
40+
41+ go func () {
42+ errc <- sl (ctx , tuid )
43+ }()
44+
45+ select {
46+ case err := <- errc :
47+ if err != nil {
48+ metrics .GetOrRegisterCounter (fmt .Sprintf ("%s.fail" , commandName ), nil ).Inc (1 )
49+ }
50+ return err
51+ case <- time .After (time .Duration (timeout ) * time .Second ):
52+ metrics .GetOrRegisterCounter (fmt .Sprintf ("%s.timeout" , commandName ), nil ).Inc (1 )
53+
54+ return fmt .Errorf ("timeout after %v sec" , timeout )
55+ }
56+ }
57+
58+ func sl (ctx * cli.Context , tuid string ) error {
4659 hashes := []uploadResult {} //swarm hashes of the uploads
4760 nodes := len (hosts )
4861 const iterationTimeout = 30 * time .Second
49- log .Info ("sliding window test started" , "nodes" , nodes , "filesize(kb)" , filesize , "timeout" , timeout )
62+ log .Info ("sliding window test started" , "tuid" , tuid , " nodes" , nodes , "filesize(kb)" , filesize , "timeout" , timeout )
5063 uploadedBytes := 0
5164 networkDepth := 0
5265 errored := false
@@ -55,19 +68,23 @@ outer:
5568 for {
5669 log .Info ("uploading to " + httpEndpoint (hosts [0 ])+ " and syncing" , "seed" , seed )
5770
58- h := md5 .New ()
59- r := io .TeeReader (io .LimitReader (crand .Reader , int64 (filesize * 1000 )), h )
6071 t1 := time .Now ()
6172
62- hash , err := upload (r , filesize * 1000 , httpEndpoint (hosts [0 ]))
73+ randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
74+
75+ hash , err := upload (randomBytes , httpEndpoint (hosts [0 ]))
6376 if err != nil {
6477 log .Error (err .Error ())
6578 return err
6679 }
6780
6881 metrics .GetOrRegisterResettingTimer ("sliding-window.upload-time" , nil ).UpdateSince (t1 )
6982
70- fhash := h .Sum (nil )
83+ fhash , err := digest (bytes .NewReader (randomBytes ))
84+ if err != nil {
85+ log .Error (err .Error ())
86+ return err
87+ }
7188
7289 log .Info ("uploaded successfully" , "hash" , hash , "digest" , fmt .Sprintf ("%x" , fhash ), "sleeping" , syncDelay )
7390 hashes = append (hashes , uploadResult {hash : hash , digest : fhash })
0 commit comments