panjf2000 / ants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
https://ants.andypan.me/
MIT License
12.83k stars 1.36k forks source link

[Question]: How can I verifier the max goroutine count in pool is correct #277

Closed mingregister closed 1 year ago

mingregister commented 1 year ago

Questions with details

I use the ants pool as the following snappet.

type taskFunc func()

const (
    DataSize    = 10000
    DataPerTask = 100
)

func taskFuncWrapper(nums []int, i int, sum *int, wg *sync.WaitGroup, t *testing.T) taskFunc {
    return func() {
        for _, num := range nums[i*DataPerTask : (i+1)*DataPerTask] {
            *sum += num
        }

        t.Logf("task:%d sum:%d\n", i+1, *sum)
        wg.Done()
    }
}

func TestAntsPool(t *testing.T) {
    p, _ := ants.NewPool(10)
    defer p.Release()

    nums := make([]int, DataSize)
    for i := range nums {
        nums[i] = rand.Intn(1000)
    }

    var wg sync.WaitGroup
    defer wg.Wait()
    wg.Add(DataSize / DataPerTask)
    partSums := make([]int, DataSize/DataPerTask)
    for i := 0; i < DataSize/DataPerTask; i++ {
        p.Submit(taskFuncWrapper(nums, i, &partSums[i], &wg, t))
    }
}

My Question is: How can I verifier the max goroutine count in pool is correct.

What had I try:

  1. I was try to get the goroutineid to vierifier this. But after google this, I found out it is very difficult to get the goroutineid.

Code snippets (optional)

No response

xyhubl commented 1 year ago

you can try it

func GoID() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("cannot get goroutine id: %v", err))
}
return id
}
panjf2000 commented 1 year ago

What did you mean "max goroutine count"?

And like @xyhubl said, you can generate the custom goroutine ID if that's what you want.