stress example use pkg/profile
Vladimir Bauer
3 years ago
| 1 | 1 |
|
| 2 | 2 |
go 1.17
|
| 3 | 3 |
|
| 4 | |
require github.com/vbauerster/mpb/v8 v8.1.2
|
|
4 |
require (
|
|
5 |
github.com/pkg/profile v1.6.0
|
|
6 |
github.com/vbauerster/mpb/v8 v8.1.2
|
|
7 |
)
|
| 5 | 8 |
|
| 6 | 9 |
require (
|
| 7 | 10 |
github.com/VividCortex/ewma v1.2.0 // indirect
|
| 2 | 2 |
import (
|
| 3 | 3 |
"flag"
|
| 4 | 4 |
"fmt"
|
| 5 | |
"log"
|
| 6 | 5 |
"math/rand"
|
| 7 | 6 |
"os"
|
| 8 | |
"runtime/pprof"
|
| 9 | 7 |
"sync"
|
| 10 | 8 |
"time"
|
| 11 | 9 |
|
|
10 |
"github.com/pkg/profile"
|
| 12 | 11 |
"github.com/vbauerster/mpb/v8"
|
| 13 | 12 |
"github.com/vbauerster/mpb/v8/decor"
|
| 14 | 13 |
)
|
|
| 17 | 16 |
totalBars = 32
|
| 18 | 17 |
)
|
| 19 | 18 |
|
| 20 | |
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
19 |
var proftype = flag.String("prof", "", "profile type (cpu, mem)")
|
| 21 | 20 |
|
| 22 | 21 |
func main() {
|
| 23 | 22 |
flag.Parse()
|
| 24 | |
if *cpuprofile != "" {
|
| 25 | |
f, err := os.Create(*cpuprofile)
|
| 26 | |
if err != nil {
|
| 27 | |
log.Fatal(err)
|
| 28 | |
}
|
| 29 | |
pprof.StartCPUProfile(f)
|
| 30 | |
defer pprof.StopCPUProfile()
|
|
23 |
switch *proftype {
|
|
24 |
case "cpu":
|
|
25 |
defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
|
|
26 |
case "mem":
|
|
27 |
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
|
| 31 | 28 |
}
|
| 32 | 29 |
var wg sync.WaitGroup
|
| 33 | 30 |
// passed wg will be accounted at p.Wait() call
|