Skip to content

Instantly share code, notes, and snippets.

@tkcranny
Last active November 27, 2017 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkcranny/3ed2b12b5e7beb762bead801b1a3b9f3 to your computer and use it in GitHub Desktop.
Save tkcranny/3ed2b12b5e7beb762bead801b1a3b9f3 to your computer and use it in GitHub Desktop.
Simple script to run a shell command multiple times very quickly and concurrently, and report on the failure rate.
#!/usr/bin/env python
"""
Simple script to run a shell command repeatedly using concurrent
subprocesses. Shows a pretty progress bar, and prints a percentage of
times the command failed.
Took a ~20 minute sequence of `npm run test` to under 2 minutes.
"""
from multiprocessing import Pool
import subprocess as sp
# pip install progressbar2
from progressbar import ProgressBar
NUM_RUNS = 500
NUM_PROCS = 8
COMMAND = 'npm run test'
def command(i):
"""
Runs COMMAND in a subprocess, returning 0 if it was successful, 1 if
it failed. The value of 1 is used to simplify the counting logic.
"""
return min(1, sp.call(COMMAND.split(), stdout=sp.PIPE, stderr=sp.PIPE))
def main():
"""
Entry point of the program, dispatches the calls to subprocesses,
and manages the progress bar.
"""
bar = ProgressBar()
pool = Pool(processes=8)
bad_runs = 0
for res in bar(pool.imap_unordered(test, range(NUM_RUNS))):
bad_runs += res
print(f'Done. {bad_runs/NUM_RUNS:.2%} fail rate ({bad_runs} of {NUM_RUNS} runs)')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment