forked from bnb-chain/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unpromoted metric add reorg metrics add Add() metrics
- Loading branch information
andyzhang2023
committed
Nov 25, 2024
1 parent
ec9393c
commit 2eead06
Showing
3 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package legacypool | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/metrics" | ||
) | ||
|
||
// throughput is a struct that holds the throughput metrics of the transaction pool. | ||
// it is used at all key points to measure the throughput of the whole path where a transaction comes into the pool. | ||
type throughput struct { | ||
costTimer metrics.Timer | ||
lastReset time.Time | ||
cost int64 // cost in nanoseconds, need to be handled with atomic, so let's use int64 | ||
counter int64 | ||
} | ||
|
||
func (t *throughput) init(timer metrics.Timer) { | ||
t.costTimer = timer | ||
t.lastReset = time.Now() | ||
} | ||
|
||
func (t *throughput) mark(cost time.Duration, count int) { | ||
atomic.AddInt64(&t.cost, int64(cost)) | ||
atomic.AddInt64(&t.counter, int64(count)) | ||
} | ||
|
||
// avgAndReset returns the average nanoseconds of a transaction that it takes to go through the path. | ||
// it's not accurate, but it's good enough to give a rough idea of the throughput. | ||
// metrics data will be reset after this call. | ||
func (t *throughput) avgAndRest(now time.Time) (avgCost time.Duration, duration time.Duration, totalCost time.Duration, count int64, tps int64) { | ||
totalCostI64 := atomic.LoadInt64(&t.cost) | ||
if t.lastReset.IsZero() { | ||
duration = time.Duration(0) | ||
} else { | ||
duration = now.Sub(t.lastReset) | ||
} | ||
count = atomic.LoadInt64(&t.counter) | ||
totalCost = time.Duration(totalCostI64) | ||
t.costTimer.Update(totalCost) | ||
|
||
atomic.StoreInt64(&t.cost, 0) | ||
atomic.StoreInt64(&t.counter, 0) | ||
t.lastReset = now | ||
if count == 0 { | ||
avgCost = 0 | ||
} else { | ||
avgCost = time.Duration(totalCostI64 / count) | ||
} | ||
|
||
tpsF64 := float64(time.Second) / float64(totalCostI64) * float64(count) | ||
tps = int64(tpsF64) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package legacypool | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestThroughputAvg(t *testing.T) { | ||
tp := throughput{ | ||
lastReset: time.Now(), | ||
cost: 0, | ||
counter: 0, | ||
} | ||
tp.mark(200*time.Millisecond, 4) | ||
tp.mark(50*time.Millisecond, 1) | ||
avg, _, total, count, tps := tp.avgAndRest(tp.lastReset.Add(500 * time.Millisecond)) | ||
if avg != 50*time.Millisecond { | ||
t.Errorf("expected avg to be 50ms, got %v", avg) | ||
} | ||
if total != 250*time.Millisecond { | ||
t.Errorf("expected total to be 250ms, got %v", total) | ||
} | ||
if count != 5 { | ||
t.Errorf("expected count to be 5, got %v", count) | ||
} | ||
if tps != 20 { | ||
t.Errorf("expected tps to be 20, got %v", tps) | ||
} | ||
|
||
tp = throughput{} | ||
tp.lastReset = time.Now() | ||
tp.mark(200*time.Millisecond, 0) | ||
avg, _, total, count, tps = tp.avgAndRest(tp.lastReset.Add(500 * time.Millisecond)) | ||
if avg != 0 { | ||
t.Errorf("expected avg to be 0, got %v", avg) | ||
} | ||
if total != 200*time.Millisecond { | ||
t.Errorf("expected total to be 200ms, got %v", total) | ||
} | ||
if count != 0 { | ||
t.Errorf("expected count to be 0, got %v", count) | ||
} | ||
if tps != 0 { | ||
t.Errorf("expected tps to be 0, got %v", tps) | ||
} | ||
|
||
} |