|
| 1 | +package profiler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "strconv" |
| 9 | + "time" |
| 10 | + |
| 11 | + "log/slog" |
| 12 | +) |
| 13 | + |
| 14 | +// Profile represents performance metrics for an operation |
| 15 | +type Profile struct { |
| 16 | + Operation string `json:"operation"` |
| 17 | + Duration time.Duration `json:"duration_ns"` |
| 18 | + MemoryBefore uint64 `json:"memory_before_bytes"` |
| 19 | + MemoryAfter uint64 `json:"memory_after_bytes"` |
| 20 | + MemoryDelta int64 `json:"memory_delta_bytes"` |
| 21 | + LinesCount int `json:"lines_count,omitempty"` |
| 22 | + BytesCount int64 `json:"bytes_count,omitempty"` |
| 23 | + Timestamp time.Time `json:"timestamp"` |
| 24 | +} |
| 25 | + |
| 26 | +// String returns a human-readable representation of the profile |
| 27 | +func (p *Profile) String() string { |
| 28 | + return fmt.Sprintf("[%s] %s: duration=%v, memory_delta=%+dB, lines=%d, bytes=%d", |
| 29 | + p.Timestamp.Format("15:04:05.000"), |
| 30 | + p.Operation, |
| 31 | + p.Duration, |
| 32 | + p.MemoryDelta, |
| 33 | + p.LinesCount, |
| 34 | + p.BytesCount, |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +// Profiler provides minimal performance profiling capabilities |
| 39 | +type Profiler struct { |
| 40 | + logger *slog.Logger |
| 41 | + enabled bool |
| 42 | +} |
| 43 | + |
| 44 | +// New creates a new Profiler instance |
| 45 | +func New(logger *slog.Logger, enabled bool) *Profiler { |
| 46 | + return &Profiler{ |
| 47 | + logger: logger, |
| 48 | + enabled: enabled, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// ProfileFunc profiles a function execution |
| 53 | +func (p *Profiler) ProfileFunc(ctx context.Context, operation string, fn func() error) (*Profile, error) { |
| 54 | + if !p.enabled { |
| 55 | + return nil, fn() |
| 56 | + } |
| 57 | + |
| 58 | + profile := &Profile{ |
| 59 | + Operation: operation, |
| 60 | + Timestamp: time.Now(), |
| 61 | + } |
| 62 | + |
| 63 | + var memBefore runtime.MemStats |
| 64 | + runtime.ReadMemStats(&memBefore) |
| 65 | + profile.MemoryBefore = memBefore.Alloc |
| 66 | + |
| 67 | + start := time.Now() |
| 68 | + err := fn() |
| 69 | + profile.Duration = time.Since(start) |
| 70 | + |
| 71 | + var memAfter runtime.MemStats |
| 72 | + runtime.ReadMemStats(&memAfter) |
| 73 | + profile.MemoryAfter = memAfter.Alloc |
| 74 | + profile.MemoryDelta = int64(memAfter.Alloc) - int64(memBefore.Alloc) |
| 75 | + |
| 76 | + if p.logger != nil { |
| 77 | + p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 78 | + } |
| 79 | + |
| 80 | + return profile, err |
| 81 | +} |
| 82 | + |
| 83 | +// ProfileFuncWithMetrics profiles a function execution and captures additional metrics |
| 84 | +func (p *Profiler) ProfileFuncWithMetrics(ctx context.Context, operation string, fn func() (int, int64, error)) (*Profile, error) { |
| 85 | + if !p.enabled { |
| 86 | + _, _, err := fn() |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + profile := &Profile{ |
| 91 | + Operation: operation, |
| 92 | + Timestamp: time.Now(), |
| 93 | + } |
| 94 | + |
| 95 | + var memBefore runtime.MemStats |
| 96 | + runtime.ReadMemStats(&memBefore) |
| 97 | + profile.MemoryBefore = memBefore.Alloc |
| 98 | + |
| 99 | + start := time.Now() |
| 100 | + lines, bytes, err := fn() |
| 101 | + profile.Duration = time.Since(start) |
| 102 | + profile.LinesCount = lines |
| 103 | + profile.BytesCount = bytes |
| 104 | + |
| 105 | + var memAfter runtime.MemStats |
| 106 | + runtime.ReadMemStats(&memAfter) |
| 107 | + profile.MemoryAfter = memAfter.Alloc |
| 108 | + profile.MemoryDelta = int64(memAfter.Alloc) - int64(memBefore.Alloc) |
| 109 | + |
| 110 | + if p.logger != nil { |
| 111 | + p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 112 | + } |
| 113 | + |
| 114 | + return profile, err |
| 115 | +} |
| 116 | + |
| 117 | +// Start begins timing an operation and returns a function to complete the profiling |
| 118 | +func (p *Profiler) Start(ctx context.Context, operation string) func(lines int, bytes int64) *Profile { |
| 119 | + if !p.enabled { |
| 120 | + return func(int, int64) *Profile { return nil } |
| 121 | + } |
| 122 | + |
| 123 | + profile := &Profile{ |
| 124 | + Operation: operation, |
| 125 | + Timestamp: time.Now(), |
| 126 | + } |
| 127 | + |
| 128 | + var memBefore runtime.MemStats |
| 129 | + runtime.ReadMemStats(&memBefore) |
| 130 | + profile.MemoryBefore = memBefore.Alloc |
| 131 | + |
| 132 | + start := time.Now() |
| 133 | + |
| 134 | + return func(lines int, bytes int64) *Profile { |
| 135 | + profile.Duration = time.Since(start) |
| 136 | + profile.LinesCount = lines |
| 137 | + profile.BytesCount = bytes |
| 138 | + |
| 139 | + var memAfter runtime.MemStats |
| 140 | + runtime.ReadMemStats(&memAfter) |
| 141 | + profile.MemoryAfter = memAfter.Alloc |
| 142 | + profile.MemoryDelta = int64(memAfter.Alloc) - int64(memBefore.Alloc) |
| 143 | + |
| 144 | + if p.logger != nil { |
| 145 | + p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 146 | + } |
| 147 | + |
| 148 | + return profile |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +var globalProfiler *Profiler |
| 153 | + |
| 154 | +// IsProfilingEnabled checks if profiling is enabled via environment variables |
| 155 | +func IsProfilingEnabled() bool { |
| 156 | + if enabled, err := strconv.ParseBool(os.Getenv("GITHUB_MCP_PROFILING_ENABLED")); err == nil { |
| 157 | + return enabled |
| 158 | + } |
| 159 | + return false |
| 160 | +} |
| 161 | + |
| 162 | +// Init initializes the global profiler |
| 163 | +func Init(logger *slog.Logger, enabled bool) { |
| 164 | + globalProfiler = New(logger, enabled) |
| 165 | +} |
| 166 | + |
| 167 | +// InitFromEnv initializes the global profiler using environment variables |
| 168 | +func InitFromEnv(logger *slog.Logger) { |
| 169 | + globalProfiler = New(logger, IsProfilingEnabled()) |
| 170 | +} |
| 171 | + |
| 172 | +// ProfileFunc profiles a function using the global profiler |
| 173 | +func ProfileFunc(ctx context.Context, operation string, fn func() error) (*Profile, error) { |
| 174 | + if globalProfiler == nil { |
| 175 | + return nil, fn() |
| 176 | + } |
| 177 | + return globalProfiler.ProfileFunc(ctx, operation, fn) |
| 178 | +} |
| 179 | + |
| 180 | +// ProfileFuncWithMetrics profiles a function with metrics using the global profiler |
| 181 | +func ProfileFuncWithMetrics(ctx context.Context, operation string, fn func() (int, int64, error)) (*Profile, error) { |
| 182 | + if globalProfiler == nil { |
| 183 | + _, _, err := fn() |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + return globalProfiler.ProfileFuncWithMetrics(ctx, operation, fn) |
| 187 | +} |
| 188 | + |
| 189 | +// Start begins timing using the global profiler |
| 190 | +func Start(ctx context.Context, operation string) func(int, int64) *Profile { |
| 191 | + if globalProfiler == nil { |
| 192 | + return func(int, int64) *Profile { return nil } |
| 193 | + } |
| 194 | + return globalProfiler.Start(ctx, operation) |
| 195 | +} |
0 commit comments