github.com/codingfuture/orig-energi3@v0.8.4/cmd/swarm/swarm-smoke/main.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of Energi Core.
     4  //
     5  // Energi Core is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // Energi Core is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with Energi Core. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"sort"
    24  
    25  	"github.com/ethereum/go-ethereum/cmd/utils"
    26  	gethmetrics "github.com/ethereum/go-ethereum/metrics"
    27  	"github.com/ethereum/go-ethereum/metrics/influxdb"
    28  	swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
    29  	"github.com/ethereum/go-ethereum/swarm/tracing"
    30  
    31  	"github.com/ethereum/go-ethereum/log"
    32  
    33  	cli "gopkg.in/urfave/cli.v1"
    34  )
    35  
    36  var (
    37  	gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
    38  )
    39  
    40  var (
    41  	allhosts     string
    42  	hosts        []string
    43  	filesize     int
    44  	syncDelay    int
    45  	httpPort     int
    46  	wsPort       int
    47  	verbosity    int
    48  	timeout      int
    49  	single       bool
    50  	trackTimeout int
    51  )
    52  
    53  func main() {
    54  
    55  	app := cli.NewApp()
    56  	app.Name = "smoke-test"
    57  	app.Usage = ""
    58  
    59  	app.Flags = []cli.Flag{
    60  		cli.StringFlag{
    61  			Name:        "hosts",
    62  			Value:       "",
    63  			Usage:       "comma-separated list of swarm hosts",
    64  			Destination: &allhosts,
    65  		},
    66  		cli.IntFlag{
    67  			Name:        "http-port",
    68  			Value:       80,
    69  			Usage:       "http port",
    70  			Destination: &httpPort,
    71  		},
    72  		cli.IntFlag{
    73  			Name:        "ws-port",
    74  			Value:       39795,
    75  			Usage:       "ws port",
    76  			Destination: &wsPort,
    77  		},
    78  		cli.IntFlag{
    79  			Name:        "filesize",
    80  			Value:       1024,
    81  			Usage:       "file size for generated random file in KB",
    82  			Destination: &filesize,
    83  		},
    84  		cli.IntFlag{
    85  			Name:        "sync-delay",
    86  			Value:       5,
    87  			Usage:       "duration of delay in seconds to wait for content to be synced",
    88  			Destination: &syncDelay,
    89  		},
    90  		cli.IntFlag{
    91  			Name:        "verbosity",
    92  			Value:       1,
    93  			Usage:       "verbosity",
    94  			Destination: &verbosity,
    95  		},
    96  		cli.IntFlag{
    97  			Name:        "timeout",
    98  			Value:       120,
    99  			Usage:       "timeout in seconds after which kill the process",
   100  			Destination: &timeout,
   101  		},
   102  		cli.BoolFlag{
   103  			Name:        "single",
   104  			Usage:       "whether to fetch content from a single node or from all nodes",
   105  			Destination: &single,
   106  		},
   107  		cli.IntFlag{
   108  			Name:        "track-timeout",
   109  			Value:       5,
   110  			Usage:       "timeout in seconds to wait for GetAllReferences to return",
   111  			Destination: &trackTimeout,
   112  		},
   113  	}
   114  
   115  	app.Flags = append(app.Flags, []cli.Flag{
   116  		utils.MetricsEnabledFlag,
   117  		swarmmetrics.MetricsInfluxDBEndpointFlag,
   118  		swarmmetrics.MetricsInfluxDBDatabaseFlag,
   119  		swarmmetrics.MetricsInfluxDBUsernameFlag,
   120  		swarmmetrics.MetricsInfluxDBPasswordFlag,
   121  		swarmmetrics.MetricsInfluxDBTagsFlag,
   122  	}...)
   123  
   124  	app.Flags = append(app.Flags, tracing.Flags...)
   125  
   126  	app.Commands = []cli.Command{
   127  		{
   128  			Name:    "upload_and_sync",
   129  			Aliases: []string{"c"},
   130  			Usage:   "upload and sync",
   131  			Action:  wrapCliCommand("upload-and-sync", uploadAndSyncCmd),
   132  		},
   133  		{
   134  			Name:    "feed_sync",
   135  			Aliases: []string{"f"},
   136  			Usage:   "feed update generate, upload and sync",
   137  			Action:  wrapCliCommand("feed-and-sync", feedUploadAndSyncCmd),
   138  		},
   139  		{
   140  			Name:    "upload_speed",
   141  			Aliases: []string{"u"},
   142  			Usage:   "measure upload speed",
   143  			Action:  wrapCliCommand("upload-speed", uploadSpeedCmd),
   144  		},
   145  		{
   146  			Name:    "sliding_window",
   147  			Aliases: []string{"s"},
   148  			Usage:   "measure network aggregate capacity",
   149  			Action:  wrapCliCommand("sliding-window", slidingWindowCmd),
   150  		},
   151  	}
   152  
   153  	sort.Sort(cli.FlagsByName(app.Flags))
   154  	sort.Sort(cli.CommandsByName(app.Commands))
   155  
   156  	app.Before = func(ctx *cli.Context) error {
   157  		tracing.Setup(ctx)
   158  		return nil
   159  	}
   160  
   161  	app.After = func(ctx *cli.Context) error {
   162  		return emitMetrics(ctx)
   163  	}
   164  
   165  	err := app.Run(os.Args)
   166  	if err != nil {
   167  		log.Error(err.Error())
   168  
   169  		os.Exit(1)
   170  	}
   171  }
   172  
   173  func emitMetrics(ctx *cli.Context) error {
   174  	if gethmetrics.Enabled {
   175  		var (
   176  			endpoint = ctx.GlobalString(swarmmetrics.MetricsInfluxDBEndpointFlag.Name)
   177  			database = ctx.GlobalString(swarmmetrics.MetricsInfluxDBDatabaseFlag.Name)
   178  			username = ctx.GlobalString(swarmmetrics.MetricsInfluxDBUsernameFlag.Name)
   179  			password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
   180  			tags     = ctx.GlobalString(swarmmetrics.MetricsInfluxDBTagsFlag.Name)
   181  		)
   182  
   183  		tagsMap := utils.SplitTagsFlag(tags)
   184  		tagsMap["version"] = gitCommit
   185  		tagsMap["filesize"] = fmt.Sprintf("%v", filesize)
   186  
   187  		return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", tagsMap)
   188  	}
   189  
   190  	return nil
   191  }