BenchmarkFieldAccess benchmarks executing a template that accesses fields in the current context. This measures the overhead from adding one additional field access to a template.
(b *testing.B)
| 1091 | // This measures the overhead from adding one additional field access to a |
| 1092 | // template. |
| 1093 | func BenchmarkFieldAccess(b *testing.B) { |
| 1094 | // This benchmark is disabled from normal execution due to being |
| 1095 | // limited by the parsing performance of this package. When the |
| 1096 | // benchmark len grows greater than about 100K, parsing performance is |
| 1097 | // degraded and this benchmark takes a long time to setup. |
| 1098 | // |
| 1099 | // To test this, comment out the following line and run this specific |
| 1100 | // benchmark with |
| 1101 | // go test -run Bench -bench BenchmarkFieldAcces -benchtime 0.02s |
| 1102 | b.Skip("Can only run manually with -benchtime 0.02s") |
| 1103 | |
| 1104 | const numFields = 3 // Must match the structure below. |
| 1105 | tmplBuilder := new(strings.Builder) |
| 1106 | for i := 0; i < b.N; i++ { |
| 1107 | fmt.Fprintf(tmplBuilder, "{{ .Field%d }} ", i%numFields+1) |
| 1108 | } |
| 1109 | |
| 1110 | loader := NewInMemLoader() |
| 1111 | loader.Set("BenchFields", tmplBuilder.String()) |
| 1112 | set := NewSet(loader, WithSafeWriter(nil)) |
| 1113 | testCtx := struct { |
| 1114 | Field1 string |
| 1115 | Field2 int |
| 1116 | Field3 float64 |
| 1117 | }{ |
| 1118 | Field1: "secret", |
| 1119 | Field2: 42, |
| 1120 | Field3: 3.1415, |
| 1121 | } |
| 1122 | t, _ := set.GetTemplate("BenchFields") |
| 1123 | |
| 1124 | b.ResetTimer() |
| 1125 | err := t.Execute(ww, nil, testCtx) |
| 1126 | if err != nil { |
| 1127 | b.Error(err.Error()) |
| 1128 | } |
| 1129 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…