stats.go 2.19 KB
Newer Older
Vladimir Barsukov's avatar
Vladimir Barsukov committed
1
2
3
4
5
6
7
package zdb

import (
	"time"
)

type Stat struct {
Vladimir Barsukov's avatar
lint    
Vladimir Barsukov committed
8
	ID   string
Vladimir Barsukov's avatar
Vladimir Barsukov committed
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
	Mode connMode
	Host string
	Port uint16

	AcquireCount            int64
	AcquireDuration         time.Duration
	AcquiredConns           int32
	CanceledAcquireCount    int64
	ConstructingConns       int32
	EmptyAcquireCount       int64
	IdleConns               int32
	MaxConns                int32
	TotalConns              int32
	NewConnsCount           int64
	MaxLifetimeDestroyCount int64
	MaxIdleDestroyCount     int64
}

func (d *Pool) StatMaster() *Stat {
	return d.Stat(d.SrvMaster)
}

func (d *Pool) StatPool() []*Stat {
	out := make([]*Stat, 0)
	for _, q := range append(d.SrvSync, d.SrvAsync...) {
		out = append(out, d.Stat(q))
	}

	if d.SrvMaster != nil {
		return append(out, d.Stat(d.SrvMaster))
	}

	return out
}

func (d *Pool) StatPoolTotal() *Stat {
Vladimir Barsukov's avatar
lint    
Vladimir Barsukov committed
45
	m := &Stat{ID: "pool", Mode: connModePool}
Vladimir Barsukov's avatar
Vladimir Barsukov committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

	for _, s := range d.StatPool() {
		m.AcquireCount += s.AcquireCount
		m.AcquireDuration += s.AcquireDuration
		m.AcquiredConns += s.AcquiredConns
		m.CanceledAcquireCount += s.CanceledAcquireCount
		m.ConstructingConns += s.ConstructingConns
		m.EmptyAcquireCount += s.EmptyAcquireCount
		m.IdleConns += s.IdleConns
		m.MaxConns += s.MaxConns
		m.TotalConns += s.TotalConns
		m.NewConnsCount += s.NewConnsCount
		m.MaxLifetimeDestroyCount += s.MaxLifetimeDestroyCount
		m.MaxIdleDestroyCount += s.MaxIdleDestroyCount
	}

	return m
}

func (d *Pool) Stat(q *Conn) *Stat {
	s := q.Stat()

	return &Stat{
Vladimir Barsukov's avatar
lint    
Vladimir Barsukov committed
69
		ID:                      q.ToString(),
Vladimir Barsukov's avatar
Vladimir Barsukov committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
		Mode:                    q.Mode,
		Host:                    q.Config().ConnConfig.Host,
		Port:                    q.Config().ConnConfig.Port,
		AcquireCount:            s.AcquireCount(),
		AcquireDuration:         s.AcquireDuration(),
		AcquiredConns:           s.AcquiredConns(),
		CanceledAcquireCount:    s.CanceledAcquireCount(),
		ConstructingConns:       s.ConstructingConns(),
		EmptyAcquireCount:       s.EmptyAcquireCount(),
		IdleConns:               s.IdleConns(),
		MaxConns:                s.MaxConns(),
		TotalConns:              s.TotalConns(),
		NewConnsCount:           s.NewConnsCount(),
		MaxLifetimeDestroyCount: s.MaxLifetimeDestroyCount(),
		MaxIdleDestroyCount:     s.MaxIdleDestroyCount(),
	}
}