ProfileFunc profiles a function execution
(ctx context.Context, operation string, fn func() error)
| 71 | |
| 72 | // ProfileFunc profiles a function execution |
| 73 | func (p *Profiler) ProfileFunc(ctx context.Context, operation string, fn func() error) (*Profile, error) { |
| 74 | if !p.enabled { |
| 75 | return nil, fn() |
| 76 | } |
| 77 | |
| 78 | profile := &Profile{ |
| 79 | Operation: operation, |
| 80 | Timestamp: time.Now(), |
| 81 | } |
| 82 | |
| 83 | var memBefore runtime.MemStats |
| 84 | runtime.ReadMemStats(&memBefore) |
| 85 | profile.MemoryBefore = memBefore.Alloc |
| 86 | |
| 87 | start := time.Now() |
| 88 | err := fn() |
| 89 | profile.Duration = time.Since(start) |
| 90 | |
| 91 | var memAfter runtime.MemStats |
| 92 | runtime.ReadMemStats(&memAfter) |
| 93 | profile.MemoryAfter = memAfter.Alloc |
| 94 | profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) |
| 95 | |
| 96 | if p.logger != nil { |
| 97 | p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 98 | } |
| 99 | |
| 100 | return profile, err |
| 101 | } |
| 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) { |
no test coverage detected