-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/bench: Output statistics from cluster nodes
Nodes gather the following statistics as they run: - the number of delivered requests, - the average number of delivered requests per second, - the average latency to deliver a request. Nodes periodically output statistic records in CSV format to the specified file or to the standard output.
- Loading branch information
Sergey Fedorov
committed
Aug 8, 2022
1 parent
f876eb6
commit a16c5e4
Showing
3 changed files
with
177 additions
and
1 deletion.
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,35 @@ | ||
// Copyright Contributors to the Mir project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/filecoin-project/mir/pkg/events" | ||
"github.com/filecoin-project/mir/pkg/pb/eventpb" | ||
) | ||
|
||
type StatInterceptor struct { | ||
*Stats | ||
} | ||
|
||
func NewStatInterceptor(s *Stats) *StatInterceptor { | ||
return &StatInterceptor{s} | ||
} | ||
|
||
func (i *StatInterceptor) Intercept(events *events.EventList) error { | ||
it := events.Iterator() | ||
for e := it.Next(); e != nil; e = it.Next() { | ||
switch e := e.Type.(type) { | ||
case *eventpb.Event_NewRequests: | ||
for _, req := range e.NewRequests.Requests { | ||
i.Stats.NewRequest(req) | ||
} | ||
case *eventpb.Event_Deliver: | ||
for _, req := range e.Deliver.Batch.Requests { | ||
i.Stats.Delivered(req.Req) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,99 @@ | ||
// Copyright Contributors to the Mir project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/csv" | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/filecoin-project/mir/pkg/pb/requestpb" | ||
) | ||
|
||
type Stats struct { | ||
lock sync.RWMutex | ||
reqTimestamps map[reqKey]time.Time | ||
avgLatency float64 | ||
timestampedRequests int | ||
deliveredRequests int | ||
} | ||
|
||
type reqKey struct { | ||
ClientID string | ||
ReqNo uint64 | ||
} | ||
|
||
func NewStats() *Stats { | ||
return &Stats{ | ||
reqTimestamps: make(map[reqKey]time.Time), | ||
} | ||
} | ||
|
||
func (s *Stats) NewRequest(req *requestpb.Request) { | ||
s.lock.Lock() | ||
k := reqKey{req.ClientId, req.ReqNo} | ||
s.reqTimestamps[k] = time.Now() | ||
s.lock.Unlock() | ||
} | ||
|
||
func (s *Stats) Delivered(req *requestpb.Request) { | ||
s.lock.Lock() | ||
s.deliveredRequests++ | ||
k := reqKey{req.ClientId, req.ReqNo} | ||
if t, ok := s.reqTimestamps[k]; ok { | ||
delete(s.reqTimestamps, k) | ||
s.timestampedRequests++ | ||
d := time.Since(t) | ||
|
||
// $CA_{n+1} = CA_n + {x_{n+1} - CA_n \over n + 1}$ | ||
s.avgLatency += (float64(d) - s.avgLatency) / float64(s.timestampedRequests) | ||
} | ||
s.lock.Unlock() | ||
} | ||
|
||
func (s *Stats) AvgLatency() time.Duration { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
return time.Duration(s.avgLatency) | ||
} | ||
|
||
func (s *Stats) DeliveredRequests() int { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
return s.deliveredRequests | ||
} | ||
|
||
func (s *Stats) Reset() { | ||
s.lock.Lock() | ||
s.avgLatency = 0 | ||
s.timestampedRequests = 0 | ||
s.deliveredRequests = 0 | ||
s.lock.Unlock() | ||
} | ||
|
||
func (s *Stats) WriteCSVHeader(w *csv.Writer) { | ||
record := []string{ | ||
"nrDelivered", | ||
"tps", | ||
"avgLatency", | ||
} | ||
_ = w.Write(record) | ||
} | ||
|
||
func (s *Stats) WriteCSVRecord(w *csv.Writer, d time.Duration) { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
tps := float64(s.deliveredRequests) / (float64(d) / float64(time.Second)) | ||
record := []string{ | ||
strconv.Itoa(s.deliveredRequests), | ||
strconv.Itoa(int(tps)), | ||
strconv.FormatFloat(s.avgLatency, 'f', 0, 64), | ||
} | ||
_ = w.Write(record) | ||
} |