cue/testdata/benchmarks: add stats

This is not tested for now, but rather to update once in a while.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I85b7059441f72bb6e9745eacf24fb3348f6f52d1
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/543679
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
diff --git a/cue/testdata/benchmarks/bench_test.go b/cue/testdata/benchmarks/bench_test.go
index 7073b78..b413030 100644
--- a/cue/testdata/benchmarks/bench_test.go
+++ b/cue/testdata/benchmarks/bench_test.go
@@ -20,6 +20,9 @@
 	"testing"
 
 	"cuelang.org/go/cue"
+	"cuelang.org/go/internal/core/eval"
+	"cuelang.org/go/internal/core/runtime"
+	"cuelang.org/go/internal/cuetest"
 	"cuelang.org/go/internal/cuetxtar"
 	"golang.org/x/tools/txtar"
 )
@@ -41,11 +44,44 @@
 			b.Fatal(err)
 		}
 
-		inst := cue.Build(cuetxtar.Load(a, b.TempDir()))[0]
+		inst := cuetxtar.Load(a, b.TempDir())[0]
 		if inst.Err != nil {
 			b.Fatal(inst.Err)
 		}
 
+		r := runtime.New()
+
+		v, err := r.Build(nil, inst)
+		if err != nil {
+			b.Fatal(err)
+		}
+		e := eval.New(r)
+		ctx := e.NewContext(v)
+		v.Finalize(ctx)
+
+		if cuetest.UpdateGoldenFiles {
+			const statsFile = "stats.txt"
+			var stats txtar.File
+			var statsPos int
+			for i, f := range a.Files {
+				if f.Name == statsFile {
+					stats = f
+					statsPos = i
+					break
+				}
+			}
+			if stats.Name == "" {
+				// At stats.txt as the first file.
+				a.Files = append([]txtar.File{{
+					Name: statsFile,
+				}}, a.Files...)
+			}
+
+			a.Files[statsPos].Data = []byte(ctx.Stats().String() + "\n\n")
+
+			ioutil.WriteFile(name, txtar.Format(a), fi.Mode())
+		}
+
 		b.Run(name, func(b *testing.B) {
 			for i := 0; i < b.N; i++ {
 				inst := cue.Build(cuetxtar.Load(a, b.TempDir()))[0]
diff --git a/cue/testdata/benchmarks/chain.txtar b/cue/testdata/benchmarks/chain.txtar
index 28a6d99..539c1e0 100644
--- a/cue/testdata/benchmarks/chain.txtar
+++ b/cue/testdata/benchmarks/chain.txtar
@@ -1,3 +1,15 @@
+// TODO(perf): reduce number of Conjunctions
+-- stats.txt --
+Leaks:  0
+Freed:  1001
+Reused: 999
+Allocs: 2
+Retain: 0
+
+Unifications: 1001
+Conjuncts:    500501
+Disjuncts:    1001
+
 -- in.cue --
 f1: string
 f2: f1
diff --git a/cue/testdata/benchmarks/cycle.txtar b/cue/testdata/benchmarks/cycle.txtar
index 9cccfd9..d45cc62 100644
--- a/cue/testdata/benchmarks/cycle.txtar
+++ b/cue/testdata/benchmarks/cycle.txtar
@@ -1,3 +1,14 @@
+-- stats.txt --
+Leaks:  0
+Freed:  25
+Reused: 15
+Allocs: 10
+Retain: 1
+
+Unifications: 15
+Conjuncts:    30
+Disjuncts:    25
+
 -- in.cue --
 sameValues: SmustBeF: {
 	T: string | F
diff --git a/cue/testdata/benchmarks/deduparc.txtar b/cue/testdata/benchmarks/deduparc.txtar
index bd610cd..6022ca3 100644
--- a/cue/testdata/benchmarks/deduparc.txtar
+++ b/cue/testdata/benchmarks/deduparc.txtar
@@ -1,3 +1,14 @@
+-- stats.txt --
+Leaks:  0
+Freed:  11
+Reused: 6
+Allocs: 5
+Retain: 0
+
+Unifications: 7
+Conjuncts:    29
+Disjuncts:    11
+
 -- in.cue --
 package bench1
 
diff --git a/cue/testdata/benchmarks/dedupelem.txtar b/cue/testdata/benchmarks/dedupelem.txtar
index a2035a2..db52d05 100644
--- a/cue/testdata/benchmarks/dedupelem.txtar
+++ b/cue/testdata/benchmarks/dedupelem.txtar
@@ -1,3 +1,14 @@
+-- stats.txt --
+Leaks:  0
+Freed:  16
+Reused: 10
+Allocs: 6
+Retain: 0
+
+Unifications: 10
+Conjuncts:    94
+Disjuncts:    16
+
 -- in.cue --
 package lpcorpus
 
diff --git a/cue/testdata/benchmarks/disjunction.txtar b/cue/testdata/benchmarks/disjunction.txtar
index 7ce9ca6..a298167 100644
--- a/cue/testdata/benchmarks/disjunction.txtar
+++ b/cue/testdata/benchmarks/disjunction.txtar
@@ -1,5 +1,15 @@
 // Triggering recomputation of disjunctions on each branch results
 // in exponential run time. Ensure this does not happen.
+-- stats.txt --
+Leaks:  0
+Freed:  82
+Reused: 74
+Allocs: 8
+Retain: 0
+
+Unifications: 4
+Conjuncts:    143
+Disjuncts:    82
 
 -- in.cue --
 x: a0: {}
diff --git a/cue/testdata/benchmarks/listdedup.txtar b/cue/testdata/benchmarks/listdedup.txtar
index 9f4c5b1..b89f29e 100644
--- a/cue/testdata/benchmarks/listdedup.txtar
+++ b/cue/testdata/benchmarks/listdedup.txtar
@@ -1,6 +1,18 @@
 #Issue: 758
 #Issue: 1044
 
+// TODO(perf): reduce number of conjunctions and disjunctions.
+-- stats.txt --
+Leaks:  0
+Freed:  24096
+Reused: 24051
+Allocs: 45
+Retain: 1
+
+Unifications: 18724
+Conjuncts:    100730
+Disjuncts:    24096
+
 -- in.cue --
 A: #Task
 B: #steps: #Script & {mount: [A]}
diff --git a/cue/testdata/benchmarks/mergeddisjunction.txtar b/cue/testdata/benchmarks/mergeddisjunction.txtar
index 73dab31..0eb54b0 100644
--- a/cue/testdata/benchmarks/mergeddisjunction.txtar
+++ b/cue/testdata/benchmarks/mergeddisjunction.txtar
@@ -9,6 +9,17 @@
 // disjunct.
 //
 // Issue #651
+-- stats.txt --
+Leaks:  0
+Freed:  163
+Reused: 157
+Allocs: 6
+Retain: 0
+
+Unifications: 99
+Conjuncts:    290
+Disjuncts:    163
+
 -- in.cue --
 list: [
 	0, 1, 2, 3, 4, 5, 6, 7, 8,