-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
138 lines (122 loc) · 3.71 KB
/
bench_test.go
File metadata and controls
138 lines (122 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"testing"
"github.com/crossplane/function-sdk-go/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/wompipomp/function-starlark/runtime"
)
// benchScript is a realistic 10-resource Starlark composition that exercises
// the full builtins pipeline: get(), oxr access, conditionals, loops,
// Resource() constructor, set_condition, emit_event, and dxr status update.
const benchScript = `
region = get(oxr, "spec.region", "us-east-1")
env = get(oxr, "spec.environment", "dev")
# Conditional resource: monitoring dashboard in prod only.
if env == "prod":
Resource("monitoring", {
"apiVersion": "monitoring.example.io/v1",
"kind": "Dashboard",
"metadata": {"name": "prod-dashboard"},
"spec": {"enabled": True},
})
# Loop-based resource creation (8 buckets) with conditional replica counts.
for i in range(8):
name = "bucket-%d" % i
Resource(name, {
"apiVersion": "s3.aws.upbound.io/v1beta1",
"kind": "Bucket",
"metadata": {"name": name, "labels": {"env": env, "index": str(i)}},
"spec": {
"forProvider": {
"region": region,
"tags": {"managed-by": "starlark", "env": env},
},
},
})
# Final resource using get() with fallback.
tier = get(oxr, "spec.tier", "standard")
Resource("cache", {
"apiVersion": "cache.example.io/v1",
"kind": "Redis",
"metadata": {"name": "redis-" + tier},
"spec": {"tier": tier, "region": region},
})
# Conditions and events.
set_condition(type = "Ready", status = "True", reason = "Available", message = "All resources created")
emit_event(severity = "Normal", message = "Created 10 resources in " + region)
# DXR status update.
dxr["status"] = {"bucketCount": 8, "region": region, "tier": tier}
`
// BenchmarkRunFunction exercises the full RunFunction pipeline including input
// parsing, globals construction, cached Starlark execution, resource collection,
// condition/event application, and dxr status update. The cache is pre-warmed
// so this measures steady-state (cached) latency.
func BenchmarkRunFunction(b *testing.B) {
f := &Function{
log: logging.NewNopLogger(),
runtime: runtime.NewRuntime(logging.NewNopLogger()),
}
req := &fnv1.RunFunctionRequest{
Input: resource.MustStructJSON(`{
"apiVersion": "starlark.fn.crossplane.io/v1alpha1",
"kind": "StarlarkInput",
"spec": {
"source": "` + escapeJSON(benchScript) + `"
}
}`),
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "example.crossplane.io/v1",
"kind": "XBucket",
"spec": {
"region": "eu-west-1",
"environment": "prod",
"tier": "premium"
},
"status": {}
}`),
},
},
}
ctx := context.Background()
// Pre-warm: populate cache and verify sanity.
rsp, err := f.RunFunction(ctx, req)
if err != nil {
b.Fatalf("pre-warm failed: %v", err)
}
if rsp == nil {
b.Fatal("pre-warm returned nil response")
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
if _, err := f.RunFunction(ctx, req); err != nil {
b.Fatalf("RunFunction failed: %v", err)
}
}
}
// escapeJSON escapes a string for embedding in a JSON string literal.
// Handles newlines, tabs, quotes, and backslashes.
func escapeJSON(s string) string {
var out []byte
for i := 0; i < len(s); i++ {
switch s[i] {
case '"':
out = append(out, '\\', '"')
case '\\':
out = append(out, '\\', '\\')
case '\n':
out = append(out, '\\', 'n')
case '\r':
out = append(out, '\\', 'r')
case '\t':
out = append(out, '\\', 't')
default:
out = append(out, s[i])
}
}
return string(out)
}