Skip to content

Commit

Permalink
test: add benchmark (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanguyenkh authored Sep 21, 2024
1 parent d9eff6a commit 3a9478f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,22 @@ func main() {

```

## Benchmark

```sh
go test -bench=. -run=xxx -benchmem
```

The benchmark result on my Macbook M3 Pro:

goos: darwin
goarch: arm64
pkg: github.com/hoanguyenkh/promise4g
cpu: Apple M3 Pro
BenchmarkNewWithPool/default-11 892462 1331 ns/op 352 B/op 8 allocs/op
BenchmarkNewWithPool/conc-11 903475 1289 ns/op 352 B/op 8 allocs/op
BenchmarkNewWithPool/ants-11 951216 1296 ns/op 352 B/op 8 allocs/op


## Referer
1) https://github.com/chebyrash/promise
43 changes: 43 additions & 0 deletions promise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,46 @@ func TestCheckAllConcurrent(t *testing.T) {
require.Equal(t, []string{"one", "two", "three"}, results)
require.Less(t, elapsed, 350*time.Millisecond, "Promises did not run concurrently")
}

func BenchmarkNewWithPool(b *testing.B) {
ctx := context.Background()

tests := []struct {
name string
pool Pool
}{
{
name: "default",
pool: newDefaultPool(),
},
{
name: "conc",
pool: func() Pool {
return FromConcPool(conc.New())
}(),
},
{
name: "ants",
pool: func() Pool {
antsPool, err := ants.NewPool(0)
require.NoError(b, err)
return FromAntsPool(antsPool)
}(),
},
}

for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
p := NewWithPool(ctx, func(resolve func(string), reject func(error)) {
resolve(test.name)
}, test.pool)

val, err := p.Await(ctx)
require.NoError(b, err)
require.NotNil(b, val)
require.Equal(b, test.name, val)
}
})
}
}

0 comments on commit 3a9478f

Please sign in to comment.