Start begins timing an operation and returns a function to complete the profiling
(ctx context.Context, operation string)
| 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 { |
| 139 | if !p.enabled { |
| 140 | return func(int, int64) *Profile { return nil } |
| 141 | } |
| 142 | |
| 143 | profile := &Profile{ |
| 144 | Operation: operation, |
| 145 | Timestamp: time.Now(), |
| 146 | } |
| 147 | |
| 148 | var memBefore runtime.MemStats |
| 149 | runtime.ReadMemStats(&memBefore) |
| 150 | profile.MemoryBefore = memBefore.Alloc |
| 151 | |
| 152 | start := time.Now() |
| 153 | |
| 154 | return func(lines int, bytes int64) *Profile { |
| 155 | profile.Duration = time.Since(start) |
| 156 | profile.LinesCount = lines |
| 157 | profile.BytesCount = bytes |
| 158 | |
| 159 | var memAfter runtime.MemStats |
| 160 | runtime.ReadMemStats(&memAfter) |
| 161 | profile.MemoryAfter = memAfter.Alloc |
| 162 | profile.MemoryDelta = safeMemoryDelta(memAfter.Alloc, memBefore.Alloc) |
| 163 | |
| 164 | if p.logger != nil { |
| 165 | p.logger.InfoContext(ctx, "Performance profile", "profile", profile.String()) |
| 166 | } |
| 167 | |
| 168 | return profile |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | var globalProfiler *Profiler |
| 173 |
no test coverage detected