forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add log statistics * add reset and batch number * total duration with milliseconds * add batch gas used statistics
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 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,40 @@ | ||
package metrics | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type LogTag string | ||
|
||
type LogStatistics interface { | ||
CumulativeCounting(tag LogTag) | ||
CumulativeValue(tag LogTag, value int64) | ||
CumulativeTiming(tag LogTag, duration time.Duration) | ||
SetTag(tag LogTag, value string) | ||
Summary() string | ||
ResetStatistics() | ||
|
||
UpdateTimestamp(tag LogTag, tm time.Time) | ||
} | ||
|
||
const ( | ||
TxCounter LogTag = "TxCounter" | ||
GetTx LogTag = "GetTx" | ||
GetTxPauseCounter LogTag = "GetTxPauseCounter" | ||
BatchCloseReason LogTag = "BatchCloseReason" | ||
ReprocessingTxCounter LogTag = "ReProcessingTxCounter" | ||
FailTxCounter LogTag = "FailTxCounter" | ||
NewRound LogTag = "NewRound" | ||
BatchGas LogTag = "BatchGas" | ||
|
||
ProcessingTxTiming LogTag = "ProcessingTxTiming" | ||
ProcessingInvalidTxCounter LogTag = "ProcessingInvalidTxCounter" | ||
ProcessingTxCommit LogTag = "ProcessingTxCommit" | ||
ProcessingTxResponse LogTag = "ProcessingTxResponse" | ||
|
||
FinalizeBatchTiming LogTag = "FinalizeBatchTiming" | ||
FinalizeBatchNumber LogTag = "FinalizeBatchNumber" | ||
FinalizeBatchReprocessFullBatch LogTag = "FinalizeBatchReprocessFullBatch" | ||
FinalizeBatchCloseBatch LogTag = "FinalizeBatchCloseBatch" | ||
FinalizeBatchOpenBatch LogTag = "FinalizeBatchOpenBatch" | ||
) |
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,85 @@ | ||
package metrics | ||
|
||
import ( | ||
"strconv" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var instance *logStatisticsInstance | ||
var once sync.Once | ||
|
||
func GetLogStatistics() LogStatistics { | ||
once.Do(func() { | ||
instance = &logStatisticsInstance{} | ||
instance.init() | ||
}) | ||
return instance | ||
} | ||
|
||
type logStatisticsInstance struct { | ||
timestamp map[LogTag]time.Time | ||
statistics map[LogTag]int64 // value maybe the counter or time.Duration(ms) | ||
tags map[LogTag]string | ||
} | ||
|
||
func (l *logStatisticsInstance) init() { | ||
l.timestamp = make(map[LogTag]time.Time) | ||
l.statistics = make(map[LogTag]int64) | ||
l.tags = make(map[LogTag]string) | ||
} | ||
|
||
func (l *logStatisticsInstance) CumulativeCounting(tag LogTag) { | ||
l.statistics[tag]++ | ||
} | ||
|
||
func (l *logStatisticsInstance) CumulativeValue(tag LogTag, value int64) { | ||
l.statistics[tag] += value | ||
} | ||
|
||
func (l *logStatisticsInstance) CumulativeTiming(tag LogTag, duration time.Duration) { | ||
l.statistics[tag] += duration.Milliseconds() | ||
} | ||
|
||
func (l *logStatisticsInstance) SetTag(tag LogTag, value string) { | ||
l.tags[tag] = value | ||
} | ||
|
||
func (l *logStatisticsInstance) UpdateTimestamp(tag LogTag, tm time.Time) { | ||
l.timestamp[tag] = tm | ||
} | ||
|
||
func (l *logStatisticsInstance) ResetStatistics() { | ||
l.statistics = make(map[LogTag]int64) | ||
l.tags = make(map[LogTag]string) | ||
} | ||
|
||
func (l *logStatisticsInstance) Summary() string { | ||
batchTotalDuration := "-" | ||
if key, ok := l.timestamp[NewRound]; ok { | ||
batchTotalDuration = strconv.Itoa(int(time.Since(key).Milliseconds())) | ||
} | ||
processTxTiming := "ProcessTx<" + strconv.Itoa(int(l.statistics[ProcessingTxTiming])) + "ms, " + | ||
"Commit<" + strconv.Itoa(int(l.statistics[ProcessingTxCommit])) + "ms>, " + | ||
"ProcessResponse<" + strconv.Itoa(int(l.statistics[ProcessingTxResponse])) + "ms>>, " | ||
|
||
finalizeBatchTiming := "FinalizeBatch<" + strconv.Itoa(int(l.statistics[FinalizeBatchTiming])) + "ms, " + | ||
"ReprocessFullBatch<" + strconv.Itoa(int(l.statistics[FinalizeBatchReprocessFullBatch])) + "ms>, " + | ||
"CloseBatch<" + strconv.Itoa(int(l.statistics[FinalizeBatchCloseBatch])) + "ms>, " + | ||
"OpenBatch<" + strconv.Itoa(int(l.statistics[FinalizeBatchOpenBatch])) + "ms>>, " | ||
|
||
result := "Batch<" + l.tags[FinalizeBatchNumber] + ">, " + | ||
"TotalDuration<" + batchTotalDuration + "ms>, " + | ||
"GasUsed<" + strconv.Itoa(int(l.statistics[BatchGas])) + ">, " + | ||
"Tx<" + strconv.Itoa(int(l.statistics[TxCounter])) + ">, " + | ||
"GetTx<" + strconv.Itoa(int(l.statistics[GetTx])) + "ms>, " + | ||
"GetTxPause<" + strconv.Itoa(int(l.statistics[GetTxPauseCounter])) + ">, " + | ||
"ReprocessTx<" + strconv.Itoa(int(l.statistics[ReprocessingTxCounter])) + ">, " + | ||
"FailTx<" + strconv.Itoa(int(l.statistics[FailTxCounter])) + ">, " + | ||
"InvalidTx<" + strconv.Itoa(int(l.statistics[ProcessingInvalidTxCounter])) + ">, " + | ||
processTxTiming + | ||
finalizeBatchTiming + | ||
"BatchCloseReason<" + l.tags[BatchCloseReason] + ">" | ||
|
||
return result | ||
} |
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,51 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_logStatisticsInstance_Summary(t *testing.T) { | ||
type fields struct { | ||
timestamp map[LogTag]time.Time | ||
statistics map[LogTag]int64 | ||
tags map[LogTag]string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
// TODO: Add test cases. | ||
{"1", fields{ | ||
timestamp: map[LogTag]time.Time{NewRound: time.Now().Add(-time.Second)}, | ||
statistics: map[LogTag]int64{ | ||
BatchGas: 111111, | ||
TxCounter: 10, | ||
GetTx: time.Second.Milliseconds(), | ||
GetTxPauseCounter: 2, | ||
ReprocessingTxCounter: 3, | ||
FailTxCounter: 1, | ||
ProcessingInvalidTxCounter: 2, | ||
ProcessingTxTiming: time.Second.Milliseconds() * 30, | ||
ProcessingTxCommit: time.Second.Milliseconds() * 10, | ||
ProcessingTxResponse: time.Second.Milliseconds() * 15, | ||
FinalizeBatchTiming: time.Second.Milliseconds() * 50, | ||
FinalizeBatchReprocessFullBatch: time.Second.Milliseconds() * 20, | ||
FinalizeBatchCloseBatch: time.Second.Milliseconds() * 10, | ||
FinalizeBatchOpenBatch: time.Second.Milliseconds() * 10, | ||
}, | ||
tags: map[LogTag]string{BatchCloseReason: "deadline", FinalizeBatchNumber: "123"}, | ||
}, "test"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := &logStatisticsInstance{ | ||
timestamp: tt.fields.timestamp, | ||
statistics: tt.fields.statistics, | ||
tags: tt.fields.tags, | ||
} | ||
t.Log(l.Summary()) | ||
}) | ||
} | ||
} |