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.
8 // The stress utility is intended for catching sporadic failures.
9 // It runs a given process in parallel in a loop and collects any failures.
11 // $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
12 // You can also specify a number of parallel processes with -p flag;
13 // instruct the utility to not kill hanged processes for gdb attach;
14 // or specify the failure output you are looking for (if you want to
15 // ignore some other sporadic failures).
21 exec "golang.org/x/sys/execabs"
32 flagP = flag.Int("p", runtime.NumCPU(), "run `N` processes in parallel")
33 flagTimeout = flag.Duration("timeout", 10*time.Minute, "timeout each process after `duration`")
34 flagKill = flag.Bool("kill", true, "kill timed out processes if true, otherwise just print pid (to attach with gdb)")
35 flagFailure = flag.String("failure", "", "fail only if output matches `regexp`")
36 flagIgnore = flag.String("ignore", "", "ignore failure if output matches `regexp`")
37 flagOutput = flag.String("o", defaultPrefix(), "output failure logs to `path` plus a unique suffix")
42 os.Stderr.WriteString(`The stress utility is intended for catching sporadic failures.
43 It runs a given process in parallel in a loop and collects any failures.
46 $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
53 func defaultPrefix() string {
54 date := time.Now().Format("go-stress-20060102T150405-")
55 return filepath.Join(os.TempDir(), date)
60 if *flagP <= 0 || *flagTimeout <= 0 || len(flag.Args()) == 0 {
64 var failureRe, ignoreRe *regexp.Regexp
65 if *flagFailure != "" {
67 if failureRe, err = regexp.Compile(*flagFailure); err != nil {
68 fmt.Println("bad failure regexp:", err)
72 if *flagIgnore != "" {
74 if ignoreRe, err = regexp.Compile(*flagIgnore); err != nil {
75 fmt.Println("bad ignore regexp:", err)
79 res := make(chan []byte)
80 for i := 0; i < *flagP; i++ {
83 cmd := exec.Command(flag.Args()[0], flag.Args()[1:]...)
84 done := make(chan bool)
90 case <-time.After(*flagTimeout):
93 fmt.Printf("process %v timed out\n", cmd.Process.Pid)
96 cmd.Process.Signal(syscall.SIGABRT)
100 case <-time.After(10 * time.Second):
105 out, err := cmd.CombinedOutput()
107 if err != nil && (failureRe == nil || failureRe.Match(out)) && (ignoreRe == nil || !ignoreRe.Match(out)) {
108 out = append(out, fmt.Sprintf("\n\nERROR: %v\n", err)...)
118 ticker := time.NewTicker(5 * time.Second).C
127 dir, path := filepath.Split(*flagOutput)
128 f, err := ioutil.TempFile(dir, path)
130 fmt.Printf("failed to create temp file: %v\n", err)
135 if len(out) > 2<<10 {
137 fmt.Printf("\n%s\n%s\n…\n", f.Name(), out)
139 fmt.Printf("\n%s\n%s\n", f.Name(), out)
142 elapsed := time.Since(start).Truncate(time.Second)
145 pct = fmt.Sprintf(" (%0.2f%%)", 100.0*float64(fails)/float64(runs))
147 fmt.Printf("%v: %v runs so far, %v failures%s\n", elapsed, runs, fails, pct)