120 lines
3.7 KiB
Go
120 lines
3.7 KiB
Go
package engine
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
|
)
|
|
|
|
func BenchmarkComposeWideMappings(b *testing.B) {
|
|
for _, width := range []int{128, 256, 512} {
|
|
b.Run(strconv.Itoa(width), func(b *testing.B) {
|
|
left := benchmarkCatalog("source-1", 0, width)
|
|
right := benchmarkCatalog("source-2", width, width)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
if _, diagnostics := composeDocuments(left, right); len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkOverlayRecursiveMappings(b *testing.B) {
|
|
for _, width := range []int{128, 256, 512} {
|
|
b.Run(strconv.Itoa(width), func(b *testing.B) {
|
|
base, diagnostics := composeDocuments(benchmarkCatalog("source-1", 0, width))
|
|
if len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
overlay := benchmarkCatalog("source-2", width/2, width)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
if _, diagnostics := overlayDocuments(base, overlay); len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkOverlayManyApplications(b *testing.B) {
|
|
for _, count := range []int{32, 64, 128} {
|
|
inputs := make([]*document.Node, count)
|
|
for index := range inputs {
|
|
inputs[index] = benchmarkCatalog("source-2", index, 1)
|
|
}
|
|
benchmarkOverlayApplications(b, count, inputs)
|
|
}
|
|
}
|
|
|
|
func BenchmarkOverlayRepeatedReplacement(b *testing.B) {
|
|
for _, count := range []int{32, 64, 128} {
|
|
inputs := make([]*document.Node, count)
|
|
for index := range inputs {
|
|
location := document.Location{SourceID: "source-2"}
|
|
value := &document.Node{Kind: document.String, Text: "replacement", Location: location}
|
|
if index%2 == 0 {
|
|
value = &document.Node{Kind: document.Mapping, Location: location, Mapping: []document.Entry{{
|
|
Key: "enabled", Value: &document.Node{Kind: document.Boolean, Text: "false", Location: location},
|
|
}}}
|
|
}
|
|
inputs[index] = &document.Node{Kind: document.Mapping, Location: location, Mapping: []document.Entry{{
|
|
Key: "catalog", Value: &document.Node{Kind: document.Mapping, Location: location, Mapping: []document.Entry{{
|
|
Key: "item-0", Value: value,
|
|
}}},
|
|
}}}
|
|
}
|
|
benchmarkOverlayApplications(b, count, inputs)
|
|
}
|
|
}
|
|
|
|
func benchmarkOverlayApplications(b *testing.B, count int, inputs []*document.Node) {
|
|
base, diagnostics := composeDocuments(benchmarkCatalog("source-1", 0, 1))
|
|
if len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
for _, mode := range []string{"owned", "copy-preserving"} {
|
|
b.Run(mode+"/"+strconv.Itoa(count), func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
if mode == "owned" {
|
|
assembly := newAssemblyAccumulator(cloneComposedDocument(base))
|
|
for _, input := range inputs {
|
|
if diagnostics := assembly.applyOverlay(input, nil, nil, LayerOverlay); len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
current := base
|
|
for _, input := range inputs {
|
|
current, diagnostics = overlayDocuments(current, input)
|
|
if len(diagnostics) != 0 {
|
|
b.Fatal(diagnostics)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func benchmarkCatalog(source string, first, width int) *document.Node {
|
|
location := document.Location{SourceID: source}
|
|
entries := make([]document.Entry, width)
|
|
for index := range entries {
|
|
entries[index] = document.Entry{
|
|
Key: "item-" + strconv.Itoa(first+index),
|
|
Value: &document.Node{Kind: document.Mapping, Location: location, Mapping: []document.Entry{{Key: "enabled", Value: &document.Node{Kind: document.Boolean, Text: "true", Location: location}}}},
|
|
}
|
|
}
|
|
return &document.Node{Kind: document.Mapping, Location: location, Mapping: []document.Entry{{
|
|
Key: "catalog", Value: &document.Node{Kind: document.Mapping, Location: location, Mapping: entries},
|
|
}}}
|
|
}
|