Profiling Utilities Example
This example demonstrates how to use the profiling utilities from the util/profiling package in the Go-LLMs project.
Features
- CPU and memory profiling of operations
- Component-specific profiling
- Profiling output to file
- Environment variable control of profiling
Running the Example
# Build the example
make build-example EXAMPLE=utils-profiling
# Run the example
./bin/utils-profiling
How It Works
The example demonstrates:
- Enabling profiling via environment variables
- Setting custom profile output directories
- Profiling specific operations with the global profiler
- Component-specific profiling with separate profilers
- Writing CPU and memory profiles to files
Using Profiling in Your Code
To add profiling to your components:
import "github.com/lexlapax/go-llms/pkg/util/profiling"
// Profile a structured operation
result, _ := profiling.ProfileStructuredOp(ctx, profiling.OpStructuredExtraction, func(ctx context.Context) (interface{}, error) {
// Your operation code here
return extractedData, nil
})
// Profile a schema operation
result, _ := profiling.ProfileSchemaOp(ctx, profiling.OpSchemaValidation, func(ctx context.Context) (interface{}, error) {
// Your validation code here
return validationResult, nil
})
// Profile a pool operation
result, _ := profiling.ProfilePoolOp(ctx, profiling.OpPoolAllocation, func(ctx context.Context) (interface{}, error) {
// Your pool allocation code here
return allocatedObject, nil
})
Analyzing Profiles
After running code with profiling enabled, you can analyze the profile files using:
go tool pprof [profile_file]
This opens an interactive pprof session where you can visualize and analyze the profile data.