Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Functional Options to GoPool and support for different lock types
- Changed the type of GoPool's mutex to sync.Locker to support different lock types. - Added a new Option type and a WithLock function to configure the lock used by GoPool. - Updated NewGoPool to accept a variadic slice of Option functions and apply them to the GoPool. - Updated all methods of GoPool that use the mutex to use the new lock. - Added a dependency on github.com/daniel-hutao/spinlock for spinlock implementation. - Updated the test cases to include tests for GoPool with sync.Mutex and spinlock.SpinLock. Signed-off-by: Daniel Hu <tao.hu@merico.dev>
- Loading branch information
1 parent
de737e9
commit f5de19a
Showing
4 changed files
with
99 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/devchat-ai/gopool | ||
|
||
go 1.20 | ||
|
||
require github.com/daniel-hutao/spinlock v0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/daniel-hutao/spinlock v0.1.0 h1:qk6v2L6mJLUmxzq1eJ5xUIlCh4q0wM+26Qy/KfH5c3U= | ||
github.com/daniel-hutao/spinlock v0.1.0/go.mod h1:KkIAx91Qk/GDks3LcKXTwOJ7CS03ApkAHU4X4nrlwto= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,84 @@ | ||
package gopool | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/daniel-hutao/spinlock" | ||
) | ||
|
||
func TestGoPool(t *testing.T) { | ||
pool := NewGoPool(100) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
}) | ||
} | ||
pool.Release() | ||
func TestGoPoolWithMutex(t *testing.T) { | ||
pool := NewGoPool(100, WithLock(new(sync.Mutex))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
}) | ||
} | ||
pool.Release() | ||
} | ||
|
||
func TestGoPoolWithSpinLock(t *testing.T) { | ||
pool := NewGoPool(100, WithLock(new(spinlock.SpinLock))) | ||
for i := 0; i < 1000; i++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
}) | ||
} | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoPoolWithMutex(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(sync.Mutex))) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.StopTimer() | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoPool(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(1e4) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
// pool.Release() | ||
func BenchmarkGoPoolWithSpinLock(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
pool := NewGoPool(5e4, WithLock(new(spinlock.SpinLock))) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
pool.AddTask(func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}) | ||
} | ||
} | ||
wg.Wait() | ||
b.StopTimer() | ||
pool.Release() | ||
} | ||
|
||
func BenchmarkGoroutines(b *testing.B) { | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
|
||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}() | ||
} | ||
} | ||
var wg sync.WaitGroup | ||
var taskNum = int(1e6) | ||
|
||
for i := 0; i < b.N; i++ { | ||
wg.Add(taskNum) | ||
for num := 0; num < taskNum; num++ { | ||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
wg.Done() | ||
}() | ||
} | ||
} | ||
} |