stat.go 1.07 KB
Newer Older
1 2 3
package bitswap

import (
Jeromy's avatar
Jeromy committed
4
	"sort"
5

6
	cid "gitlab.dms3.io/dms3/go-cid"
7 8
)

9
// Stat is a struct that provides various statistics on bitswap operations
10
type Stat struct {
11 12 13 14 15 16 17 18 19 20
	ProvideBufLen    int
	Wantlist         []cid.Cid
	Peers            []string
	BlocksReceived   uint64
	DataReceived     uint64
	BlocksSent       uint64
	DataSent         uint64
	DupBlksReceived  uint64
	DupDataReceived  uint64
	MessagesReceived uint64
21 22
}

23
// Stat returns aggregated statistics about bitswap operations
24 25 26 27
func (bs *Bitswap) Stat() (*Stat, error) {
	st := new(Stat)
	st.ProvideBufLen = len(bs.newBlocks)
	st.Wantlist = bs.GetWantlist()
Jeromy's avatar
Jeromy committed
28
	bs.counterLk.Lock()
29 30 31 32 33 34 35
	c := bs.counters
	st.BlocksReceived = c.blocksRecvd
	st.DupBlksReceived = c.dupBlocksRecvd
	st.DupDataReceived = c.dupDataRecvd
	st.BlocksSent = c.blocksSent
	st.DataSent = c.dataSent
	st.DataReceived = c.dataRecvd
36
	st.MessagesReceived = c.messagesRecvd
Jeromy's avatar
Jeromy committed
37
	bs.counterLk.Unlock()
38

39 40 41 42
	peers := bs.engine.Peers()
	st.Peers = make([]string, 0, len(peers))

	for _, p := range peers {
Jeromy's avatar
Jeromy committed
43 44 45
		st.Peers = append(st.Peers, p.Pretty())
	}
	sort.Strings(st.Peers)
46 47 48

	return st, nil
}