Skip to content

Commit 3fb8be1

Browse files
committed
use an UUID for the loop variable
1 parent b966eb4 commit 3fb8be1

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/clipperhouse/stringish v0.1.1 // indirect
1616
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
1717
github.com/fatih/color v1.18.0 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
1819
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1920
github.com/mattn/go-colorable v0.1.14 // indirect
2021
github.com/mattn/go-isatty v0.0.20 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/clipperhouse/uax29/v2 v2.3.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsV
77
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
88
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
99
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
10+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
11+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1012
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1113
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1214
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=

pkg/generator/generator.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,41 @@ import (
66
"strings"
77
"text/template"
88

9+
"github.com/google/uuid"
910
"github.com/ipavlic/apex-benchmark-cli/pkg/types"
1011
)
1112

13+
// templateData extends CodeSpec with additional template variables
14+
type templateData struct {
15+
types.CodeSpec
16+
LoopVar string
17+
}
18+
1219
// Generate creates Apex code from a CodeSpec using the template
1320
func Generate(spec types.CodeSpec) (string, error) {
1421
// Validate input
1522
if err := validateSpec(spec); err != nil {
1623
return "", err
1724
}
1825

26+
// Generate unique loop variable name to avoid conflicts with user code
27+
loopVar := "i_" + strings.ReplaceAll(uuid.New().String(), "-", "_")
28+
1929
// Parse template
2030
tmpl, err := template.New("apex").Parse(apexTemplate)
2131
if err != nil {
2232
return "", fmt.Errorf("failed to parse template: %w", err)
2333
}
2434

35+
// Prepare template data
36+
data := templateData{
37+
CodeSpec: spec,
38+
LoopVar: loopVar,
39+
}
40+
2541
// Execute template
2642
var buf bytes.Buffer
27-
if err := tmpl.Execute(&buf, spec); err != nil {
43+
if err := tmpl.Execute(&buf, data); err != nil {
2844
return "", fmt.Errorf("failed to execute template: %w", err)
2945
}
3046

pkg/generator/generator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestGenerate_BasicCode(t *testing.T) {
2929
"Integer warmupIterations = 10;",
3030
"Integer measurementIterations = 100;",
3131
"BENCH_RESULT:",
32-
"for (Integer i = 0; i < warmupIterations; i++)",
33-
"for (Integer i = 0; i < measurementIterations; i++)",
32+
"< warmupIterations;", // Loop uses UUID-based variable
33+
"< measurementIterations;", // Loop uses UUID-based variable
3434
"Long wallStart = System.now().getTime();",
3535
"Integer cpuStart = Limits.getCpuTime();",
3636
}

pkg/generator/templates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Integer warmupIterations = {{.Warmup}};
1414
Integer measurementIterations = {{.Iterations}};
1515
1616
// Warmup phase - JIT optimization
17-
for (Integer i = 0; i < warmupIterations; i++) {
17+
for (Integer {{.LoopVar}} = 0; {{.LoopVar}} < warmupIterations; {{.LoopVar}}++) {
1818
{{.UserCode}}
1919
}
2020
@@ -37,7 +37,7 @@ Integer dmlStatementsBefore = Limits.getDmlStatements();
3737
Integer soqlQueriesBefore = Limits.getQueries();
3838
{{end}}
3939
40-
for (Integer i = 0; i < measurementIterations; i++) {
40+
for (Integer {{.LoopVar}} = 0; {{.LoopVar}} < measurementIterations; {{.LoopVar}}++) {
4141
{{if .TrackHeap}}
4242
Long heapBefore = Limits.getHeapSize();
4343
{{end}}

0 commit comments

Comments
 (0)