Skip to content

Commit 52de3a6

Browse files
committed
fix: display "N/A" as I/O timing values when track_io_timing is off and keep showing numeric values when it's on (#166)
1 parent e5a8cb1 commit 52de3a6

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module gitlab.com/postgres-ai/joe
33
go 1.15
44

55
require (
6+
github.com/AlekSi/pointer v1.1.0
67
github.com/docker/docker v20.10.5+incompatible // indirect
78
github.com/dustin/go-humanize v1.0.0
89
github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4

pkg/pgexplain/pgexplain.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type Explain struct {
7373
TempWrittenBlocks uint64
7474

7575
// IO timing.
76-
IOReadTime float64
77-
IOWriteTime float64
76+
IOReadTime *float64
77+
IOWriteTime *float64
7878

7979
ActualRows uint64
8080
MaxRows uint64
@@ -104,8 +104,8 @@ type Plan struct {
104104
TempWrittenBlocks uint64 `json:"Temp Written Blocks"`
105105

106106
// IO timing.
107-
IOReadTime float64 `json:"I/O Read Time"` // ms
108-
IOWriteTime float64 `json:"I/O Write Time"` // ms
107+
IOReadTime *float64 `json:"I/O Read Time,omitempty"` // ms
108+
IOWriteTime *float64 `json:"I/O Write Time,omitempty"` // ms
109109

110110
// Actual.
111111
ActualLoops uint64 `json:"Actual Loops"`
@@ -449,15 +449,15 @@ func (ex *Explain) writeStatsText(writer io.Writer) {
449449
fmt.Fprintf(writer, " - execution: %s%s\n", util.MillisecondsToString(ex.ExecutionTime), ex.EstimationTime)
450450

451451
ioRead := util.NA
452-
if ex.IOReadTime > 0 {
453-
ioRead = util.MillisecondsToString(ex.IOReadTime)
452+
if ex.IOReadTime != nil {
453+
ioRead = util.MillisecondsToString(*ex.IOReadTime)
454454
}
455455

456456
fmt.Fprintf(writer, " - I/O read: %s\n", ioRead)
457457

458458
ioWrite := util.NA
459-
if ex.IOWriteTime > 0 {
460-
ioWrite = util.MillisecondsToString(ex.IOWriteTime)
459+
if ex.IOWriteTime != nil {
460+
ioWrite = util.MillisecondsToString(*ex.IOWriteTime)
461461
}
462462

463463
fmt.Fprintf(writer, " - I/O write: %s\n", ioWrite)
@@ -731,11 +731,12 @@ func writePlanTextNodeDetails(outputFn func(string, ...interface{}) (int, error)
731731
}
732732

733733
ioTiming := ""
734-
if plan.IOReadTime > 0 {
735-
ioTiming += fmt.Sprintf(" read=%.3f", plan.IOReadTime)
734+
if plan.IOReadTime != nil {
735+
ioTiming += fmt.Sprintf(" read=%.3f", *plan.IOReadTime)
736736
}
737-
if plan.IOWriteTime > 0 {
738-
ioTiming += fmt.Sprintf(" write=%.3f", plan.IOWriteTime)
737+
738+
if plan.IOWriteTime != nil {
739+
ioTiming += fmt.Sprintf(" write=%.3f", *plan.IOWriteTime)
739740
}
740741

741742
if len(ioTiming) > 0 {

pkg/pgexplain/pgexplain_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"bytes"
99
"testing"
1010

11+
"github.com/AlekSi/pointer"
12+
1113
"gitlab.com/postgres-ai/joe/pkg/util"
1214

1315
"github.com/sergi/go-diff/diffmatchpatch"
@@ -1857,8 +1859,6 @@ func TestStatsText(t *testing.T) {
18571859
TotalTime: 25,
18581860
PlanningTime: 3,
18591861
ExecutionTime: 22,
1860-
IOReadTime: 0,
1861-
IOWriteTime: 0,
18621862
EstimationTime: " (estimated* for prod: 0.0018...0.0021 s)",
18631863
},
18641864
expectedResult: `
@@ -1880,8 +1880,8 @@ Shared buffers:
18801880
TotalTime: 25,
18811881
PlanningTime: 3,
18821882
ExecutionTime: 22,
1883-
IOReadTime: 3,
1884-
IOWriteTime: 5,
1883+
IOReadTime: pointer.ToFloat64(3),
1884+
IOWriteTime: pointer.ToFloat64(5),
18851885
},
18861886
expectedResult: `
18871887
Time: 25.000 ms
@@ -1890,6 +1890,28 @@ Time: 25.000 ms
18901890
- I/O read: 3.000 ms
18911891
- I/O write: 5.000 ms
18921892
1893+
Shared buffers:
1894+
- hits: 0 from the buffer pool
1895+
- reads: 0 from the OS file cache, including disk I/O
1896+
- dirtied: 0
1897+
- writes: 0
1898+
`,
1899+
},
1900+
{
1901+
explain: Explain{
1902+
TotalTime: 25,
1903+
PlanningTime: 3,
1904+
ExecutionTime: 22,
1905+
IOReadTime: pointer.ToFloat64(0),
1906+
IOWriteTime: pointer.ToFloat64(0),
1907+
},
1908+
expectedResult: `
1909+
Time: 25.000 ms
1910+
- planning: 3.000 ms
1911+
- execution: 22.000 ms
1912+
- I/O read: 0.000 ms
1913+
- I/O write: 0.000 ms
1914+
18931915
Shared buffers:
18941916
- hits: 0 from the buffer pool
18951917
- reads: 0 from the OS file cache, including disk I/O

0 commit comments

Comments
 (0)