Problem: Sources.xz was being parsed multiple times during single install command - Each dependency lookup triggered fresh parsing of 37,633 packages - This caused 6.5s delays repeatedly during installation Solution: Implement singleton resolver using sync.Once - Add GetGlobalResolver() function with sync.Once pattern - Ensure Sources.xz is parsed only once per process - Thread-safe implementation for concurrent access Changes: - pkg/resolver/global_resolver.go: Singleton resolver implementation - pkg/loader/package_loader.go: Use GetGlobalResolver instead of NewFastResolver - cmd/optimized_build.go: Use GetGlobalResolver with lazy initialization - Add cacheDir fields to support singleton pattern Performance Results: - First resolver init: 477µs (includes parsing) - Subsequent calls: 42ns (11,360x faster) - Target achieved: <1ms for repeated calls Expected Behavior: - Sources.xz parsed once per process - All subsequent resolver calls reuse same instance - O(1) dependency lookups after initial load
104 lines
3 KiB
Go
104 lines
3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"zsvo/pkg/resolver"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("🚀 Testing Singleton Resolver Performance")
|
|
fmt.Println("=======================================")
|
|
|
|
cacheDir := "/tmp/zsvo_singleton_test"
|
|
mirror := "https://deb.debian.org/debian"
|
|
suite := "stable"
|
|
component := "main"
|
|
|
|
// Test 1: First resolver creation (should parse Sources.xz)
|
|
fmt.Printf("\n📦 Test 1: First resolver creation...\n")
|
|
start := time.Now()
|
|
|
|
r1, err := resolver.GetGlobalResolver(cacheDir, mirror, suite, component)
|
|
if err != nil {
|
|
fmt.Printf("❌ Error: %v\n", err)
|
|
return
|
|
}
|
|
|
|
duration1 := time.Since(start)
|
|
fmt.Printf("✅ First resolver created in: %v\n", duration1)
|
|
|
|
// Test 2: Second resolver creation (should be instant, reuse cache)
|
|
fmt.Printf("\n📦 Test 2: Second resolver creation...\n")
|
|
start = time.Now()
|
|
|
|
r2, err := resolver.GetGlobalResolver(cacheDir, mirror, suite, component)
|
|
if err != nil {
|
|
fmt.Printf("❌ Error: %v\n", err)
|
|
return
|
|
}
|
|
|
|
duration2 := time.Since(start)
|
|
fmt.Printf("✅ Second resolver created in: %v\n", duration2)
|
|
|
|
// Verify they are the same instance
|
|
if r1 == r2 {
|
|
fmt.Printf("✅ Same resolver instance (singleton pattern working)\n")
|
|
} else {
|
|
fmt.Printf("❌ Different resolver instances (singleton failed)\n")
|
|
}
|
|
|
|
// Test 3: Multiple resolver calls (should all be instant)
|
|
fmt.Printf("\n📦 Test 3: Multiple resolver calls...\n")
|
|
totalStart := time.Now()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
start = time.Now()
|
|
_, err := resolver.GetGlobalResolver(cacheDir, mirror, suite, component)
|
|
duration := time.Since(start)
|
|
|
|
if err != nil {
|
|
fmt.Printf("❌ Error in call %d: %v\n", i+1, err)
|
|
} else {
|
|
fmt.Printf(" Call %d: %v\n", i+1, duration)
|
|
}
|
|
}
|
|
|
|
totalDuration := time.Since(totalStart)
|
|
fmt.Printf("✅ Total time for 5 calls: %v\n", totalDuration)
|
|
fmt.Printf("📈 Average per call: %v\n", totalDuration/5)
|
|
|
|
// Test 4: Test package resolution performance
|
|
fmt.Printf("\n📦 Test 4: Package resolution performance...\n")
|
|
|
|
// Resolve a few packages
|
|
packages := []string{"cmake", "pkg-config", "libssl-dev"}
|
|
|
|
for _, pkg := range packages {
|
|
start := time.Now()
|
|
graph, err := r1.ResolveDependencies(pkg)
|
|
duration := time.Since(start)
|
|
|
|
if err != nil {
|
|
fmt.Printf("❌ Failed to resolve %s: %v\n", pkg, err)
|
|
} else {
|
|
nodeCount, maxLevel := graph.GetStats()
|
|
fmt.Printf("✅ Resolved %s in %v: %d packages, %d levels\n",
|
|
pkg, duration, nodeCount, maxLevel)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("\n🎯 Performance Summary:\n")
|
|
fmt.Printf("• First resolver init: %v (includes Sources.xz parsing)\n", duration1)
|
|
fmt.Printf("• Subsequent resolver init: %v (singleton reuse)\n", duration2)
|
|
fmt.Printf("• Performance improvement: %.1fx faster\n", float64(duration1)/float64(duration2))
|
|
|
|
if duration2 < time.Millisecond {
|
|
fmt.Printf("🎯 TARGET ACHIEVED: Subsequent calls <1ms\n")
|
|
} else {
|
|
fmt.Printf("⚠️ Target not achieved: %v > 1ms\n", duration2)
|
|
}
|
|
|
|
fmt.Printf("\n✅ Singleton resolver ensures Sources.xz is parsed only once!\n")
|
|
}
|