xref: /xnu-8796.141.3/tools/tests/perf_index/test_controller.py (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions#!/usr/bin/python
2*1b191cb5SApple OSS Distributionsfrom __future__ import absolute_import, print_function
3*1b191cb5SApple OSS Distributionsimport socket
4*1b191cb5SApple OSS Distributionsimport time
5*1b191cb5SApple OSS Distributionsimport select
6*1b191cb5SApple OSS Distributionsimport sys
7*1b191cb5SApple OSS Distributions
8*1b191cb5SApple OSS Distributions_CONTROL_PORT = 17694
9*1b191cb5SApple OSS Distributions
10*1b191cb5SApple OSS Distributionsdef waitformsgs(client_sockets, msg):
11*1b191cb5SApple OSS Distributions  client_sockets_set = set(client_sockets)
12*1b191cb5SApple OSS Distributions  while len(client_sockets_set) > 0:
13*1b191cb5SApple OSS Distributions    rl, _, _ = select.select(client_sockets_set, [], [])
14*1b191cb5SApple OSS Distributions    for client_socket in rl:
15*1b191cb5SApple OSS Distributions      sentmsg = client_socket.recv(1024)
16*1b191cb5SApple OSS Distributions      if sentmsg == msg:
17*1b191cb5SApple OSS Distributions        client_sockets_set.remove(client_socket)
18*1b191cb5SApple OSS Distributions
19*1b191cb5SApple OSS Distributionsdef main(num_clients, test_type, num_threads, job_size, args):
20*1b191cb5SApple OSS Distributions  client_sockets = []
21*1b191cb5SApple OSS Distributions  control_socket = socket.socket()
22*1b191cb5SApple OSS Distributions  control_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
23*1b191cb5SApple OSS Distributions  control_socket.bind(("", _CONTROL_PORT))
24*1b191cb5SApple OSS Distributions  control_socket.listen(num_clients)
25*1b191cb5SApple OSS Distributions  while(len(client_sockets)<num_clients):
26*1b191cb5SApple OSS Distributions    client_socket, _ = control_socket.accept()
27*1b191cb5SApple OSS Distributions    msg = "\0".join(["%s\0%d\0%d" % (test_type, num_threads, job_size)] + args) + "\0\0"
28*1b191cb5SApple OSS Distributions    client_socket.send(msg)
29*1b191cb5SApple OSS Distributions    client_sockets.append(client_socket)
30*1b191cb5SApple OSS Distributions
31*1b191cb5SApple OSS Distributions  control_socket.close()
32*1b191cb5SApple OSS Distributions
33*1b191cb5SApple OSS Distributions  waitformsgs(client_sockets, "Ready")
34*1b191cb5SApple OSS Distributions
35*1b191cb5SApple OSS Distributions  start_time = time.time()
36*1b191cb5SApple OSS Distributions
37*1b191cb5SApple OSS Distributions  for client_socket in client_sockets:
38*1b191cb5SApple OSS Distributions    client_socket.shutdown(socket.SHUT_WR)
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions  waitformsgs(client_sockets, "Done")
41*1b191cb5SApple OSS Distributions
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions  for client_socket in client_sockets:
44*1b191cb5SApple OSS Distributions    client_socket.close()
45*1b191cb5SApple OSS Distributions
46*1b191cb5SApple OSS Distributions  end_time = time.time()
47*1b191cb5SApple OSS Distributions  return end_time - start_time
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributionsdef usage():
50*1b191cb5SApple OSS Distributions  sys.stderr.write("usage: start_tests.py num_clients type threads size\n")
51*1b191cb5SApple OSS Distributions  exit(1)
52*1b191cb5SApple OSS Distributions
53*1b191cb5SApple OSS Distributionsif __name__ == "__main__":
54*1b191cb5SApple OSS Distributions  if len(sys.argv) < 5:
55*1b191cb5SApple OSS Distributions    usage()
56*1b191cb5SApple OSS Distributions  try:
57*1b191cb5SApple OSS Distributions    num_clients = int(sys.argv[1])
58*1b191cb5SApple OSS Distributions    test_type = sys.argv[2]
59*1b191cb5SApple OSS Distributions    num_threads = int(sys.argv[3])
60*1b191cb5SApple OSS Distributions    job_size = int(sys.argv[4])
61*1b191cb5SApple OSS Distributions    args = sys.argv[5:]
62*1b191cb5SApple OSS Distributions  except ValueError:
63*1b191cb5SApple OSS Distributions    usage()
64*1b191cb5SApple OSS Distributions
65*1b191cb5SApple OSS Distributions  print(main(num_clients, test_type, num_threads, job_size, args))
66