-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrt-crexec
executable file
·88 lines (67 loc) · 2.15 KB
/
rt-crexec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
#
# Copyright (C) 2016 Rolf Neugebauer <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
"""Concurrent and random execution of commands
This utility starts 'n' threads which randomly execute commands from a
file. The file contains a line per command.
"""
import argparse
import random
import threading
import subprocess
import sys
import time
commands = []
status = 0
class thread(threading.Thread):
def __init__(self, tid, iterations):
threading.Thread.__init__(self)
self.tid = tid
self.iters = iterations
def run(self):
for i in range(self.iters):
if len(commands) > 1:
idx = random.randrange(0, len(commands))
cmd = commands[idx]
else:
cmd = commands[0]
print("[THREAD-%03d] %03d: %s\n" % (self.tid, i, cmd))
ret = subprocess.call(cmd, shell=True)
if not ret == 0:
print("[THREAD-%03d] %03d: FAILED with %d\n" % (self.tid, i, ret))
global status
status = 1
return
print("[THREAD-%03d] %03d: DONE\n" % (self.tid, i))
# yield
time.sleep(0)
p = argparse.ArgumentParser()
p.description = "Concurrent and random execution of commands"
p.add_argument("-c", "--concurrent", default=10, type=int,
help="How many current threads to execute")
p.add_argument("-i", "--iterations", default=10, type=int,
help="How iterations per thread to execute")
p.add_argument('cmdfile', nargs=1,
help="File to execute commands from.")
args = p.parse_args()
# read in the commands
with open(args.cmdfile[0], 'r') as f:
commands = f.readlines()
# create threads
threads = []
for i in range(args.concurrent):
threads.append(thread(i, args.iterations))
# run threads
for t in threads:
t.start()
# wait for them to finish
for t in threads:
t.join()
sys.exit(status)