Skip to content

Commit

Permalink
feat: tpool - add native thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jul 20, 2023
1 parent 20b5c00 commit c5fda09
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
34 changes: 34 additions & 0 deletions util/tpool/thread_pool_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tpool

import (
"runtime"
"sync"
"sync/atomic"
)

func NewNativeThreadPool() ThreadPool {
tp := new(nativeThreadPool)
return tp
}

type nativeThreadPool struct {
mu sync.Mutex
state int32 // 0: running, -1: closed
}

func (tp *nativeThreadPool) Size() (size int) {
return runtime.GOMAXPROCS(0)
}

func (tp *nativeThreadPool) Submit(t task) {
if atomic.LoadInt32(&tp.state) < 0 { // closed
return
}
go func() {
t()
}()
}

func (tp *nativeThreadPool) Close() {
atomic.CompareAndSwapInt32(&tp.state, 0, -1)
}
45 changes: 41 additions & 4 deletions util/tpool/thread_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package tpool

import (
"fmt"
"runtime"
"sync"
"testing"
"time"
)

type benchcase struct {
Expand All @@ -13,8 +15,9 @@ type benchcase struct {

func BenchmarkCPUTasks(b *testing.B) {
cases := []benchcase{
{name: "FixedThreadPool-4Threads", threadPool: NewFixedThreadPool(4)},
{name: "CachedThreadPool-UnlimitedThreads", threadPool: NewCachedThreadPool(WithCachedMaxIdleThreads(32))},
{name: "NewNativeThreadPool", threadPool: NewFixedThreadPool(runtime.NumCPU())},
//{name: "FixedThreadPool", threadPool: NewFixedThreadPool(runtime.NumCPU())},
{name: "CachedThreadPool", threadPool: NewCachedThreadPool(WithCachedMaxIdleThreads(32))},
}
defer func() {
for _, c := range cases {
Expand All @@ -24,8 +27,8 @@ func BenchmarkCPUTasks(b *testing.B) {

for _, c := range cases {
b.Run(fmt.Sprintf("%s", c.name), func(b *testing.B) {
maxCPUTasks := 32
for tasks := 1; tasks <= maxCPUTasks; tasks *= 2 {
maxTasks := 32
for tasks := 1; tasks <= maxTasks; tasks *= 2 {
b.Run(fmt.Sprintf("Tasks[%d]", tasks), func(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
Expand All @@ -47,3 +50,37 @@ func BenchmarkCPUTasks(b *testing.B) {
})
}
}

func BenchmarkIOTasks(b *testing.B) {
cases := []benchcase{
{name: "NativeThreadPool", threadPool: NewFixedThreadPool(runtime.NumCPU())},
//{name: "FixedThreadPool", threadPool: NewFixedThreadPool(runtime.NumCPU())},
{name: "CachedThreadPool", threadPool: NewCachedThreadPool(WithCachedMaxIdleThreads(32))},
}
defer func() {
for _, c := range cases {
c.threadPool.Close()
}
}()

for _, c := range cases {
b.Run(fmt.Sprintf("%s", c.name), func(b *testing.B) {
maxTasks := 32
for tasks := 1; tasks <= maxTasks; tasks *= 2 {
b.Run(fmt.Sprintf("Tasks[%d]", tasks), func(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
for t := 0; t < tasks; t++ {
wg.Add(1)
c.threadPool.Submit(func() {
defer wg.Done()
time.Sleep(time.Millisecond * 10)
})
}
wg.Wait()
}
})
}
})
}
}

0 comments on commit c5fda09

Please sign in to comment.