github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/cmd/swarm/swarm-smoke/upload_speed.go (about)

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