1
0
mirror of https://github.com/nxshock/gwp.git synced 2024-11-27 03:31:02 +05:00

Add option to show progress

This commit is contained in:
nxshock 2021-03-27 10:52:23 +05:00
parent cda1b68900
commit 7c3de6635f
2 changed files with 10 additions and 7 deletions

View File

@ -29,7 +29,8 @@ func f(i int) error {
func main() {
worker := gwp.New(4) // Create pool with specified number of workers
worker.EstimateCount = 100 // Set estimate jobs count count to show progress
worker.ShowProgress = true // Enable progress indicator
worker.EstimateCount = 100 // Set total number on jobs to calculate ETA
for i := 0; i < 100; i++ {
n := i

14
gwp.go
View File

@ -10,11 +10,13 @@ import (
// WorkerPool represents pool of workers.
type WorkerPool struct {
jobChan chan func() error
resultChan chan error
stopChan chan struct{}
wg sync.WaitGroup
jobChan chan func() error
resultChan chan error
stopChan chan struct{}
wg sync.WaitGroup
EstimateCount int
ShowProgress bool
processedCount int // processed jobs count
errorCount int // processed jobs count that returned error
@ -80,7 +82,7 @@ func New(threadCount int) *WorkerPool {
}
func (workerPool *WorkerPool) printProgress() {
if workerPool.EstimateCount == 0 {
if !workerPool.ShowProgress {
return
}
@ -92,7 +94,7 @@ func (workerPool *WorkerPool) printProgress() {
fmt.Fprintf(os.Stderr, " Errors: %d (%.1f%%)",
workerPool.errorCount, float64(workerPool.errorCount*100)/float64(workerPool.EstimateCount))
}
if workerPool.currentSpeed > 0 {
if workerPool.EstimateCount > 0 && workerPool.currentSpeed > 0 {
fmt.Fprintf(os.Stderr, " ETA: %s at %.2f rps",
time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed), workerPool.currentSpeed)
}