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