Skip to content

Commit 7684ca3

Browse files
authored
Add incremental speed status mode for more responsive speed and estimates (#15)
1 parent 95fdd83 commit 7684ca3

9 files changed

Lines changed: 77 additions & 46 deletions

File tree

.github/workflows/golangci-lint.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
steps:
2222
- uses: actions/setup-go@v3
2323
with:
24-
go-version: 1.22.x
24+
go-version: 1.23.x
2525
- uses: actions/checkout@v2
2626
- name: golangci-lint
27-
uses: golangci/golangci-lint-action@v4.0.0
27+
uses: golangci/golangci-lint-action@v6.1.0
2828
with:
2929
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
30-
version: v1.56.2
30+
version: v1.61.0
3131

3232
# Optional: working directory, useful for monorepos
3333
# working-directory: somedir

.github/workflows/gorelease.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ concurrency:
99
cancel-in-progress: true
1010

1111
env:
12-
GO_VERSION: 1.22.x
12+
GO_VERSION: 1.23.x
1313
jobs:
1414
gorelease:
1515
runs-on: ubuntu-latest

.github/workflows/release-assets.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- created
99
env:
1010
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11-
GO_VERSION: 1.22.x
11+
GO_VERSION: 1.23.x
1212
LINUX_AMD64_BUILD_OPTIONS: '-tags cgo_zstd'
1313
GOAMD64: v3
1414
jobs:

.github/workflows/test-unit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
test:
2222
strategy:
2323
matrix:
24-
go-version: [ 1.20.x, 1.21.x, 1.22.x ]
24+
go-version: [ 1.21.x, 1.22.x, 1.23.x ]
2525
runs-on: ubuntu-latest
2626
steps:
2727
- name: Install Go stable

.golangci.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,32 @@ linters:
2525
enable-all: true
2626
disable:
2727
- lll
28-
- maligned
2928
- gochecknoglobals
3029
- gomnd
3130
- wrapcheck
3231
- paralleltest
3332
- forbidigo
34-
- exhaustivestruct
35-
- interfacer # deprecated
3633
- forcetypeassert
37-
- scopelint # deprecated
38-
- ifshort # too many false positives
39-
- golint # deprecated
4034
- varnamelen
4135
- tagliatelle
4236
- errname
4337
- ireturn
4438
- exhaustruct
4539
- nonamedreturns
46-
- nosnakecase
47-
- structcheck
48-
- varcheck
49-
- deadcode
5040
- testableexamples
5141
- dupword
5242
- depguard
5343
- tagalign
44+
- execinquery
45+
- mnd
46+
- testifylint
5447

5548
issues:
5649
exclude-use-default: false
5750
exclude-rules:
5851
- linters:
5952
- gomnd
53+
- mnd
6054
- goconst
6155
- goerr113
6256
- noctx
@@ -71,4 +65,7 @@ issues:
7165
- errcheck # Error checking omitted for brevity.
7266
- gosec
7367
path: "example_"
68+
- linters:
69+
- revive
70+
text: "unused-parameter: parameter"
7471

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.56.2" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.61.0" # Optional configuration to pinpoint golangci-lint version.
22

33
# The head of Makefile determines location of dev-go to include standard targets.
44
GO ?= go

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/bool64/progress
33
go 1.21
44

55
require (
6-
github.com/DataDog/zstd v1.5.5
7-
github.com/bool64/dev v0.2.34
8-
github.com/klauspost/compress v1.17.8
6+
github.com/DataDog/zstd v1.5.6
7+
github.com/bool64/dev v0.2.36
8+
github.com/klauspost/compress v1.17.10
99
github.com/klauspost/pgzip v1.2.6
1010
)

go.sum

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
2-
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
3-
github.com/bool64/dev v0.2.33 h1:ETAcSa8H9w4talcCdSQCCnLX7PMHmuxdLcDl6TpSDj4=
4-
github.com/bool64/dev v0.2.33/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
5-
github.com/bool64/dev v0.2.34 h1:P9n315P8LdpxusnYQ0X7MP1CZXwBK5ae5RZrd+GdSZE=
6-
github.com/bool64/dev v0.2.34/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
7-
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
8-
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
9-
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
10-
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
1+
github.com/DataDog/zstd v1.5.6 h1:LbEglqepa/ipmmQJUDnSsfvA8e8IStVcGaFWDuxvGOY=
2+
github.com/DataDog/zstd v1.5.6/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
3+
github.com/bool64/dev v0.2.36 h1:yU3bbOTujoxhWnt8ig8t94PVmZXIkCaRj9C57OtqJBY=
4+
github.com/bool64/dev v0.2.36/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
5+
github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0=
6+
github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
117
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
128
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=

progress.go

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Status struct {
1414
Task string `json:"task"`
1515
DonePercent float64 `json:"done_percent"`
1616
LinesCompleted int64 `json:"lines_completed"`
17+
BytesCompleted int64 `json:"bytes_completed"`
1718
SpeedMBPS float64 `json:"speed_mbps"`
1819
SpeedLPS float64 `json:"speed_lps"`
1920
Elapsed time.Duration `json:"-"`
@@ -27,13 +28,20 @@ type Progress struct {
2728
Print func(status Status)
2829
ShowHeapStats bool
2930
ShowLinesStats bool
30-
done chan bool
31-
task Task
32-
lines func() int64
33-
current func() int64
34-
tot func() int64
35-
prnt func(s Status)
36-
start time.Time
31+
32+
// IncrementalSpeed shows speed and remaining estimate based on performance between two status updates.
33+
IncrementalSpeed bool
34+
35+
done chan bool
36+
task Task
37+
lines func() int64
38+
current func() int64
39+
tot func() int64
40+
prnt func(s Status)
41+
start time.Time
42+
43+
prevStatus Status
44+
3745
continuedLines int64
3846
continuedBytes int64
3947
metrics []Metric
@@ -191,16 +199,34 @@ func (p *Progress) Reset() {
191199
p.metrics = nil
192200
}
193201

194-
func (p *Progress) printStatus(last bool) {
195-
s := Status{}
196-
s.Task = p.task.Task
197-
s.LinesCompleted = p.Lines()
198-
s.Metrics = p.metrics
202+
func (p *Progress) speedStatus(s *Status) {
203+
if p.IncrementalSpeed {
204+
lc := s.LinesCompleted - p.prevStatus.LinesCompleted
205+
bc := s.BytesCompleted - p.prevStatus.BytesCompleted
206+
dc := s.DonePercent - p.prevStatus.DonePercent
207+
ela := s.Elapsed - p.prevStatus.Elapsed
199208

200-
b := float64(p.Bytes())
201-
s.DonePercent = 100 * b / float64(p.tot())
202-
s.Elapsed = time.Since(p.start)
203-
s.SpeedMBPS = (b / s.Elapsed.Seconds()) / (1024 * 1024)
209+
if lc > 0 {
210+
s.SpeedLPS = float64(lc) / ela.Seconds()
211+
}
212+
213+
if bc > 0 {
214+
s.SpeedMBPS = (float64(bc) / ela.Seconds()) / (1024 * 1024)
215+
}
216+
217+
if s.DonePercent > 0 {
218+
s.Remaining = time.Duration((100.0 - s.DonePercent) * float64(ela) / dc)
219+
s.Remaining = s.Remaining.Truncate(time.Second)
220+
} else {
221+
s.Remaining = 0
222+
}
223+
224+
p.prevStatus = *s
225+
226+
return
227+
}
228+
229+
s.SpeedMBPS = (float64(s.BytesCompleted) / s.Elapsed.Seconds()) / (1024 * 1024)
204230
s.SpeedLPS = float64(s.LinesCompleted) / s.Elapsed.Seconds()
205231

206232
if s.DonePercent > 0 {
@@ -209,6 +235,18 @@ func (p *Progress) printStatus(last bool) {
209235
} else {
210236
s.Remaining = 0
211237
}
238+
}
239+
240+
func (p *Progress) printStatus(last bool) {
241+
s := Status{}
242+
s.Task = p.task.Task
243+
s.LinesCompleted = p.Lines()
244+
s.BytesCompleted = p.Bytes()
245+
s.Metrics = p.metrics
246+
s.Elapsed = time.Since(p.start)
247+
s.DonePercent = 100 * float64(s.BytesCompleted) / float64(p.tot())
248+
249+
p.speedStatus(&s)
212250

213251
if s.Remaining > 100*time.Millisecond || s.Remaining == 0 || last {
214252
p.prnt(s)

0 commit comments

Comments
 (0)