internal/fetch: benchmark decode times with AST removal

Compare the time to decode ast.Files with and without
removing parts of the AST.

Change-Id: I79f36a18248c769a323b5b39a348ce22406eeb64
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/257797
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/fetch/sourcefiles_test.go b/internal/fetch/sourcefiles_test.go
index ab4d996..382d068 100644
--- a/internal/fetch/sourcefiles_test.go
+++ b/internal/fetch/sourcefiles_test.go
@@ -14,17 +14,10 @@
 
 func TestEncodeDecodeASTFiles(t *testing.T) {
 	// Verify that we can encode and decode the Go files in this directory.
-	fset := token.NewFileSet()
-	pkgs, err := parser.ParseDir(fset, ".", nil, parser.ParseComments)
+	files, fset, err := astFilesForDir(".")
 	if err != nil {
 		t.Fatal(err)
 	}
-	var files []*ast.File
-	for _, p := range pkgs {
-		for _, f := range p.Files {
-			files = append(files, f)
-		}
-	}
 
 	data, err := EncodeASTFiles(fset, files)
 	if err != nil {
@@ -80,3 +73,54 @@
 	}
 	compareObjs(files[0])
 }
+
+// Compare the time to decode AST files with and without
+// removing parts of the AST not relevant to documentation.
+//
+// Run on a cloudtop 9/29/2020:
+// - data size is 3x smaller
+// - decode time is 3.5x faster
+func BenchmarkRemovingAST(b *testing.B) {
+	files, fset, err := astFilesForDir(".")
+	if err != nil {
+		b.Fatal(err)
+	}
+
+	run := func(name string) {
+		data, err := EncodeASTFiles(fset, files)
+		if err != nil {
+			b.Fatal(err)
+		}
+		b.Logf("len(data) = %d", len(data))
+		b.Run(name, func(b *testing.B) {
+			for i := 0; i < b.N; i++ {
+				_, _, err := DecodeASTFiles(data)
+				if err != nil {
+					b.Fatal(err)
+				}
+			}
+		})
+	}
+
+	run("not removed")
+
+	for _, f := range files {
+		removeUnusedASTNodes(f)
+	}
+	run("removed")
+}
+
+func astFilesForDir(dir string) ([]*ast.File, *token.FileSet, error) {
+	fset := token.NewFileSet()
+	pkgs, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
+	if err != nil {
+		return nil, nil, err
+	}
+	var files []*ast.File
+	for _, p := range pkgs {
+		for _, f := range p.Files {
+			files = append(files, f)
+		}
+	}
+	return files, fset, nil
+}