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