github.com/XinFinOrg/xdcchain@v1.1.0/cmd/swarm/swarm-smoke/main.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  	"fmt"
    21  	"os"
    22  	"sort"
    23  
    24  	"github.com/ethereum/go-ethereum/cmd/utils"
    25  	gethmetrics "github.com/ethereum/go-ethereum/metrics"
    26  	"github.com/ethereum/go-ethereum/metrics/influxdb"
    27  	swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
    28  	"github.com/ethereum/go-ethereum/swarm/tracing"
    29  
    30  	"github.com/ethereum/go-ethereum/log"
    31  
    32  	cli "gopkg.in/urfave/cli.v1"
    33  )
    34  
    35  var (
    36  	gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
    37  )
    38  
    39  var (
    40  	allhosts     string
    41  	hosts        []string
    42  	filesize     int
    43  	syncDelay    int
    44  	httpPort     int
    45  	wsPort       int
    46  	verbosity    int
    47  	timeout      int
    48  	single       bool
    49  	trackTimeout int
    50  )
    51  
    52  func main() {
    53  
    54  	app := cli.NewApp()
    55  	app.Name = "smoke-test"
    56  	app.Usage = ""
    57  
    58  	app.Flags = []cli.Flag{
    59  		cli.StringFlag{
    60  			Name:        "hosts",
    61  			Value:       "",
    62  			Usage:       "comma-separated list of swarm hosts",
    63  			Destination: &allhosts,
    64  		},
    65  		cli.IntFlag{
    66  			Name:        "http-port",
    67  			Value:       80,
    68  			Usage:       "http port",
    69  			Destination: &httpPort,
    70  		},
    71  		cli.IntFlag{
    72  			Name:        "ws-port",
    73  			Value:       8546,
    74  			Usage:       "ws port",
    75  			Destination: &wsPort,
    76  		},
    77  		cli.IntFlag{
    78  			Name:        "filesize",
    79  			Value:       1024,
    80  			Usage:       "file size for generated random file in KB",
    81  			Destination: &filesize,
    82  		},
    83  		cli.IntFlag{
    84  			Name:        "sync-delay",
    85  			Value:       5,
    86  			Usage:       "duration of delay in seconds to wait for content to be synced",
    87  			Destination: &syncDelay,
    88  		},
    89  		cli.IntFlag{
    90  			Name:        "verbosity",
    91  			Value:       1,
    92  			Usage:       "verbosity",
    93  			Destination: &verbosity,
    94  		},
    95  		cli.IntFlag{
    96  			Name:        "timeout",
    97  			Value:       120,
    98  			Usage:       "timeout in seconds after which kill the process",
    99  			Destination: &timeout,
   100  		},
   101  		cli.BoolFlag{
   102  			Name:        "single",
   103  			Usage:       "whether to fetch content from a single node or from all nodes",
   104  			Destination: &single,
   105  		},
   106  		cli.IntFlag{
   107  			Name:        "track-timeout",
   108  			Value:       5,
   109  			Usage:       "timeout in seconds to wait for GetAllReferences to return",
   110  			Destination: &trackTimeout,
   111  		},
   112  	}
   113  
   114  	app.Flags = append(app.Flags, []cli.Flag{
   115  		utils.MetricsEnabledFlag,
   116  		swarmmetrics.MetricsInfluxDBEndpointFlag,
   117  		swarmmetrics.MetricsInfluxDBDatabaseFlag,
   118  		swarmmetrics.MetricsInfluxDBUsernameFlag,
   119  		swarmmetrics.MetricsInfluxDBPasswordFlag,
   120  		swarmmetrics.MetricsInfluxDBTagsFlag,
   121  	}...)
   122  
   123  	app.Flags = append(app.Flags, tracing.Flags...)
   124  
   125  	app.Commands = []cli.Command{
   126  		{
   127  			Name:    "upload_and_sync",
   128  			Aliases: []string{"c"},
   129  			Usage:   "upload and sync",
   130  			Action:  wrapCliCommand("upload-and-sync", uploadAndSyncCmd),
   131  		},
   132  		{
   133  			Name:    "feed_sync",
   134  			Aliases: []string{"f"},
   135  			Usage:   "feed update generate, upload and sync",
   136  			Action:  wrapCliCommand("feed-and-sync", feedUploadAndSyncCmd),
   137  		},
   138  		{
   139  			Name:    "upload_speed",
   140  			Aliases: []string{"u"},
   141  			Usage:   "measure upload speed",
   142  			Action:  wrapCliCommand("upload-speed", uploadSpeedCmd),
   143  		},
   144  		{
   145  			Name:    "sliding_window",
   146  			Aliases: []string{"s"},
   147  			Usage:   "measure network aggregate capacity",
   148  			Action:  wrapCliCommand("sliding-window", slidingWindowCmd),
   149  		},
   150  	}
   151  
   152  	sort.Sort(cli.FlagsByName(app.Flags))
   153  	sort.Sort(cli.CommandsByName(app.Commands))
   154  
   155  	app.Before = func(ctx *cli.Context) error {
   156  		tracing.Setup(ctx)
   157  		return nil
   158  	}
   159  
   160  	app.After = func(ctx *cli.Context) error {
   161  		return emitMetrics(ctx)
   162  	}
   163  
   164  	err := app.Run(os.Args)
   165  	if err != nil {
   166  		log.Error(err.Error())
   167  
   168  		os.Exit(1)
   169  	}
   170  }
   171  
   172  func emitMetrics(ctx *cli.Context) error {
   173  	if gethmetrics.Enabled {
   174  		var (
   175  			endpoint = ctx.GlobalString(swarmmetrics.MetricsInfluxDBEndpointFlag.Name)
   176  			database = ctx.GlobalString(swarmmetrics.MetricsInfluxDBDatabaseFlag.Name)
   177  			username = ctx.GlobalString(swarmmetrics.MetricsInfluxDBUsernameFlag.Name)
   178  			password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
   179  			tags     = ctx.GlobalString(swarmmetrics.MetricsInfluxDBTagsFlag.Name)
   180  		)
   181  
   182  		tagsMap := utils.SplitTagsFlag(tags)
   183  		tagsMap["version"] = gitCommit
   184  		tagsMap["filesize"] = fmt.Sprintf("%v", filesize)
   185  
   186  		return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", tagsMap)
   187  	}
   188  
   189  	return nil
   190  }