diff --git a/go.mod b/go.mod index 9f16206..a1a7fd2 100644 --- a/go.mod +++ b/go.mod @@ -15,10 +15,13 @@ require ( require ( github.com/gosuri/uiprogress v0.0.1 github.com/jessevdk/go-flags v1.5.0 + github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 + github.com/valyala/fasthttp v1.40.0 ) require ( github.com/andybalholm/brotli v1.0.4 // indirect + github.com/chainreactors/go-metrics v0.0.0-20220926021830-24787b7a10f8 // indirect github.com/go-dedup/megophone v0.0.0-20170830025436-f01be21026f5 // indirect github.com/go-dedup/text v0.0.0-20170907015346-8bb1b95e3cb7 // indirect github.com/gosuri/uilive v0.0.4 // indirect @@ -26,7 +29,6 @@ require ( github.com/mattn/go-isatty v0.0.16 // indirect github.com/twmb/murmur3 v1.1.6 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.40.0 // indirect golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 3470c34..bd328dc 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/chainreactors/files v0.2.0 h1:LeN97o4VxIvK9ZACjXfdRTR+N7puXuWyQO5GarCkMLM= github.com/chainreactors/files v0.2.0/go.mod h1:/Xa9YXhjBlaC33JTD6ZTJFig6pcplak2IDcovf42/6A= +github.com/chainreactors/go-metrics v0.0.0-20220926021830-24787b7a10f8 h1:kMFr1Hj+rkp1wBPIw2pcQvelO5GnA7r7wY3h6vJ1joA= +github.com/chainreactors/go-metrics v0.0.0-20220926021830-24787b7a10f8/go.mod h1:7NDvFERNiXsujaBPD6s4WXj52uKdfnF2zVHQtKXIEV4= github.com/chainreactors/gogo/v2 v2.8.7 h1:pN83yw7mtLFcNV12ZwxKdTR2sDZFSiqva/JRQMMRvao= github.com/chainreactors/gogo/v2 v2.8.7/go.mod h1:KFMyHLZSkpgDh/17N/k9gKYQ81MO+9MLdTMHQvoBSj0= github.com/chainreactors/ipcs v0.0.9/go.mod h1:E9M3Ohyq0TYQLlV4i2dbM9ThBZB1Nnd7Oexoie2xLII= @@ -44,6 +46,8 @@ github.com/panjf2000/ants/v2 v2.5.0 h1:1rWGWSnxCsQBga+nQbA4/iY6VMeNoOIAM0ZWh9u3q github.com/panjf2000/ants/v2 v2.5.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE= 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/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= +github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pkg/bar.go b/pkg/bar.go index 77bb44e..093d8e8 100644 --- a/pkg/bar.go +++ b/pkg/bar.go @@ -2,49 +2,44 @@ package pkg import ( "fmt" + "github.com/chainreactors/go-metrics" "github.com/gosuri/uiprogress" - "time" ) func NewBar(u string, total int, progress *uiprogress.Progress) *Bar { bar := &Bar{ - Bar: progress.AddBar(total), - url: u, - spend: 1, + Bar: progress.AddBar(total), + url: u, + m: metrics.NewMeter(), } - bar.AppendCompleted() - bar.PrependElapsed() + metrics.Register(bar.url, bar.m) + bar.PrependCompleted() bar.PrependFunc(func(b *uiprogress.Bar) string { - return fmt.Sprintf("%v/s", bar.Current()/bar.spend) + return fmt.Sprintf("%f/s %v/%v", bar.m.Rate1(), bar.m.Count(), bar.Bar.Total) }) - bar.PrependFunc(func(b *uiprogress.Bar) string { return u }) + bar.AppendElapsed() - go func() { - for !bar.close { - select { - case <-time.After(time.Duration(250) * time.Millisecond): - bar.spend++ - } - } - }() return bar } type Bar struct { - spend int url string + total int close bool *uiprogress.Bar + m metrics.Meter } func (bar *Bar) Done() { + bar.m.Mark(1) bar.Incr() } func (bar *Bar) Close() { + metrics.Unregister(bar.url) bar.close = true }