You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.3 KiB

  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. // +build !plan9
  5. // The stress utility is intended for catching sporadic failures.
  6. // It runs a given process in parallel in a loop and collects any failures.
  7. // Usage:
  8. // $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
  9. // You can also specify a number of parallel processes with -p flag;
  10. // instruct the utility to not kill hanged processes for gdb attach;
  11. // or specify the failure output you are looking for (if you want to
  12. // ignore some other sporadic failures).
  13. package main
  14. import (
  15. "flag"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "os/exec"
  20. "regexp"
  21. "runtime"
  22. "syscall"
  23. "time"
  24. )
  25. var (
  26. flagP = flag.Int("p", runtime.NumCPU(), "run `N` processes in parallel")
  27. flagTimeout = flag.Duration("timeout", 10*time.Minute, "timeout each process after `duration`")
  28. flagKill = flag.Bool("kill", true, "kill timed out processes if true, otherwise just print pid (to attach with gdb)")
  29. flagFailure = flag.String("failure", "", "fail only if output matches `regexp`")
  30. flagIgnore = flag.String("ignore", "", "ignore failure if output matches `regexp`")
  31. )
  32. func init() {
  33. flag.Usage = func() {
  34. os.Stderr.WriteString(`The stress utility is intended for catching sporadic failures.
  35. It runs a given process in parallel in a loop and collects any failures.
  36. Usage:
  37. $ stress ./fmt.test -test.run=TestSometing -test.cpu=10
  38. `)
  39. flag.PrintDefaults()
  40. }
  41. }
  42. func main() {
  43. flag.Parse()
  44. if *flagP <= 0 || *flagTimeout <= 0 || len(flag.Args()) == 0 {
  45. flag.Usage()
  46. os.Exit(1)
  47. }
  48. var failureRe, ignoreRe *regexp.Regexp
  49. if *flagFailure != "" {
  50. var err error
  51. if failureRe, err = regexp.Compile(*flagFailure); err != nil {
  52. fmt.Println("bad failure regexp:", err)
  53. os.Exit(1)
  54. }
  55. }
  56. if *flagIgnore != "" {
  57. var err error
  58. if ignoreRe, err = regexp.Compile(*flagIgnore); err != nil {
  59. fmt.Println("bad ignore regexp:", err)
  60. os.Exit(1)
  61. }
  62. }
  63. res := make(chan []byte)
  64. for i := 0; i < *flagP; i++ {
  65. go func() {
  66. for {
  67. cmd := exec.Command(flag.Args()[0], flag.Args()[1:]...)
  68. done := make(chan bool)
  69. if *flagTimeout > 0 {
  70. go func() {
  71. select {
  72. case <-done:
  73. return
  74. case <-time.After(*flagTimeout):
  75. }
  76. if !*flagKill {
  77. fmt.Printf("process %v timed out\n", cmd.Process.Pid)
  78. return
  79. }
  80. cmd.Process.Signal(syscall.SIGABRT)
  81. select {
  82. case <-done:
  83. return
  84. case <-time.After(10 * time.Second):
  85. }
  86. cmd.Process.Kill()
  87. }()
  88. }
  89. out, err := cmd.CombinedOutput()
  90. close(done)
  91. if err != nil && (failureRe == nil || failureRe.Match(out)) && (ignoreRe == nil || !ignoreRe.Match(out)) {
  92. out = append(out, fmt.Sprintf("\n\nERROR: %v\n", err)...)
  93. } else {
  94. out = []byte{}
  95. }
  96. res <- out
  97. }
  98. }()
  99. }
  100. runs, fails := 0, 0
  101. ticker := time.NewTicker(5 * time.Second).C
  102. for {
  103. select {
  104. case out := <-res:
  105. runs++
  106. if len(out) == 0 {
  107. continue
  108. }
  109. fails++
  110. f, err := ioutil.TempFile("", "go-stress")
  111. if err != nil {
  112. fmt.Printf("failed to create temp file: %v\n", err)
  113. os.Exit(1)
  114. }
  115. f.Write(out)
  116. f.Close()
  117. if len(out) > 2<<10 {
  118. out = out[:2<<10]
  119. }
  120. fmt.Printf("\n%s\n%s\n", f.Name(), out)
  121. case <-ticker:
  122. fmt.Printf("%v runs so far, %v failures\n", runs, fails)
  123. }
  124. }
  125. }