write_stress_runner.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. import argparse
  4. import random
  5. import subprocess
  6. import sys
  7. import time
  8. def generate_runtimes(total_runtime):
  9. # combination of short runtimes and long runtimes, with heavier
  10. # weight on short runtimes
  11. possible_runtimes_sec = list(range(1, 10)) + list(range(1, 20)) + [100, 1000]
  12. runtimes = []
  13. while total_runtime > 0:
  14. chosen = random.choice(possible_runtimes_sec)
  15. chosen = min(chosen, total_runtime)
  16. runtimes.append(chosen)
  17. total_runtime -= chosen
  18. return runtimes
  19. def main(args):
  20. runtimes = generate_runtimes(int(args.runtime_sec))
  21. print(
  22. "Going to execute write stress for " + str(runtimes)
  23. ) # noqa: E999 T25377293 Grandfathered in
  24. first_time = True
  25. for runtime in runtimes:
  26. kill = random.choice([False, True])
  27. cmd = "./write_stress --runtime_sec=" + ("-1" if kill else str(runtime))
  28. if len(args.db) > 0:
  29. cmd = cmd + " --db=" + args.db
  30. if first_time:
  31. first_time = False
  32. else:
  33. # use current db
  34. cmd = cmd + " --destroy_db=false"
  35. if random.choice([False, True]):
  36. cmd = cmd + " --delete_obsolete_files_with_fullscan=true"
  37. if random.choice([False, True]):
  38. cmd = cmd + " --low_open_files_mode=true"
  39. print(
  40. "Running write_stress for %d seconds (%s): %s"
  41. % (runtime, ("kill-mode" if kill else "clean-shutdown-mode"), cmd)
  42. )
  43. child = subprocess.Popen([cmd], shell=True)
  44. killtime = time.time() + runtime
  45. while not kill or time.time() < killtime:
  46. time.sleep(1)
  47. if child.poll() is not None:
  48. if child.returncode == 0:
  49. break
  50. else:
  51. print(
  52. "ERROR: write_stress died with exitcode=%d\n" % child.returncode
  53. )
  54. sys.exit(1)
  55. if kill:
  56. child.kill()
  57. # breathe
  58. time.sleep(3)
  59. if __name__ == "__main__":
  60. random.seed(time.time())
  61. parser = argparse.ArgumentParser(
  62. description="This script runs and kills \
  63. write_stress multiple times"
  64. )
  65. parser.add_argument("--runtime_sec", default="1000")
  66. parser.add_argument("--db", default="")
  67. args = parser.parse_args()
  68. main(args)