github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/swarm-smoke/main.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450071575924736>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  	"os"
    18  	"sort"
    19  
    20  	"github.com/ethereum/go-ethereum/cmd/utils"
    21  	gethmetrics "github.com/ethereum/go-ethereum/metrics"
    22  	"github.com/ethereum/go-ethereum/metrics/influxdb"
    23  	swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
    24  	"github.com/ethereum/go-ethereum/swarm/tracing"
    25  
    26  	"github.com/ethereum/go-ethereum/log"
    27  
    28  	cli "gopkg.in/urfave/cli.v1"
    29  )
    30  
    31  var (
    32  gitCommit string //git sha1提交发布的哈希(通过链接器标志设置)
    33  )
    34  
    35  var (
    36  	endpoints        []string
    37  	includeLocalhost bool
    38  	cluster          string
    39  	appName          string
    40  	scheme           string
    41  	filesize         int
    42  	syncDelay        int
    43  	from             int
    44  	to               int
    45  	verbosity        int
    46  	timeout          int
    47  	single           bool
    48  )
    49  
    50  func main() {
    51  
    52  	app := cli.NewApp()
    53  	app.Name = "smoke-test"
    54  	app.Usage = ""
    55  
    56  	app.Flags = []cli.Flag{
    57  		cli.StringFlag{
    58  			Name:        "cluster-endpoint",
    59  			Value:       "prod",
    60  			Usage:       "cluster to point to (prod or a given namespace)",
    61  			Destination: &cluster,
    62  		},
    63  		cli.StringFlag{
    64  			Name:        "app",
    65  			Value:       "swarm",
    66  			Usage:       "application to point to (swarm or swarm-private)",
    67  			Destination: &appName,
    68  		},
    69  		cli.IntFlag{
    70  			Name:        "cluster-from",
    71  			Value:       8501,
    72  			Usage:       "swarm node (from)",
    73  			Destination: &from,
    74  		},
    75  		cli.IntFlag{
    76  			Name:        "cluster-to",
    77  			Value:       8512,
    78  			Usage:       "swarm node (to)",
    79  			Destination: &to,
    80  		},
    81  		cli.StringFlag{
    82  			Name:        "cluster-scheme",
    83  			Value:       "http",
    84  			Usage:       "http or https",
    85  			Destination: &scheme,
    86  		},
    87  		cli.BoolFlag{
    88  			Name:        "include-localhost",
    89  			Usage:       "whether to include localhost:8500 as an endpoint",
    90  			Destination: &includeLocalhost,
    91  		},
    92  		cli.IntFlag{
    93  			Name:        "filesize",
    94  			Value:       1024,
    95  			Usage:       "file size for generated random file in KB",
    96  			Destination: &filesize,
    97  		},
    98  		cli.IntFlag{
    99  			Name:        "sync-delay",
   100  			Value:       5,
   101  			Usage:       "duration of delay in seconds to wait for content to be synced",
   102  			Destination: &syncDelay,
   103  		},
   104  		cli.IntFlag{
   105  			Name:        "verbosity",
   106  			Value:       1,
   107  			Usage:       "verbosity",
   108  			Destination: &verbosity,
   109  		},
   110  		cli.IntFlag{
   111  			Name:        "timeout",
   112  			Value:       120,
   113  			Usage:       "timeout in seconds after which kill the process",
   114  			Destination: &timeout,
   115  		},
   116  		cli.BoolFlag{
   117  			Name:        "single",
   118  			Usage:       "whether to fetch content from a single node or from all nodes",
   119  			Destination: &single,
   120  		},
   121  	}
   122  
   123  	app.Flags = append(app.Flags, []cli.Flag{
   124  		utils.MetricsEnabledFlag,
   125  		swarmmetrics.MetricsInfluxDBEndpointFlag,
   126  		swarmmetrics.MetricsInfluxDBDatabaseFlag,
   127  		swarmmetrics.MetricsInfluxDBUsernameFlag,
   128  		swarmmetrics.MetricsInfluxDBPasswordFlag,
   129  		swarmmetrics.MetricsInfluxDBHostTagFlag,
   130  	}...)
   131  
   132  	app.Flags = append(app.Flags, tracing.Flags...)
   133  
   134  	app.Commands = []cli.Command{
   135  		{
   136  			Name:    "upload_and_sync",
   137  			Aliases: []string{"c"},
   138  			Usage:   "upload and sync",
   139  			Action:  cliUploadAndSync,
   140  		},
   141  		{
   142  			Name:    "feed_sync",
   143  			Aliases: []string{"f"},
   144  			Usage:   "feed update generate, upload and sync",
   145  			Action:  cliFeedUploadAndSync,
   146  		},
   147  		{
   148  			Name:    "upload_speed",
   149  			Aliases: []string{"u"},
   150  			Usage:   "measure upload speed",
   151  			Action:  cliUploadSpeed,
   152  		},
   153  	}
   154  
   155  	sort.Sort(cli.FlagsByName(app.Flags))
   156  	sort.Sort(cli.CommandsByName(app.Commands))
   157  
   158  	app.Before = func(ctx *cli.Context) error {
   159  		tracing.Setup(ctx)
   160  		return nil
   161  	}
   162  
   163  	app.After = func(ctx *cli.Context) error {
   164  		return emitMetrics(ctx)
   165  	}
   166  
   167  	err := app.Run(os.Args)
   168  	if err != nil {
   169  		log.Error(err.Error())
   170  
   171  		os.Exit(1)
   172  	}
   173  }
   174  
   175  func emitMetrics(ctx *cli.Context) error {
   176  	if gethmetrics.Enabled {
   177  		var (
   178  			endpoint = ctx.GlobalString(swarmmetrics.MetricsInfluxDBEndpointFlag.Name)
   179  			database = ctx.GlobalString(swarmmetrics.MetricsInfluxDBDatabaseFlag.Name)
   180  			username = ctx.GlobalString(swarmmetrics.MetricsInfluxDBUsernameFlag.Name)
   181  			password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
   182  			hosttag  = ctx.GlobalString(swarmmetrics.MetricsInfluxDBHostTagFlag.Name)
   183  		)
   184  		return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
   185  			"host":     hosttag,
   186  			"version":  gitCommit,
   187  			"filesize": fmt.Sprintf("%v", filesize),
   188  		})
   189  	}
   190  
   191  	return nil
   192  }
   193