Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: simplify metrics
  • Loading branch information
RodrigoVillar committed Sep 30, 2025
commit 893adbbd4912923faef32d06c066650b4e4f2e66
154 changes: 29 additions & 125 deletions tests/reexecute/c/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,83 +14,21 @@ import (

var (
gasMetric = topLevelMetric{
Name: "gas",
Queries: []string{"avalanche_evm_eth_chain_block_gas_used_processed"},
}
topLevelMetrics = []topLevelMetric{
{
Name: "content_validation",
Queries: []string{"avalanche_evm_eth_chain_block_validations_content"},
},
{
Name: "state_init",
Queries: []string{
"avalanche_evm_eth_chain_block_inits_state",
},
},
{
Name: "execution",
Queries: []string{
"avalanche_evm_eth_chain_block_executions",
},
},
{
Name: "block_validation",
Queries: []string{
"avalanche_evm_eth_chain_block_validations_state",
},
},
{
Name: "trie_hash",
Queries: []string{
"avalanche_evm_eth_chain_storage_hashes",
"avalanche_evm_eth_chain_account_hashes",
},
},
{
Name: "trie_update",
Queries: []string{
"avalanche_evm_eth_chain_account_updates",
"avalanche_evm_eth_chain_storage_updates",
},
},
{
Name: "trie_read",
Queries: []string{
"avalanche_evm_eth_chain_snapshot_account_reads",
"avalanche_evm_eth_chain_account_reads",
"avalanche_evm_eth_chain_snapshot_storage_reads",
"avalanche_evm_eth_chain_storage_reads",
},
},
{
Name: "block_write",
Queries: []string{
"avalanche_evm_eth_chain_block_writes",
},
},
{
Name: "commit",
Queries: []string{
"avalanche_evm_eth_chain_account_commits",
"avalanche_evm_eth_chain_storage_commits",
"avalanche_evm_eth_chain_snapshot_commits",
"avalanche_evm_eth_chain_triedb_commits",
},
},
name: "gas",
query: "avalanche_evm_eth_chain_block_gas_used_processed",
}
meterVMMetrics = []topLevelMetric{
{
Name: "avg_block_parse",
Queries: []string{"avalanche_meterchainvm_C_parse_block"},
name: "block_parse",
query: "avalanche_meterchainvm_C_parse_block_sum",
},
{
Name: "avg_block_verify",
Queries: []string{"avalanche_meterchainvm_C_verify"},
name: "block_verify",
query: "avalanche_meterchainvm_C_verify_sum",
},
{
Name: "avg_block_accept",
Queries: []string{"avalanche_meterchainvm_C_accept"},
name: "block_accept",
query: "avalanche_meterchainvm_C_accept_sum",
},
}
)
Expand All @@ -110,85 +48,51 @@ func getCounterMetricValue(registry prometheus.Gatherer, query string) (float64,
return 0, fmt.Errorf("metric %s not found", query)
}

func getAveragerValue(registry prometheus.Gatherer, query string) (float64, error) {
func getGaugeMetricValue(registry prometheus.Gatherer, query string) (float64, error) {
metricFamilies, err := registry.Gather()
if err != nil {
return 0, fmt.Errorf("failed to gather metrics: %w", err)
}

var (
sumName = query + "_sum"
countName = query + "_count"

sum float64
count float64
)

for _, mf := range metricFamilies {
name := mf.GetName()
switch name {
case sumName:
sum = mf.GetMetric()[0].Gauge.GetValue()
case countName:
count = mf.GetMetric()[0].Counter.GetValue()
default:
if mf.GetName() == query {
return mf.GetMetric()[0].Gauge.GetValue(), nil
}
}

if sum == 0 || count == 0 {
return 0, fmt.Errorf("failed to compute averager value for %s", query)
}

return sum / count, nil
return 0, fmt.Errorf("metric %s not found", query)
}

type topLevelMetric struct {
Name string
Queries []string
}

func calcMetric(tb testing.TB, m topLevelMetric, registry prometheus.Gatherer) float64 {
r := require.New(tb)
sum := float64(0)
for _, query := range m.Queries {
val, err := getCounterMetricValue(registry, query)
r.NoError(err, "failed to get counter value for metric %q query %q", m.Name, query)
sum += val
}
return sum
name string
query string
}

func getTopLevelMetrics(b *testing.B, registry prometheus.Gatherer, elapsed time.Duration) {
r := require.New(b)

totalGas := calcMetric(b, gasMetric, registry)
r.NotZero(totalGas, "denominator metric %q has value 0", gasMetric.Name)
totalGas, err := getCounterMetricValue(registry, gasMetric.query)
r.NoError(err)
r.NotZero(totalGas, "denominator metric %q has value 0", gasMetric.name)

mgasPerSecond := totalGas / 1_000_000 / elapsed.Seconds() // mega
b.ReportMetric(mgasPerSecond, fmt.Sprintf("m%s/s", gasMetric.Name))
var (
gGas float64 = 1_000_000_000
nsPerMs float64 = 1_000_000
)

totalGGas := totalGas / 1_000_000_000 // giga
totalGGas := totalGas / gGas
msPerGGas := (float64(elapsed) / nsPerMs) / totalGGas
b.ReportMetric(msPerGGas, "ms/gGas")

totalMSTrackedPerGGas := float64(0)
for _, metric := range topLevelMetrics {
metricValMS := calcMetric(b, metric, registry) / (totalGGas) // metric / ggas
totalMSTrackedPerGGas += metricValMS
b.ReportMetric(metricValMS, fmt.Sprintf("%s_ms/g%s", metric.Name, gasMetric.Name))
}

for _, metric := range meterVMMetrics {
query := metric.Queries[0]
queryVal, err := getAveragerValue(registry, query)
metricVal, err := getGaugeMetricValue(registry, metric.query)
r.NoError(err)

// convert from ns to ms
queryVal /= 1_000_000

b.ReportMetric(queryVal, metric.Name+"_ms/block")
metricValMS := (metricVal / nsPerMs) / totalGGas
totalMSTrackedPerGGas += metricValMS
b.ReportMetric(metricValMS, metric.name+"_ms/gGas")
}

totalSTracked := totalMSTrackedPerGGas / 1000
b.ReportMetric(totalSTracked, "s_tracked")
b.ReportMetric(elapsed.Seconds(), "s_total")
b.ReportMetric(totalSTracked/elapsed.Seconds(), "s_tracked/s_total")
b.ReportMetric(totalMSTrackedPerGGas, "tracked_ms/gGas")
}
Loading