Skip to content

Commit

Permalink
Profile: rename stats type to waitEventsStat to avoid types/packages …
Browse files Browse the repository at this point in the history
…name collisions; add and improve comments.
  • Loading branch information
lesovsky committed Mar 8, 2021
1 parent ea188d9 commit f1daad9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
40 changes: 22 additions & 18 deletions profile/profile.go
Expand Up @@ -55,24 +55,27 @@ func RunMain(dbConfig postgres.Config, config Config) error {
return profileLoop(os.Stdout, conn, config, doQuit)
}

// stats defines local statistics storage for profiled query.
type stats struct {
real float64
accumulated float64
durations map[string]float64
ratios map[string]float64
// waitEventsStat defines local statistics storage for single, profiled query.
type waitEventsStat struct {
real float64 // Number of seconds the query has been executed
accumulated float64 // Number of seconds the query and all parallel workers have been executed
durations map[string]float64 // Total per-wait_event durations in seconds
ratios map[string]float64 // Per-wait_event relative ratios to accumulated execution time, in percent
}

// newStatsStore creates new stats store.
func newStatsStore() stats {
return stats{
// newStatsStore creates new wait_events stats store.
func newStatsStore() waitEventsStat {
return waitEventsStat{
durations: make(map[string]float64),
ratios: make(map[string]float64),
}
}

// resetStatsStore deletes all entries from the stats maps counters
func resetStatsStore(s stats) stats {
// resetStatsStore deletes all entries from the wait_events stats maps counters.
func resetStatsStore(s waitEventsStat) waitEventsStat {
s.real = 0
s.accumulated = 0

for k := range s.durations {
delete(s.durations, k)
}
Expand All @@ -82,7 +85,7 @@ func resetStatsStore(s stats) stats {
return s
}

// profileStat describes snapshot of activity statistics about single profiled process.
// profileStat describes snapshot of raw activity statistics about single profiled process returned from Postgres.
type profileStat struct {
queryDurationSec float64 // number of seconds query is running at the moment of snapshot.
changeStateTime string // value of pg_stat_activity.change_state tells about when query has been finished (or new one started)
Expand Down Expand Up @@ -183,7 +186,7 @@ func profileLoop(w io.Writer, conn *postgres.DB, cfg Config, doQuit chan os.Sign

// parseActivitySnapshot parses PGresult and returns per-process profile statistics.
func parseActivitySnapshot(res stat.PGresult) map[int]profileStat {
stat := make(map[int]profileStat)
stats := make(map[int]profileStat)

for _, row := range res.Values {
var pid int
Expand Down Expand Up @@ -219,14 +222,14 @@ func parseActivitySnapshot(res stat.PGresult) map[int]profileStat {
}
}

stat[pid] = s
stats[pid] = s
}

return stat
return stats
}

// countWaitEvents counts wait events durations and its percent rations accordingly to total query time.
func countWaitEvents(s stats, targetPid int, curr map[int]profileStat, prev map[int]profileStat) stats {
func countWaitEvents(s waitEventsStat, targetPid int, curr map[int]profileStat, prev map[int]profileStat) waitEventsStat {
// Walk through current and previous activity snapshots and calculate durations
for k, vCurr := range curr {
if vPrev, ok := prev[k]; ok {
Expand Down Expand Up @@ -278,7 +281,8 @@ func printHeader(w io.Writer, curr profileStat, strsize int) error {
return nil
}

// waitEvent defines particular wait event and how many times it is occurred.
// waitEvent defines particular wait_event and its duration.
// waitEvent type is needed for translating waitEventsStat.durations map to sort-possible waitEvents slice.
type waitEvent struct {
waitEventName string
waitEventValue float64
Expand All @@ -293,7 +297,7 @@ func (p waitEvents) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p waitEvents) Less(i, j int) bool { return p[i].waitEventValue > p[j].waitEventValue }

// printStat prints report body with collected wait events durations and percent ratios.
func printStat(w io.Writer, s stats) error {
func printStat(w io.Writer, s waitEventsStat) error {
if len(s.durations) == 0 {
return nil
} // nothing to do
Expand Down
10 changes: 5 additions & 5 deletions profile/profile_test.go
Expand Up @@ -19,7 +19,7 @@ func Test_newStatsStore(t *testing.T) {
}

func Test_resetStatsStore(t *testing.T) {
s := stats{
s := waitEventsStat{
durations: map[string]float64{
"Test.Entry3": 140,
"Test.Entry2": 330,
Expand Down Expand Up @@ -143,7 +143,7 @@ func Test_countWaitEvents(t *testing.T) {
},
}

want := stats{
want := waitEventsStat{
real: 1.5,
accumulated: 5,
durations: map[string]float64{
Expand All @@ -160,7 +160,7 @@ func Test_countWaitEvents(t *testing.T) {
},
}

s := stats{
s := waitEventsStat{
durations: map[string]float64{},
ratios: map[string]float64{},
}
Expand All @@ -185,7 +185,7 @@ func Test_printHeader(t *testing.T) {
}

func Test_printStat(t *testing.T) {
s := stats{
s := waitEventsStat{
real: 10000,
accumulated: 30000,
durations: map[string]float64{
Expand All @@ -211,7 +211,7 @@ func Test_printStat(t *testing.T) {

// Test with empty stats.
buf = bytes.NewBuffer([]byte{})
assert.NoError(t, printStat(buf, stats{durations: map[string]float64{}}))
assert.NoError(t, printStat(buf, waitEventsStat{durations: map[string]float64{}}))
assert.Equal(t, "", buf.String())
}

Expand Down

0 comments on commit f1daad9

Please sign in to comment.