Skip to content

Commit 5fa1d16

Browse files
authored
🐛 fix eta display (#7)
1 parent 8eb674a commit 5fa1d16

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,47 @@ func main() {
5252
}
5353
```
5454

55+
## 💡 For better performance
56+
57+
Using a modulo to reduce the eta display (fast example)
58+
59+
```go
60+
package main
61+
62+
import (
63+
"github.com/lab210-dev/async-job"
64+
"log"
65+
"time"
66+
)
67+
68+
func main() {
69+
// create slice of jobs
70+
var list []time.Duration
71+
for i := 1; i <= 100; i++ {
72+
list = append(list, time.Duration(1)*time.Millisecond)
73+
}
74+
err := asyncjob.New().
75+
SetWorkers(2).
76+
OnProgress(func(progress asyncjob.Progress) {
77+
// Eta will be displayed every 10 jobs
78+
if progress.Current()%10 != 0 {
79+
return
80+
}
81+
// print the eta
82+
log.Printf("Progress: %s\n", progress.String())
83+
}).
84+
Run(func(job asyncjob.Job) error {
85+
// slow down the job
86+
time.Sleep(job.Data().(time.Duration))
87+
return nil
88+
}, list)
89+
90+
// if a job returns an error, it stops the process
91+
if err != nil {
92+
log.Fatal(err)
93+
}
94+
}
95+
```
96+
5597
## 🤝 Contributions
5698
Contributors to the package are encouraged to help improve the code.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (aj *AsyncJob) _Progress() {
8888
ret := aj.jobs.Len() - aj.position
8989

9090
// call the anonymous function with data
91-
aj.onProgressFunc(Progress{aj.position, aj.jobs.Len(), time.Duration((time.Since(aj.startTimer).Milliseconds()/int64(aj.position))*int64(ret)) * time.Millisecond})
91+
aj.onProgressFunc(Progress{aj.position, aj.jobs.Len(), time.Duration((time.Since(aj.startTimer).Nanoseconds()/int64(aj.position))*int64(ret)) * time.Nanosecond})
9292
}
9393

9494
// _Next allows you to retrieve the next job

main_test.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package asyncjob
33
import (
44
"errors"
55
"github.com/stretchr/testify/assert"
6-
"io/ioutil"
76
"log"
8-
"os"
97
"sync"
108
"testing"
119
"time"
@@ -16,24 +14,6 @@ func LogJob(t *testing.T, job Job) {
1614
t.Logf("OnJob=%s key=%d", job.Data(), job.Index())
1715
}
1816

19-
// function for capture stout
20-
func CaptureStdout(t *testing.T, f func()) string {
21-
r, w, err := os.Pipe()
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
stdout := os.Stdout
26-
os.Stdout = w
27-
f()
28-
w.Close()
29-
os.Stdout = stdout
30-
out, err := ioutil.ReadAll(r)
31-
if err != nil {
32-
t.Fatal(err)
33-
}
34-
return string(out)
35-
}
36-
3717
func TestNew(t *testing.T) {
3818
t.Run("Run with empty slice data", func(t *testing.T) {
3919
err := New().
@@ -255,4 +235,34 @@ func TestNew(t *testing.T) {
255235
assert.True(t, result[2] >= 0.500 && result[2] <= 0.600)
256236
t.Log(result)
257237
})
238+
t.Run("Testing large eta", func(t *testing.T) {
239+
var list []time.Duration
240+
for i := 1; i <= 5000; i++ {
241+
list = append(list, time.Duration(1)*time.Microsecond)
242+
}
243+
var result []float64
244+
err := New().
245+
SetWorkers(2).
246+
OnProgress(func(progress Progress) {
247+
if progress.Current()%1000 != 0 {
248+
return
249+
}
250+
t.Log(progress.Current(), progress.Total(), progress.EstimateTimeLeft(), progress)
251+
result = append(result, progress.EstimateTimeLeft().Seconds())
252+
}).
253+
Run(func(job Job) error {
254+
time.Sleep(job.Data().(time.Duration))
255+
return nil
256+
}, list)
257+
258+
// test err if nil
259+
if !assert.Nil(t, err) {
260+
return
261+
}
262+
// verify all value into result
263+
for _, v := range result {
264+
assert.True(t, v != 0)
265+
}
266+
t.Log(result)
267+
})
258268
}

0 commit comments

Comments
 (0)