|
| 1 | +//go:build evm |
| 2 | + |
| 3 | +package benchmark |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// benchConfig holds all tunable parameters for a benchmark run. |
| 13 | +// fields are populated from BENCH_* env vars with sensible defaults. |
| 14 | +type benchConfig struct { |
| 15 | + ServiceName string |
| 16 | + |
| 17 | + // infrastructure (used by setupLocalEnv) |
| 18 | + BlockTime string |
| 19 | + SlotDuration string |
| 20 | + GasLimit string |
| 21 | + ScrapeInterval string |
| 22 | + |
| 23 | + // load generation (used by test functions) |
| 24 | + NumSpammers int |
| 25 | + CountPerSpammer int |
| 26 | + Throughput int |
| 27 | + WarmupTxs int |
| 28 | + GasUnitsToBurn int |
| 29 | + MaxWallets int |
| 30 | + WaitTimeout time.Duration |
| 31 | +} |
| 32 | + |
| 33 | +func newBenchConfig(serviceName string) benchConfig { |
| 34 | + return benchConfig{ |
| 35 | + ServiceName: serviceName, |
| 36 | + BlockTime: envOrDefault("BENCH_BLOCK_TIME", "100ms"), |
| 37 | + SlotDuration: envOrDefault("BENCH_SLOT_DURATION", "250ms"), |
| 38 | + GasLimit: envOrDefault("BENCH_GAS_LIMIT", ""), |
| 39 | + ScrapeInterval: envOrDefault("BENCH_SCRAPE_INTERVAL", "1s"), |
| 40 | + NumSpammers: envInt("BENCH_NUM_SPAMMERS", 2), |
| 41 | + CountPerSpammer: envInt("BENCH_COUNT_PER_SPAMMER", 2000), |
| 42 | + Throughput: envInt("BENCH_THROUGHPUT", 200), |
| 43 | + WarmupTxs: envInt("BENCH_WARMUP_TXS", 200), |
| 44 | + GasUnitsToBurn: envInt("BENCH_GAS_UNITS_TO_BURN", 1_000_000), |
| 45 | + MaxWallets: envInt("BENCH_MAX_WALLETS", 500), |
| 46 | + WaitTimeout: envDuration("BENCH_WAIT_TIMEOUT", 10*time.Minute), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (c benchConfig) totalCount() int { |
| 51 | + return c.NumSpammers * c.CountPerSpammer |
| 52 | +} |
| 53 | + |
| 54 | +func (c benchConfig) log(t testing.TB) { |
| 55 | + t.Logf("load: spammers=%d, count_per=%d, throughput=%d, warmup=%d, gas_units=%d, max_wallets=%d", |
| 56 | + c.NumSpammers, c.CountPerSpammer, c.Throughput, c.WarmupTxs, c.GasUnitsToBurn, c.MaxWallets) |
| 57 | + t.Logf("infra: block_time=%s, slot_duration=%s, gas_limit=%s, scrape_interval=%s", |
| 58 | + c.BlockTime, c.SlotDuration, c.GasLimit, c.ScrapeInterval) |
| 59 | +} |
| 60 | + |
| 61 | +func envOrDefault(key, fallback string) string { |
| 62 | + if v := os.Getenv(key); v != "" { |
| 63 | + return v |
| 64 | + } |
| 65 | + return fallback |
| 66 | +} |
| 67 | + |
| 68 | +// envInt returns the integer value of the given env var, or fallback if unset |
| 69 | +// or unparseable. Invalid values silently fall back to the default. |
| 70 | +func envInt(key string, fallback int) int { |
| 71 | + v := os.Getenv(key) |
| 72 | + if v == "" { |
| 73 | + return fallback |
| 74 | + } |
| 75 | + n, err := strconv.Atoi(v) |
| 76 | + if err != nil { |
| 77 | + return fallback |
| 78 | + } |
| 79 | + return n |
| 80 | +} |
| 81 | + |
| 82 | +// envDuration returns the duration value of the given env var (e.g. "5m", "30s"), |
| 83 | +// or fallback if unset or unparseable. |
| 84 | +func envDuration(key string, fallback time.Duration) time.Duration { |
| 85 | + v := os.Getenv(key) |
| 86 | + if v == "" { |
| 87 | + return fallback |
| 88 | + } |
| 89 | + d, err := time.ParseDuration(v) |
| 90 | + if err != nil { |
| 91 | + return fallback |
| 92 | + } |
| 93 | + return d |
| 94 | +} |
0 commit comments