Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
icefed committed Apr 10, 2024
1 parent 90fdb71 commit 8ecb12e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,64 @@
- Automatic scaling Number of routines by the number of tasks.
- Support waiting all tasks finished.
- Panic in tasks will be recovered.

### Usages

Write a http benchmark tool with routinepool, calculate the average time of each request.
```
package main
import (
"fmt"
"io"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/icefed/routinepool"
)
func main() {
p := routinepool.NewPool(routinepool.WithMaxWorkers(8))
p.StartN(8)
var errCount int32
client := &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 12,
},
}
costs := make([]time.Duration, 0)
mu := sync.Mutex{}
f := func() {
start := time.Now()
defer func() {
mu.Lock()
defer mu.Unlock()
costs = append(costs, time.Since(start))
}()
req, _ := http.NewRequest("GET", "http://localhost:8099/hello", nil)
resp, err := client.Do(req)
io.Copy(io.Discard, resp.Body)
if err != nil {
atomic.AddInt32(&errCount, 1)
return
}
resp.Body.Close()
}
for i := 0; i < 100000; i++ {
p.AddTask(f)
}
p.Wait()
avg := time.Duration(0)
total := time.Duration(0)
for _, cost := range costs {
total += cost
}
avg = total / time.Duration(len(costs))
fmt.Printf("total requests: %d, avg cost: %s, err count: %d\n", len(costs), avg, errCount)
}
```
2 changes: 1 addition & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewPool(opts ...Option) *Pool {
}

// Start start routine pool in background.
// The initial number of workers is determined by the number of tasks in the channel.
// The initial number of workers is determined by the number of tasks in the channel and maxWorkers.
func (p *Pool) Start() {
p.start(min(len(p.taskChan), p.maxWorkers))
}
Expand Down

0 comments on commit 8ecb12e

Please sign in to comment.