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.
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.
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).
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")
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.
45 $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
52 func defaultPrefix() string {
53 date := time.Now().Format("go-stress-20060102T150405-")
54 return filepath.Join(os.TempDir(), date)
59 if *flagP <= 0 || *flagTimeout <= 0 || len(flag.Args()) == 0 {
63 var failureRe, ignoreRe *regexp.Regexp
64 if *flagFailure != "" {
66 if failureRe, err = regexp.Compile(*flagFailure); err != nil {
67 fmt.Println("bad failure regexp:", err)
71 if *flagIgnore != "" {
73 if ignoreRe, err = regexp.Compile(*flagIgnore); err != nil {
74 fmt.Println("bad ignore regexp:", err)
78 res := make(chan []byte)
79 for i := 0; i < *flagP; i++ {
82 cmd := exec.Command(flag.Args()[0], flag.Args()[1:]...)
83 done := make(chan bool)
89 case <-time.After(*flagTimeout):
92 fmt.Printf("process %v timed out\n", cmd.Process.Pid)
95 cmd.Process.Signal(syscall.SIGABRT)
99 case <-time.After(10 * time.Second):
104 out, err := cmd.CombinedOutput()
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)...)
117 ticker := time.NewTicker(5 * time.Second).C
126 dir, path := filepath.Split(*flagOutput)
127 f, err := ioutil.TempFile(dir, path)
129 fmt.Printf("failed to create temp file: %v\n", err)
134 if len(out) > 2<<10 {
136 fmt.Printf("\n%s\n%s\n…\n", f.Name(), out)
138 fmt.Printf("\n%s\n%s\n", f.Name(), out)
141 elapsed := time.Since(start).Truncate(time.Second)
144 pct = fmt.Sprintf(" (%0.2f%%)", 100.0*float64(fails)/float64(runs))
146 fmt.Printf("%v: %v runs so far, %v failures%s\n", elapsed, runs, fails, pct)