ProfileFuncWithMetrics profiles a function execution and captures additional metrics
(ctx context.Context, operation string, fn func() (int, int64, error))
| 102 | |
| 103 | // ProfileFuncWithMetrics profiles a function execution and captures additional metrics |
| 104 | func (p *Profiler) ProfileFuncWithMetrics(ctx context.Context, operation string, fn func() (int, int64, error)) (*Profile, error) { |
| 105 | if !p.enabled { |
| 106 | _, _, err := fn() |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | profile := &Profile{ |
| 111 | Operation: operation, |
| 112 | Timestamp: time.Now(), |
| 113 | } |
| 114 | |
| 115 | var memBefore runtime.MemStats |
| 116 | runtime.ReadMemStats(&memBefore) |
| 117 | profile.MemoryBefore = memBefore.Alloc |
| 118 | |
| 119 | start := time.Now() |
| 120 | lines, bytes, err := fn() |
| 121 | profile.Duration = time.Since(start) |
| 122 | profile.LinesCount = lines |
| 123 | profile.BytesCount = bytes |
| 124 | |
| 125 | var memAfter runtime.MemStats |
| 126 | runtime.ReadMemStats(&memAfter) |
| 127 | profile.MemoryAfter = memAfter.Alloc |
| 128 | profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) |
| 129 | |
| 130 | if p.logger != nil { |
| 131 | p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 132 | } |
| 133 | |
| 134 | return profile, err |
| 135 | } |
| 136 | |
| 137 | // Start begins timing an operation and returns a function to complete the profiling |
| 138 | func (p *Profiler) Start(ctx context.Context, operation string) func(lines int, bytes int64) *Profile { |
no test coverage detected