1
0
mirror of https://github.com/nxshock/gwp.git synced 2025-04-20 01:21:52 +05:00

Compare commits

..

No commits in common. "main" and "v0.1.7" have entirely different histories.
main ... v0.1.7

4 changed files with 26 additions and 63 deletions

View File

@ -32,7 +32,7 @@ func main() {
worker.ShowProgress = true // Enable progress indicator
worker.ShowSpeed = true // Show processing speed in progress indicator
worker.SetEstimateCount(100) // Set total number on jobs to calculate ETA
worker.EstimateCount = 100 // Set total number on jobs to calculate ETA
for i := 0; i < 100; i++ {
n := i

5
go.mod
View File

@ -1,8 +1,3 @@
module github.com/nxshock/gwp
go 1.16
require (
github.com/nxshock/go-eta v0.1.1
github.com/stretchr/testify v1.8.0
)

18
go.sum
View File

@ -1,18 +0,0 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/nxshock/go-eta v0.1.0 h1:rcy8rNPaVeL3f3c5rJRLvho0FErUJThD0+vihxA7t9c=
github.com/nxshock/go-eta v0.1.0/go.mod h1:hZ59FHIJSpPeZk38pd/3zUkI4H31hAQ2oFw+NgdU+SA=
github.com/nxshock/go-eta v0.1.1 h1:N4wi3wjrXJGiheYF4zBIjOwugM+++NWsB1fpeRZaHS4=
github.com/nxshock/go-eta v0.1.1/go.mod h1:hZ59FHIJSpPeZk38pd/3zUkI4H31hAQ2oFw+NgdU+SA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

54
gwp.go
View File

@ -7,8 +7,6 @@ import (
"runtime"
"sync"
"time"
"github.com/nxshock/go-eta"
)
// WorkerPool represents pool of workers.
@ -18,7 +16,7 @@ type WorkerPool struct {
stopChan chan struct{}
wg sync.WaitGroup
estimateCount int
EstimateCount int
ShowProgress bool
ShowSpeed bool
@ -26,8 +24,6 @@ type WorkerPool struct {
errorCount int // processed jobs count that returned error
currentSpeed float64 // speed calculated for last minute
eta *eta.Calculator
lastProgressMessage string
}
@ -41,8 +37,7 @@ func New(threadCount int) *WorkerPool {
workerPool := &WorkerPool{
jobChan: make(chan func() error),
resultChan: make(chan error),
stopChan: make(chan struct{}),
eta: eta.New(0)}
stopChan: make(chan struct{})}
workerPool.wg.Add(threadCount)
@ -63,13 +58,13 @@ func New(threadCount int) *WorkerPool {
select {
case <-tickerUpdateText.C:
if !newLined {
fmt.Fprint(os.Stderr, endLine)
fmt.Fprintf(os.Stderr, endLine)
newLined = true
}
_ = workerPool.printProgress()
workerPool.printProgress()
case <-tickerCalculateEta.C:
workerPool.currentSpeed = float64(workerPool.processedCount-prevPos) * float64(time.Second) / float64(time.Since(prevTime))
workerPool.currentSpeed = float64(workerPool.processedCount-prevPos) * float64(time.Second) / float64(time.Now().Sub(prevTime))
prevPos = workerPool.processedCount
prevTime = time.Now()
case err := <-workerPool.resultChan:
@ -77,7 +72,6 @@ func New(threadCount int) *WorkerPool {
workerPool.errorCount++
}
workerPool.processedCount++
workerPool.eta.Increment(1)
case <-workerPool.stopChan:
return
}
@ -97,54 +91,46 @@ func New(threadCount int) *WorkerPool {
return workerPool
}
func (workerPool *WorkerPool) SetEstimateCount(n int) {
workerPool.estimateCount = n
workerPool.eta.TotalCount = n
}
func (workerPool *WorkerPool) printProgress() error {
func (workerPool *WorkerPool) printProgress() {
if !workerPool.ShowProgress {
return nil
return
}
buf := new(bytes.Buffer)
fmt.Fprint(buf, newLine)
fmt.Fprintf(buf, newLine)
if workerPool.estimateCount == 0 {
if workerPool.EstimateCount == 0 {
fmt.Fprintf(buf, "Progress: %d", workerPool.processedCount)
} else {
fmt.Fprintf(buf, "Progress: %.1f%% (%d / %d)",
float64(workerPool.processedCount*100)/float64(workerPool.estimateCount), workerPool.processedCount, workerPool.estimateCount)
float64(workerPool.processedCount*100)/float64(workerPool.EstimateCount), workerPool.processedCount, workerPool.EstimateCount)
}
if workerPool.errorCount > 0 {
fmt.Fprintf(buf, " Errors: %d (%.1f%%)",
workerPool.errorCount, float64(workerPool.errorCount*100)/float64(workerPool.estimateCount))
workerPool.errorCount, float64(workerPool.errorCount*100)/float64(workerPool.EstimateCount))
}
if workerPool.estimateCount > 0 {
fmt.Fprintf(buf, " ETA: %s", fmtDuration(time.Until(workerPool.eta.Average())))
if workerPool.currentSpeed > 0 {
if workerPool.EstimateCount > 0 {
fmt.Fprintf(buf, " ETA: %s", fmtDuration(time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed)))
}
if workerPool.currentSpeed > 0 && workerPool.ShowSpeed {
if workerPool.ShowSpeed {
fmt.Fprintf(buf, " Speed: %.2f rps", workerPool.currentSpeed)
}
}
fmt.Fprint(buf, endLine)
if buf.String() == workerPool.lastProgressMessage {
return nil
return
}
buf.WriteTo(os.Stderr)
workerPool.lastProgressMessage = buf.String()
_, err := buf.WriteTo(os.Stderr)
if err != nil {
return err
}
return nil
}
// Add sends specified task for execution.
@ -159,7 +145,7 @@ func (workerPool *WorkerPool) CloseAndWait() {
workerPool.stopChan <- struct{}{}
close(workerPool.resultChan)
_ = workerPool.printProgress()
workerPool.printProgress()
}
// ErrorCount returns total error count.