upload_speed.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. package main
  17. import (
  18. "bytes"
  19. "fmt"
  20. "time"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/metrics"
  23. "github.com/ethereum/go-ethereum/swarm/testutil"
  24. cli "gopkg.in/urfave/cli.v1"
  25. )
  26. func uploadSpeedCmd(ctx *cli.Context) error {
  27. log.Info("uploading to "+hosts[0], "seed", seed)
  28. randomBytes := testutil.RandomBytes(seed, filesize*1000)
  29. errc := make(chan error)
  30. go func() {
  31. errc <- uploadSpeed(ctx, randomBytes)
  32. }()
  33. select {
  34. case err := <-errc:
  35. if err != nil {
  36. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", commandName), nil).Inc(1)
  37. }
  38. return err
  39. case <-time.After(time.Duration(timeout) * time.Second):
  40. metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", commandName), nil).Inc(1)
  41. // trigger debug functionality on randomBytes
  42. return fmt.Errorf("timeout after %v sec", timeout)
  43. }
  44. }
  45. func uploadSpeed(c *cli.Context, data []byte) error {
  46. t1 := time.Now()
  47. hash, err := upload(data, hosts[0])
  48. if err != nil {
  49. log.Error(err.Error())
  50. return err
  51. }
  52. metrics.GetOrRegisterCounter("upload-speed.upload-time", nil).Inc(int64(time.Since(t1)))
  53. fhash, err := digest(bytes.NewReader(data))
  54. if err != nil {
  55. log.Error(err.Error())
  56. return err
  57. }
  58. log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash))
  59. return nil
  60. }