-
Notifications
You must be signed in to change notification settings - Fork 1
Implement baseline benchmarks for Task T001 (Phase 3.1) #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Copilot
wants to merge
2
commits into
001-baseline-specification-for
from
copilot/fix-e69269cb-a634-4daa-aafb-72dd30cfeb55
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| //go:build planned | ||
|
|
||
| // Package benchmark provides baseline benchmarks for the modular framework. | ||
| // This file implements Task T001 from specs/001-baseline-specification-for/tasks.md | ||
| // | ||
| // Purpose: Establish baseline performance metrics prior to dynamic reload & health aggregator changes. | ||
| // These benchmarks measure core framework operations: application bootstrap, service lookup, | ||
| // and configuration loading to track performance regressions during feature development. | ||
| package benchmark | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/GoCodeAlone/modular" | ||
| ) | ||
|
|
||
| // benchLogger provides a no-op logger for benchmarking | ||
| type benchLogger struct{} | ||
|
|
||
| func (l *benchLogger) Debug(msg string, args ...interface{}) {} | ||
| func (l *benchLogger) Info(msg string, args ...interface{}) {} | ||
| func (l *benchLogger) Warn(msg string, args ...interface{}) {} | ||
| func (l *benchLogger) Error(msg string, args ...interface{}) {} | ||
| func (l *benchLogger) With(args ...interface{}) modular.Logger { | ||
| return l | ||
| } | ||
|
|
||
| // mockModule provides a minimal implementation for benchmarking | ||
| type mockModule struct { | ||
| name string | ||
| } | ||
|
|
||
| func (m *mockModule) Name() string { | ||
| return m.name | ||
| } | ||
|
|
||
| func (m *mockModule) Init(app modular.Application) error { | ||
| // Register a simple service for this module | ||
| app.SvcRegistry()[m.name+"-service"] = &simpleService{name: m.name} | ||
| return nil | ||
| } | ||
|
|
||
| // simpleService is a basic service type for benchmarking | ||
| type simpleService struct { | ||
| name string | ||
| } | ||
|
|
||
| func (s *simpleService) GetName() string { | ||
| return s.name | ||
| } | ||
|
|
||
| // mockConfigModule extends mockModule with configuration capabilities | ||
| type mockConfigModule struct { | ||
| mockModule | ||
| } | ||
|
|
||
| func (m *mockConfigModule) RegisterConfig(app modular.Application) error { | ||
| // Register a simple configuration section | ||
| cfg := map[string]interface{}{ | ||
| "enabled": true, | ||
| "timeout": 30, | ||
| "name": m.name, | ||
| } | ||
| app.RegisterConfigSection(m.name+"-config", modular.NewStdConfigProvider(cfg)) | ||
| return nil | ||
| } | ||
|
|
||
| // testConfig represents a simple configuration structure for benchmarking | ||
| type testConfig struct { | ||
| Database struct { | ||
| Host string `yaml:"host" default:"localhost"` | ||
| Port int `yaml:"port" default:"5432"` | ||
| Username string `yaml:"username" required:"true"` | ||
| Password string `yaml:"password" required:"true"` | ||
| } `yaml:"database"` | ||
| Server struct { | ||
| Port int `yaml:"port" default:"8080"` | ||
| Enabled bool `yaml:"enabled" default:"true"` | ||
| } `yaml:"server"` | ||
| Features map[string]bool `yaml:"features"` | ||
| } | ||
|
|
||
| // BenchmarkApplicationBootstrap measures application construction and startup time | ||
| // with a couple of lightweight mock modules. This benchmark focuses on the Build+Start | ||
| // phase performance, which is critical for application startup time. | ||
| func BenchmarkApplicationBootstrap(b *testing.B) { | ||
| b.ReportAllocs() | ||
|
|
||
| // Setup phase - create modules outside of timing | ||
| // Use nil config provider to avoid config loading complexity | ||
| modules := []modular.Module{ | ||
| &mockModule{name: "module1"}, | ||
| &mockModule{name: "module2"}, | ||
| } | ||
|
|
||
| b.ResetTimer() // Start timing after setup | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| // Create new application with a proper logger and no config | ||
| app := modular.NewStdApplication(nil, &benchLogger{}) | ||
|
|
||
| // Register modules | ||
| for _, module := range modules { | ||
| app.RegisterModule(module) | ||
| } | ||
|
|
||
| // Initialize, start, and stop application | ||
| if err := app.Init(); err != nil { | ||
| b.Fatalf("Failed to init application: %v", err) | ||
| } | ||
|
|
||
| if err := app.Start(); err != nil { | ||
| b.Fatalf("Failed to start application: %v", err) | ||
| } | ||
|
|
||
| // Stop application to clean up | ||
| if err := app.Stop(); err != nil { | ||
| b.Logf("Warning: Failed to stop application: %v", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // BenchmarkServiceLookup measures service registry lookup performance. | ||
| // This benchmark registers N dummy services and measures repeated lookups | ||
| // via the registry APIs, testing both interface matching and named service lookups. | ||
| func BenchmarkServiceLookup(b *testing.B) { | ||
| b.ReportAllocs() | ||
|
|
||
| // Setup phase - create application and register services | ||
| const numServices = 50 | ||
| app := modular.NewStdApplication(modular.NewStdConfigProvider(nil), &benchLogger{}) | ||
|
|
||
| // Register N dummy services | ||
| registry := app.SvcRegistry() | ||
| for i := 0; i < numServices; i++ { | ||
| serviceName := "service-" + string(rune('0'+i%10)) + string(rune('0'+(i/10)%10)) | ||
| registry[serviceName] = &simpleService{name: serviceName} | ||
| } | ||
|
|
||
| // Service names to lookup during benchmark | ||
| lookupNames := make([]string, numServices) | ||
| for i := 0; i < numServices; i++ { | ||
| lookupNames[i] = "service-" + string(rune('0'+i%10)) + string(rune('0'+(i/10)%10)) | ||
|
||
| } | ||
|
|
||
| b.ResetTimer() // Start timing after setup | ||
|
|
||
| b.Run("NamedLookup", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| // Lookup each service by name | ||
| for _, name := range lookupNames { | ||
| service, exists := registry[name] | ||
| if !exists { | ||
| b.Fatalf("Service %s not found", name) | ||
| } | ||
| _ = service // Use the service to prevent optimization | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| b.Run("InterfaceLookup", func(b *testing.B) { | ||
| // Enhanced registry for interface-based lookups | ||
| enhancedRegistry := modular.NewEnhancedServiceRegistry() | ||
|
|
||
| // Register services in enhanced registry | ||
| for name, service := range registry { | ||
| if _, err := enhancedRegistry.RegisterService(name, service); err != nil { | ||
| b.Fatalf("Failed to register service %s: %v", name, err) | ||
| } | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| // Lookup services that implement a common interface pattern | ||
| for _, name := range lookupNames { | ||
| service, exists := enhancedRegistry.GetService(name) | ||
| if !exists { | ||
| b.Fatalf("Service %s not found in enhanced registry", name) | ||
| } | ||
| _ = service // Use the service to prevent optimization | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // BenchmarkConfigLoad measures configuration feeding and validation performance. | ||
| // This benchmark tests the config feeders + validation pipeline with a synthetic | ||
| // configuration structure that exercises common configuration patterns. | ||
| func BenchmarkConfigLoad(b *testing.B) { | ||
| b.ReportAllocs() | ||
|
|
||
| // Setup phase - create test configuration | ||
| testCfg := testConfig{ | ||
| Database: struct { | ||
| Host string `yaml:"host" default:"localhost"` | ||
| Port int `yaml:"port" default:"5432"` | ||
| Username string `yaml:"username" required:"true"` | ||
| Password string `yaml:"password" required:"true"` | ||
| }{ | ||
| Host: "benchmark-db", | ||
| Port: 5432, | ||
| Username: "testuser", | ||
| Password: "testpass", | ||
| }, | ||
| Server: struct { | ||
| Port int `yaml:"port" default:"8080"` | ||
| Enabled bool `yaml:"enabled" default:"true"` | ||
| }{ | ||
| Port: 8080, | ||
| Enabled: true, | ||
| }, | ||
| Features: map[string]bool{ | ||
| "feature1": true, | ||
| "feature2": false, | ||
| "feature3": true, | ||
| }, | ||
| } | ||
|
|
||
| b.ResetTimer() // Start timing after setup | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| // Create new config provider and validate | ||
| configProvider := modular.NewStdConfigProvider(testCfg) | ||
|
|
||
| // Test configuration loading and validation | ||
| loadedConfig := configProvider.GetConfig().(testConfig) | ||
|
|
||
| // Verify some basic fields to ensure actual work is done | ||
| if loadedConfig.Database.Host == "" { | ||
| b.Fatal("Configuration not properly loaded") | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The service name generation logic is unnecessarily complex and hard to understand. Consider using
fmt.Sprintf(\"service-%02d\", i)for clearer intent and better readability.