write_stress_runner.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python2
  2. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. import subprocess
  4. import argparse
  5. import random
  6. import time
  7. import sys
  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 = range(1, 10) + 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 "Going to execute write stress for " + str(runtimes) # noqa: E999 T25377293 Grandfathered in
  22. first_time = True
  23. for runtime in runtimes:
  24. kill = random.choice([False, True])
  25. cmd = './write_stress --runtime_sec=' + \
  26. ("-1" if kill else str(runtime))
  27. if len(args.db) > 0:
  28. cmd = cmd + ' --db=' + args.db
  29. if first_time:
  30. first_time = False
  31. else:
  32. # use current db
  33. cmd = cmd + ' --destroy_db=false'
  34. if random.choice([False, True]):
  35. cmd = cmd + ' --delete_obsolete_files_with_fullscan=true'
  36. if random.choice([False, True]):
  37. cmd = cmd + ' --low_open_files_mode=true'
  38. print("Running write_stress for %d seconds (%s): %s" %
  39. (runtime, ("kill-mode" if kill else "clean-shutdown-mode"),
  40. cmd))
  41. child = subprocess.Popen([cmd], shell=True)
  42. killtime = time.time() + runtime
  43. while not kill or time.time() < killtime:
  44. time.sleep(1)
  45. if child.poll() is not None:
  46. if child.returncode == 0:
  47. break
  48. else:
  49. print("ERROR: write_stress died with exitcode=%d\n"
  50. % child.returncode)
  51. sys.exit(1)
  52. if kill:
  53. child.kill()
  54. # breathe
  55. time.sleep(3)
  56. if __name__ == '__main__':
  57. random.seed(time.time())
  58. parser = argparse.ArgumentParser(description="This script runs and kills \
  59. write_stress multiple times")
  60. parser.add_argument("--runtime_sec", default='1000')
  61. parser.add_argument("--db", default='')
  62. args = parser.parse_args()
  63. main(args)