Skip to content

Commit

Permalink
add benchmark test for watch (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Mar 12, 2023
1 parent 539fb63 commit d39a92b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package konf_test

import (
"context"
"sync"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -40,3 +42,34 @@ func BenchmarkGet(b *testing.B) {

require.Equal(b, "v", value)
}

func BenchmarkWatch(b *testing.B) {
watcher := mapWatcher(make(chan map[string]any))
config, err := konf.New(konf.WithLoader(watcher))
require.NoError(b, err)
konf.SetGlobal(config)

cfg := konf.Get[string]("config")
require.Equal(b, "string", cfg)
var waitGroup sync.WaitGroup
waitGroup.Add(b.N)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
err := konf.Watch(ctx, func() {
defer waitGroup.Done()

cfg = konf.Get[string]("config")
})
require.NoError(b, err)
}()
b.ResetTimer()

for i := 0; i < b.N; i++ {
watcher.change(map[string]any{"config": "changed"})
}
waitGroup.Wait()
b.StopTimer()

require.Equal(b, "changed", cfg)
}

0 comments on commit d39a92b

Please sign in to comment.