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() { func main() {
worker := gwp.New(4) // Create pool with specified number of workers 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++ { for i := 0; i < 100; i++ {
n := i n := i

6
gwp.go
View File

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