Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / cmd / stress / stress.go
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build !plan9
6
7 // The stress utility is intended for catching sporadic failures.
8 // It runs a given process in parallel in a loop and collects any failures.
9 // Usage:
10 //      $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
11 // You can also specify a number of parallel processes with -p flag;
12 // instruct the utility to not kill hanged processes for gdb attach;
13 // or specify the failure output you are looking for (if you want to
14 // ignore some other sporadic failures).
15 package main
16
17 import (
18         "flag"
19         "fmt"
20         "io/ioutil"
21         "os"
22         "os/exec"
23         "path/filepath"
24         "regexp"
25         "runtime"
26         "syscall"
27         "time"
28 )
29
30 var (
31         flagP       = flag.Int("p", runtime.NumCPU(), "run `N` processes in parallel")
32         flagTimeout = flag.Duration("timeout", 10*time.Minute, "timeout each process after `duration`")
33         flagKill    = flag.Bool("kill", true, "kill timed out processes if true, otherwise just print pid (to attach with gdb)")
34         flagFailure = flag.String("failure", "", "fail only if output matches `regexp`")
35         flagIgnore  = flag.String("ignore", "", "ignore failure if output matches `regexp`")
36         flagOutput  = flag.String("o", defaultPrefix(), "output failure logs to `path` plus a unique suffix")
37 )
38
39 func init() {
40         flag.Usage = func() {
41                 os.Stderr.WriteString(`The stress utility is intended for catching sporadic failures.
42 It runs a given process in parallel in a loop and collects any failures.
43 Usage:
44
45         $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
46
47 `)
48                 flag.PrintDefaults()
49         }
50 }
51
52 func defaultPrefix() string {
53         date := time.Now().Format("go-stress-20060102T150405-")
54         return filepath.Join(os.TempDir(), date)
55 }
56
57 func main() {
58         flag.Parse()
59         if *flagP <= 0 || *flagTimeout <= 0 || len(flag.Args()) == 0 {
60                 flag.Usage()
61                 os.Exit(1)
62         }
63         var failureRe, ignoreRe *regexp.Regexp
64         if *flagFailure != "" {
65                 var err error
66                 if failureRe, err = regexp.Compile(*flagFailure); err != nil {
67                         fmt.Println("bad failure regexp:", err)
68                         os.Exit(1)
69                 }
70         }
71         if *flagIgnore != "" {
72                 var err error
73                 if ignoreRe, err = regexp.Compile(*flagIgnore); err != nil {
74                         fmt.Println("bad ignore regexp:", err)
75                         os.Exit(1)
76                 }
77         }
78         res := make(chan []byte)
79         for i := 0; i < *flagP; i++ {
80                 go func() {
81                         for {
82                                 cmd := exec.Command(flag.Args()[0], flag.Args()[1:]...)
83                                 done := make(chan bool)
84                                 if *flagTimeout > 0 {
85                                         go func() {
86                                                 select {
87                                                 case <-done:
88                                                         return
89                                                 case <-time.After(*flagTimeout):
90                                                 }
91                                                 if !*flagKill {
92                                                         fmt.Printf("process %v timed out\n", cmd.Process.Pid)
93                                                         return
94                                                 }
95                                                 cmd.Process.Signal(syscall.SIGABRT)
96                                                 select {
97                                                 case <-done:
98                                                         return
99                                                 case <-time.After(10 * time.Second):
100                                                 }
101                                                 cmd.Process.Kill()
102                                         }()
103                                 }
104                                 out, err := cmd.CombinedOutput()
105                                 close(done)
106                                 if err != nil && (failureRe == nil || failureRe.Match(out)) && (ignoreRe == nil || !ignoreRe.Match(out)) {
107                                         out = append(out, fmt.Sprintf("\n\nERROR: %v\n", err)...)
108                                 } else {
109                                         out = []byte{}
110                                 }
111                                 res <- out
112                         }
113                 }()
114         }
115         runs, fails := 0, 0
116         ticker := time.NewTicker(5 * time.Second).C
117         for {
118                 select {
119                 case out := <-res:
120                         runs++
121                         if len(out) == 0 {
122                                 continue
123                         }
124                         fails++
125                         dir, path := filepath.Split(*flagOutput)
126                         f, err := ioutil.TempFile(dir, path)
127                         if err != nil {
128                                 fmt.Printf("failed to create temp file: %v\n", err)
129                                 os.Exit(1)
130                         }
131                         f.Write(out)
132                         f.Close()
133                         if len(out) > 2<<10 {
134                                 out := out[:2<<10]
135                                 fmt.Printf("\n%s\n%s\n…\n", f.Name(), out)
136                         } else {
137                                 fmt.Printf("\n%s\n%s\n", f.Name(), out)
138                         }
139                 case <-ticker:
140                         fmt.Printf("%v runs so far, %v failures\n", runs, fails)
141                 }
142         }
143 }