Skip to content

Commit 845fbb4

Browse files
authored
improve metrics (#1093)
* improve metrics * printout metric descriptions * use json output
1 parent 254ae2b commit 845fbb4

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

cmd/simulator/config/flags.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
TimeoutKey = "timeout"
2929
BatchSizeKey = "batch-size"
3030
MetricsPortKey = "metrics-port"
31+
MetricsOutputKey = "metrics-output"
3132
)
3233

3334
var (
@@ -37,28 +38,30 @@ var (
3738
)
3839

3940
type Config struct {
40-
Endpoints []string `json:"endpoints"`
41-
MaxFeeCap int64 `json:"max-fee-cap"`
42-
MaxTipCap int64 `json:"max-tip-cap"`
43-
Workers int `json:"workers"`
44-
TxsPerWorker uint64 `json:"txs-per-worker"`
45-
KeyDir string `json:"key-dir"`
46-
Timeout time.Duration `json:"timeout"`
47-
BatchSize uint64 `json:"batch-size"`
48-
MetricsPort uint64 `json:"metrics-port"`
41+
Endpoints []string `json:"endpoints"`
42+
MaxFeeCap int64 `json:"max-fee-cap"`
43+
MaxTipCap int64 `json:"max-tip-cap"`
44+
Workers int `json:"workers"`
45+
TxsPerWorker uint64 `json:"txs-per-worker"`
46+
KeyDir string `json:"key-dir"`
47+
Timeout time.Duration `json:"timeout"`
48+
BatchSize uint64 `json:"batch-size"`
49+
MetricsPort uint64 `json:"metrics-port"`
50+
MetricsOutput string `json:"metrics-output"`
4951
}
5052

5153
func BuildConfig(v *viper.Viper) (Config, error) {
5254
c := Config{
53-
Endpoints: v.GetStringSlice(EndpointsKey),
54-
MaxFeeCap: v.GetInt64(MaxFeeCapKey),
55-
MaxTipCap: v.GetInt64(MaxTipCapKey),
56-
Workers: v.GetInt(WorkersKey),
57-
TxsPerWorker: v.GetUint64(TxsPerWorkerKey),
58-
KeyDir: v.GetString(KeyDirKey),
59-
Timeout: v.GetDuration(TimeoutKey),
60-
BatchSize: v.GetUint64(BatchSizeKey),
61-
MetricsPort: v.GetUint64(MetricsPortKey),
55+
Endpoints: v.GetStringSlice(EndpointsKey),
56+
MaxFeeCap: v.GetInt64(MaxFeeCapKey),
57+
MaxTipCap: v.GetInt64(MaxTipCapKey),
58+
Workers: v.GetInt(WorkersKey),
59+
TxsPerWorker: v.GetUint64(TxsPerWorkerKey),
60+
KeyDir: v.GetString(KeyDirKey),
61+
Timeout: v.GetDuration(TimeoutKey),
62+
BatchSize: v.GetUint64(BatchSizeKey),
63+
MetricsPort: v.GetUint64(MetricsPortKey),
64+
MetricsOutput: v.GetString(MetricsOutputKey),
6265
}
6366
if len(c.Endpoints) == 0 {
6467
return c, ErrNoEndpoints
@@ -122,4 +125,5 @@ func addSimulatorFlags(fs *pflag.FlagSet) {
122125
fs.String(LogLevelKey, "info", "Specify the log level to use in the simulator")
123126
fs.Uint64(BatchSizeKey, 100, "Specify the batchsize for the worker to issue and confirm txs")
124127
fs.Uint64(MetricsPortKey, 8082, "Specify the port to use for the metrics server")
128+
fs.String(MetricsOutputKey, "", "Specify the file to write metrics in json format, or empy to write to stdout (defaults to stdout)")
125129
}

cmd/simulator/load/loader.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ func ExecuteLoader(ctx context.Context, config config.Config) error {
150150
}()
151151

152152
m := metrics.NewDefaultMetrics()
153-
ms := m.Serve(ctx, strconv.Itoa(int(config.MetricsPort)), MetricsEndpoint)
153+
metricsCtx := context.Background()
154+
ms := m.Serve(metricsCtx, strconv.Itoa(int(config.MetricsPort)), MetricsEndpoint)
154155
defer ms.Shutdown()
155156

156157
// Construct the arguments for the load simulator
@@ -186,13 +187,13 @@ func ExecuteLoader(ctx context.Context, config config.Config) error {
186187
// to fund gas for all of their transactions.
187188
maxFeeCap := new(big.Int).Mul(big.NewInt(params.GWei), big.NewInt(config.MaxFeeCap))
188189
minFundsPerAddr := new(big.Int).Mul(maxFeeCap, big.NewInt(int64(config.TxsPerWorker*params.TxGas)))
189-
190+
fundStart := time.Now()
190191
log.Info("Distributing funds", "numTxsPerWorker", config.TxsPerWorker, "minFunds", minFundsPerAddr)
191192
keys, err = DistributeFunds(ctx, clients[0], keys, config.Workers, minFundsPerAddr, m)
192193
if err != nil {
193194
return err
194195
}
195-
log.Info("Distributed funds successfully")
196+
log.Info("Distributed funds successfully", "time", time.Since(fundStart))
196197

197198
pks := make([]*ecdsa.PrivateKey, 0, len(keys))
198199
senders := make([]common.Address, 0, len(keys))
@@ -225,18 +226,22 @@ func ExecuteLoader(ctx context.Context, config config.Config) error {
225226
Value: common.Big0,
226227
})
227228
}
228-
229+
txSequenceStart := time.Now()
229230
txSequences, err := txs.GenerateTxSequences(ctx, txGenerator, clients[0], pks, config.TxsPerWorker, false)
230231
if err != nil {
231232
return err
232233
}
234+
log.Info("Created transaction sequences successfully", "time", time.Since(txSequenceStart))
233235

234236
workers := make([]txs.Worker[*types.Transaction], 0, len(clients))
235237
for i, client := range clients {
236238
workers = append(workers, NewSingleAddressTxWorker(ctx, client, ethcrypto.PubkeyToAddress(pks[i].PublicKey)))
237239
}
238240
loader := New(workers, txSequences, config.BatchSize, m)
239241
err = loader.Execute(ctx)
240-
ms.Print() // Print regardless of execution error
242+
prerr := m.Print(config.MetricsOutput) // Print regardless of execution error
243+
if prerr != nil {
244+
log.Warn("Failed to print metrics", "error", prerr)
245+
}
241246
return err
242247
}

cmd/simulator/metrics/metrics.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ package metrics
55

66
import (
77
"context"
8+
"encoding/json"
89
"errors"
910
"fmt"
10-
"io/ioutil"
1111
"net/http"
12-
"strings"
12+
"os"
1313

1414
"github.com/ethereum/go-ethereum/log"
1515
"github.com/prometheus/client_golang/prometheus"
@@ -108,22 +108,32 @@ func (ms *MetricsServer) Shutdown() {
108108
<-ms.stopCh
109109
}
110110

111-
func (ms *MetricsServer) Print() {
112-
// Get response from server
113-
resp, err := http.Get(fmt.Sprintf("http://localhost:%s%s", ms.metricsPort, ms.metricsEndpoint))
111+
func (m *Metrics) Print(outputFile string) error {
112+
metrics, err := m.reg.Gather()
114113
if err != nil {
115-
log.Error("cannot get response from metrics servers", "err", err)
116-
return
114+
return err
117115
}
118-
// Read response body
119-
respBody, err := ioutil.ReadAll(resp.Body)
120-
if err != nil {
121-
log.Error("cannot read response body", "err", err)
122-
return
123-
}
124-
// Print out formatted individual metrics
125-
parts := strings.Split(string(respBody), "\n")
126-
for _, s := range parts {
127-
fmt.Printf(" \t\t\t%s\n", s)
116+
117+
if outputFile == "" {
118+
// Printout to stdout
119+
fmt.Println("*** Metrics ***")
120+
for _, mf := range metrics {
121+
for _, m := range mf.GetMetric() {
122+
fmt.Printf("Type: %s, Name: %s, Description: %s, Values: %s\n", mf.GetType().String(), mf.GetName(), mf.GetHelp(), m.String())
123+
}
124+
}
125+
fmt.Println("***************")
126+
} else {
127+
jsonFile, err := os.Create(outputFile)
128+
if err != nil {
129+
return err
130+
}
131+
defer jsonFile.Close()
132+
133+
if err := json.NewEncoder(jsonFile).Encode(metrics); err != nil {
134+
return err
135+
}
128136
}
137+
138+
return nil
129139
}

0 commit comments

Comments
 (0)