Files
dummy-service-workflow/gen.go
bapung 24ea8bf578
Some checks failed
Build Heavy Service / build-and-package (push) Has been cancelled
add heavy build
2025-12-23 11:00:33 +08:00

85 lines
1.8 KiB
Go

//go:build ignore
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
func main() {
countPtr := flag.Int("count", 2000, "Number of files to generate")
flag.Parse()
count := *countPtr
dir := "dummy"
fmt.Printf("Generating %d files in %s/...\n", count, dir)
if err := os.MkdirAll(dir, 0755); err != nil {
panic(err)
}
// Generate individual heavy files
for i := 0; i < count; i++ {
filename := filepath.Join(dir, fmt.Sprintf("file_%05d.go", i))
content := fmt.Sprintf(`package dummy
import "math"
// Struct%[1]d is a dummy structure to consume memory during compilation
type Struct%[1]d struct {
FieldA int
FieldB string
FieldC float64
FieldD []int
FieldE map[string]string
}
// Function%[1]d performs arbitrary calculations to consume CPU during compilation
func Function%[1]d(input int) float64 {
s := Struct%[1]d{
FieldA: input,
FieldB: "dummy string to take up space in the binary symbol table",
FieldC: float64(input) * 1.5,
FieldD: []int{1, 2, 3, 4, 5},
FieldE: map[string]string{"key": "value"},
}
// Some math operations
res := math.Sqrt(s.FieldC) + float64(s.FieldA)
for _, v := range s.FieldD {
res += float64(v)
}
return res
}
`, i)
if err := os.WriteFile(filename, []byte(content), 0644); err != nil {
panic(err)
}
}
// Generate entrypoint to ensure they are linked and not dead-code eliminated
entrypoint := filepath.Join(dir, "entrypoint.go")
entryContent := `package dummy
import "fmt"
// RunAll calls every generated function to prevent linker dead-code elimination
func RunAll() {
sum := 0.0
`
for i := 0; i < count; i++ {
entryContent += fmt.Sprintf("\tsum += Function%d(%d)\n", i, i)
}
entryContent += ` fmt.Printf("Total calculation: %f\n", sum)
}
`
if err := os.WriteFile(entrypoint, []byte(entryContent), 0644); err != nil {
panic(err)
}
fmt.Println("Done.")
}