godoc: add benchmark for directory scan

I'd like to propose changes to the directory scanner implementation,
and it would be good to be able to measure how changes compare in
terms of allocations and time taken.

Change-Id: I4ff4bbd38b5e3522f50d31473f2ac607bb0de802
Reviewed-on: https://go-review.googlesource.com/94904
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/godoc/dirtrees_test.go b/godoc/dirtrees_test.go
index c8e8827..9727206 100644
--- a/godoc/dirtrees_test.go
+++ b/godoc/dirtrees_test.go
@@ -5,6 +5,8 @@
 package godoc
 
 import (
+	"go/build"
+	"path/filepath"
 	"runtime"
 	"sort"
 	"testing"
@@ -38,3 +40,25 @@
 		t.Errorf("list: %v is not sorted\n", list)
 	}
 }
+
+func BenchmarkNewDirectory(b *testing.B) {
+	if testing.Short() {
+		b.Skip("not running tests requiring large file scan in short mode")
+	}
+
+	fsGate := make(chan bool, 20)
+
+	goroot := runtime.GOROOT()
+	rootfs := gatefs.New(vfs.OS(goroot), fsGate)
+	fs := vfs.NameSpace{}
+	fs.Bind("/", rootfs, "/", vfs.BindReplace)
+	for _, p := range filepath.SplitList(build.Default.GOPATH) {
+		fs.Bind("/src/golang.org", gatefs.New(vfs.OS(p), fsGate), "/src/golang.org", vfs.BindAfter)
+	}
+	b.ResetTimer()
+	b.ReportAllocs()
+	for tries := 0; tries < b.N; tries++ {
+		corpus := NewCorpus(fs)
+		corpus.newDirectory("/", -1)
+	}
+}