github.com/letterj/go-ethereum@v1.8.22-0.20190204142846-520024dfd689/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  	"crypto/md5"
    21  	crand "crypto/rand"
    22  	"fmt"
    23  	"io"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/log"
    27  	"github.com/ethereum/go-ethereum/metrics"
    28  
    29  	cli "gopkg.in/urfave/cli.v1"
    30  )
    31  
    32  func uploadSpeed(c *cli.Context) error {
    33  	endpoint := generateEndpoint(scheme, cluster, appName, from)
    34  	seed := int(time.Now().UnixNano() / 1e6)
    35  	log.Info("uploading to "+endpoint, "seed", seed)
    36  
    37  	h := md5.New()
    38  	r := io.TeeReader(io.LimitReader(crand.Reader, int64(filesize*1000)), h)
    39  
    40  	t1 := time.Now()
    41  	hash, err := upload(r, filesize*1000, endpoint)
    42  	if err != nil {
    43  		log.Error(err.Error())
    44  		return err
    45  	}
    46  	metrics.GetOrRegisterCounter("upload-speed.upload-time", nil).Inc(int64(time.Since(t1)))
    47  
    48  	fhash := h.Sum(nil)
    49  
    50  	log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash))
    51  	return nil
    52  }