github.com/rayrapetyan/go-ethereum@v1.8.21/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  	endpoints        []string
    41  	includeLocalhost bool
    42  	cluster          string
    43  	appName          string
    44  	scheme           string
    45  	filesize         int
    46  	syncDelay        int
    47  	from             int
    48  	to               int
    49  	verbosity        int
    50  	timeout          int
    51  	single           bool
    52  )
    53  
    54  func main() {
    55  
    56  	app := cli.NewApp()
    57  	app.Name = "smoke-test"
    58  	app.Usage = ""
    59  
    60  	app.Flags = []cli.Flag{
    61  		cli.StringFlag{
    62  			Name:        "cluster-endpoint",
    63  			Value:       "prod",
    64  			Usage:       "cluster to point to (prod or a given namespace)",
    65  			Destination: &cluster,
    66  		},
    67  		cli.StringFlag{
    68  			Name:        "app",
    69  			Value:       "swarm",
    70  			Usage:       "application to point to (swarm or swarm-private)",
    71  			Destination: &appName,
    72  		},
    73  		cli.IntFlag{
    74  			Name:        "cluster-from",
    75  			Value:       8501,
    76  			Usage:       "swarm node (from)",
    77  			Destination: &from,
    78  		},
    79  		cli.IntFlag{
    80  			Name:        "cluster-to",
    81  			Value:       8512,
    82  			Usage:       "swarm node (to)",
    83  			Destination: &to,
    84  		},
    85  		cli.StringFlag{
    86  			Name:        "cluster-scheme",
    87  			Value:       "http",
    88  			Usage:       "http or https",
    89  			Destination: &scheme,
    90  		},
    91  		cli.BoolFlag{
    92  			Name:        "include-localhost",
    93  			Usage:       "whether to include localhost:8500 as an endpoint",
    94  			Destination: &includeLocalhost,
    95  		},
    96  		cli.IntFlag{
    97  			Name:        "filesize",
    98  			Value:       1024,
    99  			Usage:       "file size for generated random file in KB",
   100  			Destination: &filesize,
   101  		},
   102  		cli.IntFlag{
   103  			Name:        "sync-delay",
   104  			Value:       5,
   105  			Usage:       "duration of delay in seconds to wait for content to be synced",
   106  			Destination: &syncDelay,
   107  		},
   108  		cli.IntFlag{
   109  			Name:        "verbosity",
   110  			Value:       1,
   111  			Usage:       "verbosity",
   112  			Destination: &verbosity,
   113  		},
   114  		cli.IntFlag{
   115  			Name:        "timeout",
   116  			Value:       120,
   117  			Usage:       "timeout in seconds after which kill the process",
   118  			Destination: &timeout,
   119  		},
   120  		cli.BoolFlag{
   121  			Name:        "single",
   122  			Usage:       "whether to fetch content from a single node or from all nodes",
   123  			Destination: &single,
   124  		},
   125  	}
   126  
   127  	app.Flags = append(app.Flags, []cli.Flag{
   128  		utils.MetricsEnabledFlag,
   129  		swarmmetrics.MetricsInfluxDBEndpointFlag,
   130  		swarmmetrics.MetricsInfluxDBDatabaseFlag,
   131  		swarmmetrics.MetricsInfluxDBUsernameFlag,
   132  		swarmmetrics.MetricsInfluxDBPasswordFlag,
   133  		swarmmetrics.MetricsInfluxDBHostTagFlag,
   134  	}...)
   135  
   136  	app.Flags = append(app.Flags, tracing.Flags...)
   137  
   138  	app.Commands = []cli.Command{
   139  		{
   140  			Name:    "upload_and_sync",
   141  			Aliases: []string{"c"},
   142  			Usage:   "upload and sync",
   143  			Action:  cliUploadAndSync,
   144  		},
   145  		{
   146  			Name:    "feed_sync",
   147  			Aliases: []string{"f"},
   148  			Usage:   "feed update generate, upload and sync",
   149  			Action:  cliFeedUploadAndSync,
   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  			hosttag  = ctx.GlobalString(swarmmetrics.MetricsInfluxDBHostTagFlag.Name)
   181  		)
   182  		return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
   183  			"host":     hosttag,
   184  			"version":  gitCommit,
   185  			"filesize": fmt.Sprintf("%v", filesize),
   186  		})
   187  	}
   188  
   189  	return nil
   190  }