forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtps_counter.go
144 lines (122 loc) · 3.79 KB
/
tps_counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2022 Evmos Foundation
// This file is part of the Evmos Network packages.
//
// Evmos is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Evmos packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE
package app
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/tendermint/tendermint/libs/log"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
tagKeyStatus = tag.MustNewKey("status")
mTransactions = stats.Int64("transactions", "the number of transactions after .EndBlocker", "1")
viewTransactions = &view.View{
Name: "transactions_processed",
Measure: mTransactions,
Description: "The transactions processed",
TagKeys: []tag.Key{tagKeyStatus},
Aggregation: view.Count(),
}
)
func ObservabilityViews() (views []*view.View) {
views = append(views, viewTransactions)
return views
}
type tpsCounter struct {
nSuccessful, NFailed uint64
reportPeriod time.Duration
logger log.Logger
doneCloseOnce sync.Once
doneCh chan bool
}
func newTPSCounter(logger log.Logger) *tpsCounter {
return &tpsCounter{logger: logger, doneCh: make(chan bool, 1)}
}
func (tpc *tpsCounter) incrementSuccess() { atomic.AddUint64(&tpc.nSuccessful, 1) }
func (tpc *tpsCounter) incrementFailure() { atomic.AddUint64(&tpc.NFailed, 1) }
const defaultTPSReportPeriod = 10 * time.Second
func (tpc *tpsCounter) start(ctx context.Context) error {
tpsReportPeriod := defaultTPSReportPeriod
if tpc.reportPeriod > 0 {
tpsReportPeriod = tpc.reportPeriod
}
ticker := time.NewTicker(tpsReportPeriod)
defer ticker.Stop()
defer tpc.doneCloseOnce.Do(func() {
close(tpc.doneCh)
})
var lastNSuccessful, lastNFailed uint64
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
// Report the number of transactions seen in the designated period of time.
latestNSuccessful := atomic.LoadUint64(&tpc.nSuccessful)
latestNFailed := atomic.LoadUint64(&tpc.NFailed)
var nTxn int64
nSuccess, err := tpc.recordValue(ctx, latestNSuccessful, lastNSuccessful, statusSuccess)
if err == nil {
nTxn += nSuccess
} else {
panic(err)
}
nFailed, err := tpc.recordValue(ctx, latestNFailed, lastNFailed, statusFailure)
if err == nil {
nTxn += nFailed
} else {
panic(err)
}
if nTxn != 0 {
// Record to our logger for easy examination in the logs.
secs := float64(tpsReportPeriod) / float64(time.Second)
tpc.logger.Info("Transactions per second", "tps", float64(nTxn)/secs)
}
lastNFailed = latestNFailed
lastNSuccessful = latestNSuccessful
}
}
}
type status string
const (
statusSuccess = "success"
statusFailure = "failure"
)
func (tpc *tpsCounter) recordValue(ctx context.Context, latest, previous uint64, status status) (int64, error) {
if latest < previous {
return 0, nil
}
n := int64(latest - previous)
if n < 0 {
// Perhaps we exceeded the uint64 limits then wrapped around, for the latest value.
// TODO: Perhaps log this?
return 0, nil
}
statusValue := "OK"
if status == statusFailure {
statusValue = "ERR"
}
ctx, err := tag.New(ctx, tag.Upsert(tagKeyStatus, statusValue))
if err != nil {
return 0, err
}
stats.Record(ctx, mTransactions.M(n))
return n, nil
}