Browse Source

add watch benchmark

Xiang Li 12 years ago
parent
commit
026f93cd83
1 changed files with 40 additions and 1 deletions
  1. 40 1
      store/watcher_test.go

+ 40 - 1
store/watcher_test.go

@@ -1,7 +1,8 @@
 package store
 
 import (
-	"fmt"
+	"math/rand"
+	"strconv"
 	"testing"
 	"time"
 )
@@ -54,3 +55,41 @@ func TestWatch(t *testing.T) {
 		}
 	}
 }
+
+// BenchmarkWatch creates 10K watchers watch at /foo/[paht] each time.
+// Path is randomly chosen with max depth 10.
+// It should take less than 15ms to wake up 10K watchers.
+func BenchmarkWatch(b *testing.B) {
+	s := CreateStore(100)
+
+	key := make([]string, 10000)
+	for i := 0; i < 10000; i++ {
+
+		key[i] = "/foo/"
+		depth := rand.Intn(10)
+
+		for j := 0; j < depth; j++ {
+			key[i] += "/" + strconv.Itoa(rand.Int()%10)
+		}
+	}
+
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		watchers := make([]*Watcher, 10000)
+		for i := 0; i < 10000; i++ {
+			// create a new watcher
+			watchers[i] = NewWatcher()
+			// add to the watchers list
+			s.AddWatcher(key[i], watchers[i], 0)
+		}
+
+		s.watcher.stopWatchers()
+
+		for _, watcher := range watchers {
+			// wait for the notification for any changing
+			<-watcher.C
+		}
+
+		s.watcher = newWatcherHub()
+	}
+}