Skip to content

Commit

Permalink
Add a basic benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Dec 20, 2022
1 parent 736c884 commit 36dadbe
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions fsnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -1478,3 +1479,54 @@ func TestWatchList(t *testing.T) {
t.Errorf("\nhave: %s\nwant: %s", have, want)
}
}

func BenchmarkWatch(b *testing.B) {
w, err := NewWatcher()
if err != nil {
b.Fatal(err)
}

tmp := b.TempDir()
file := join(tmp, "file")
err = w.Add(tmp)
if err != nil {
b.Fatal(err)
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
select {
case err, ok := <-w.Errors:
if !ok {
wg.Done()
return
}
b.Fatal(err)
case _, ok := <-w.Events:
if !ok {
wg.Done()
return
}
}
}
}()

b.ResetTimer()
for n := 0; n < b.N; n++ {
fp, err := os.Create(file)
if err != nil {
b.Fatal(err)
}
err = fp.Close()
if err != nil {
b.Fatal(err)
}
}
err = w.Close()
if err != nil {
b.Fatal(err)
}
wg.Wait()
}

0 comments on commit 36dadbe

Please sign in to comment.