benchmarker.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. from setup.linux import setup_util
  2. from benchmark import framework_test
  3. from benchmark.test_types import *
  4. from utils import header
  5. from utils import gather_tests
  6. from utils import gather_frameworks
  7. from utils import verify_database_connections
  8. import os
  9. import shutil
  10. import stat
  11. import json
  12. import subprocess
  13. import traceback
  14. import time
  15. import pprint
  16. import csv
  17. import sys
  18. import logging
  19. import socket
  20. import threading
  21. import textwrap
  22. from pprint import pprint
  23. from multiprocessing import Process
  24. from datetime import datetime
  25. # Cross-platform colored text
  26. from colorama import Fore, Back, Style
  27. # Text-based progress indicators
  28. import progressbar
  29. class Benchmarker:
  30. ##########################################################################################
  31. # Public methods
  32. ##########################################################################################
  33. ############################################################
  34. # Prints all the available tests
  35. ############################################################
  36. def run_list_tests(self):
  37. all_tests = self.__gather_tests
  38. for test in all_tests:
  39. print test.name
  40. self.__finish()
  41. ############################################################
  42. # End run_list_tests
  43. ############################################################
  44. ############################################################
  45. # Prints the metadata for all the available tests
  46. ############################################################
  47. def run_list_test_metadata(self):
  48. all_tests = self.__gather_tests
  49. all_tests_json = json.dumps(map(lambda test: {
  50. "name": test.name,
  51. "approach": test.approach,
  52. "classification": test.classification,
  53. "database": test.database,
  54. "framework": test.framework,
  55. "language": test.language,
  56. "orm": test.orm,
  57. "platform": test.platform,
  58. "webserver": test.webserver,
  59. "os": test.os,
  60. "database_os": test.database_os,
  61. "display_name": test.display_name,
  62. "notes": test.notes,
  63. "versus": test.versus
  64. }, all_tests))
  65. with open(os.path.join(self.full_results_directory(), "test_metadata.json"), "w") as f:
  66. f.write(all_tests_json)
  67. ############################################################
  68. # End run_list_test_metadata
  69. ############################################################
  70. ############################################################
  71. # parse_timestamp
  72. # Re-parses the raw data for a given timestamp
  73. ############################################################
  74. def parse_timestamp(self):
  75. all_tests = self.__gather_tests
  76. for test in all_tests:
  77. test.parse_all()
  78. self.__parse_results(all_tests)
  79. self.__finish()
  80. ############################################################
  81. # End parse_timestamp
  82. ############################################################
  83. ############################################################
  84. # Run the tests:
  85. # This process involves setting up the client/server machines
  86. # with any necessary change. Then going through each test,
  87. # running their setup script, verifying the URLs, and
  88. # running benchmarks against them.
  89. ############################################################
  90. def run(self):
  91. ##########################
  92. # Generate metadata
  93. ##########################
  94. self.run_list_test_metadata()
  95. ##########################
  96. # Get a list of all known
  97. # tests that we can run.
  98. ##########################
  99. all_tests = self.__gather_tests
  100. ##########################
  101. # Setup client/server
  102. ##########################
  103. print header("Preparing Server, Database, and Client ...", top='=', bottom='=')
  104. self.__setup_server()
  105. self.__setup_database()
  106. self.__setup_client()
  107. ## Check if wrk (and wrk-pipeline) is installed and executable, if not, raise an exception
  108. #if not (os.access("/usr/local/bin/wrk", os.X_OK) and os.access("/usr/local/bin/wrk-pipeline", os.X_OK)):
  109. # raise Exception("wrk and/or wrk-pipeline are not properly installed. Not running tests.")
  110. ##########################
  111. # Run tests
  112. ##########################
  113. print header("Running Tests...", top='=', bottom='=')
  114. result = self.__run_tests(all_tests)
  115. ##########################
  116. # Parse results
  117. ##########################
  118. if self.mode == "benchmark":
  119. print header("Parsing Results ...", top='=', bottom='=')
  120. self.__parse_results(all_tests)
  121. self.__finish()
  122. return result
  123. ############################################################
  124. # End run
  125. ############################################################
  126. ############################################################
  127. # database_sftp_string(batch_file)
  128. # generates a fully qualified URL for sftp to database
  129. ############################################################
  130. def database_sftp_string(self, batch_file):
  131. sftp_string = "sftp -oStrictHostKeyChecking=no "
  132. if batch_file != None: sftp_string += " -b " + batch_file + " "
  133. if self.database_identity_file != None:
  134. sftp_string += " -i " + self.database_identity_file + " "
  135. return sftp_string + self.database_user + "@" + self.database_host
  136. ############################################################
  137. # End database_sftp_string
  138. ############################################################
  139. ############################################################
  140. # client_sftp_string(batch_file)
  141. # generates a fully qualified URL for sftp to client
  142. ############################################################
  143. def client_sftp_string(self, batch_file):
  144. sftp_string = "sftp -oStrictHostKeyChecking=no "
  145. if batch_file != None: sftp_string += " -b " + batch_file + " "
  146. if self.client_identity_file != None:
  147. sftp_string += " -i " + self.client_identity_file + " "
  148. return sftp_string + self.client_user + "@" + self.client_host
  149. ############################################################
  150. # End client_sftp_string
  151. ############################################################
  152. ############################################################
  153. # generate_url(url, port)
  154. # generates a fully qualified URL for accessing a test url
  155. ############################################################
  156. def generate_url(self, url, port):
  157. return self.server_host + ":" + str(port) + url
  158. ############################################################
  159. # End generate_url
  160. ############################################################
  161. ############################################################
  162. # get_output_file(test_name, test_type)
  163. # returns the output file name for this test_name and
  164. # test_type timestamp/test_type/test_name/raw
  165. ############################################################
  166. def get_output_file(self, test_name, test_type):
  167. return os.path.join(self.result_directory, self.timestamp, test_name, test_type, "raw")
  168. ############################################################
  169. # End get_output_file
  170. ############################################################
  171. ############################################################
  172. # output_file(test_name, test_type)
  173. # returns the output file for this test_name and test_type
  174. # timestamp/test_type/test_name/raw
  175. ############################################################
  176. def output_file(self, test_name, test_type):
  177. path = self.get_output_file(test_name, test_type)
  178. try:
  179. os.makedirs(os.path.dirname(path))
  180. except OSError:
  181. pass
  182. return path
  183. ############################################################
  184. # End output_file
  185. ############################################################
  186. ############################################################
  187. # get_stats_file(test_name, test_type)
  188. # returns the stats file name for this test_name and
  189. # test_type timestamp/test_type/test_name/raw
  190. ############################################################
  191. def get_stats_file(self, test_name, test_type):
  192. return os.path.join(self.result_directory, self.timestamp, test_name, test_type, "stats")
  193. ############################################################
  194. # End get_stats_file
  195. ############################################################
  196. ############################################################
  197. # stats_file(test_name, test_type)
  198. # returns the stats file for this test_name and test_type
  199. # timestamp/test_type/test_name/raw
  200. ############################################################
  201. def stats_file(self, test_name, test_type):
  202. path = self.get_stats_file(test_name, test_type)
  203. try:
  204. os.makedirs(os.path.dirname(path))
  205. except OSError:
  206. pass
  207. return path
  208. ############################################################
  209. # End stats_file
  210. ############################################################
  211. ############################################################
  212. # full_results_directory
  213. ############################################################
  214. def full_results_directory(self):
  215. path = os.path.join(self.fwroot, self.result_directory, self.timestamp)
  216. try:
  217. os.makedirs(path)
  218. except OSError:
  219. pass
  220. return path
  221. ############################################################
  222. # End full_results_directory
  223. ############################################################
  224. ############################################################
  225. # report_verify_results
  226. # Used by FrameworkTest to add verification details to our results
  227. #
  228. # TODO: Technically this is an IPC violation - we are accessing
  229. # the parent process' memory from the child process
  230. ############################################################
  231. def report_verify_results(self, framework, test, result):
  232. if framework.name not in self.results['verify'].keys():
  233. self.results['verify'][framework.name] = dict()
  234. self.results['verify'][framework.name][test] = result
  235. ############################################################
  236. # report_benchmark_results
  237. # Used by FrameworkTest to add benchmark data to this
  238. #
  239. # TODO: Technically this is an IPC violation - we are accessing
  240. # the parent process' memory from the child process
  241. ############################################################
  242. def report_benchmark_results(self, framework, test, results):
  243. if test not in self.results['rawData'].keys():
  244. self.results['rawData'][test] = dict()
  245. # If results has a size from the parse, then it succeeded.
  246. if results:
  247. self.results['rawData'][test][framework.name] = results
  248. # This may already be set for single-tests
  249. if framework.name not in self.results['succeeded'][test]:
  250. self.results['succeeded'][test].append(framework.name)
  251. else:
  252. # This may already be set for single-tests
  253. if framework.name not in self.results['failed'][test]:
  254. self.results['failed'][test].append(framework.name)
  255. ############################################################
  256. # End report_results
  257. ############################################################
  258. ##########################################################################################
  259. # Private methods
  260. ##########################################################################################
  261. ############################################################
  262. # Gathers all the tests
  263. ############################################################
  264. @property
  265. def __gather_tests(self):
  266. tests = gather_tests(include=self.test,
  267. exclude=self.exclude,
  268. benchmarker=self)
  269. # If the tests have been interrupted somehow, then we want to resume them where we left
  270. # off, rather than starting from the beginning
  271. if os.path.isfile(self.current_benchmark):
  272. with open(self.current_benchmark, 'r') as interrupted_benchmark:
  273. interrupt_bench = interrupted_benchmark.read().strip()
  274. for index, atest in enumerate(tests):
  275. if atest.name == interrupt_bench:
  276. tests = tests[index:]
  277. break
  278. return tests
  279. ############################################################
  280. # End __gather_tests
  281. ############################################################
  282. ############################################################
  283. # Makes any necessary changes to the server that should be
  284. # made before running the tests. This involves setting kernal
  285. # settings to allow for more connections, or more file
  286. # descriptiors
  287. #
  288. # http://redmine.lighttpd.net/projects/weighttp/wiki#Troubleshooting
  289. ############################################################
  290. def __setup_server(self):
  291. try:
  292. if os.name == 'nt':
  293. return True
  294. subprocess.call(['sudo', 'sysctl', '-w', 'net.ipv4.tcp_max_syn_backlog=65535'])
  295. subprocess.call(['sudo', 'sysctl', '-w', 'net.core.somaxconn=65535'])
  296. subprocess.call(['sudo', '-s', 'ulimit', '-n', '65535'])
  297. subprocess.call(['sudo', 'sysctl', 'net.ipv4.tcp_tw_reuse=1'])
  298. subprocess.call(['sudo', 'sysctl', 'net.ipv4.tcp_tw_recycle=1'])
  299. subprocess.call(['sudo', 'sysctl', '-w', 'kernel.shmmax=134217728'])
  300. subprocess.call(['sudo', 'sysctl', '-w', 'kernel.shmall=2097152'])
  301. with open(os.path.join(self.full_results_directory(), 'sysctl.txt'), 'w') as f:
  302. f.write(subprocess.check_output(['sudo','sysctl','-a']))
  303. except subprocess.CalledProcessError:
  304. return False
  305. ############################################################
  306. # End __setup_server
  307. ############################################################
  308. ############################################################
  309. # Clean up any processes that run with root privileges
  310. ############################################################
  311. def __cleanup_leftover_processes_before_test(self):
  312. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True)
  313. p.communicate("""
  314. sudo /etc/init.d/apache2 stop
  315. """)
  316. ############################################################
  317. # Makes any necessary changes to the database machine that
  318. # should be made before running the tests. Is very similar
  319. # to the server setup, but may also include database specific
  320. # changes.
  321. ############################################################
  322. def __setup_database(self):
  323. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True)
  324. p.communicate("""
  325. sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
  326. sudo sysctl -w net.core.somaxconn=65535
  327. sudo sysctl -w kernel.sched_autogroup_enabled=0
  328. sudo -s ulimit -n 65535
  329. sudo sysctl net.ipv4.tcp_tw_reuse=1
  330. sudo sysctl net.ipv4.tcp_tw_recycle=1
  331. sudo sysctl -w kernel.shmmax=2147483648
  332. sudo sysctl -w kernel.shmall=2097152
  333. sudo sysctl -w kernel.sem="250 32000 256 512"
  334. """)
  335. # TODO - print kernel configuration to file
  336. # echo "Printing kernel configuration:" && sudo sysctl -a
  337. # Explanations:
  338. # net.ipv4.tcp_max_syn_backlog, net.core.somaxconn, kernel.sched_autogroup_enabled: http://tweaked.io/guide/kernel/
  339. # ulimit -n: http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/
  340. # net.ipv4.tcp_tw_*: http://www.linuxbrigade.com/reduce-time_wait-socket-connections/
  341. # kernel.shm*: http://seriousbirder.com/blogs/linux-understanding-shmmax-and-shmall-settings/
  342. # For kernel.sem: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/chap-Oracle_9i_and_10g_Tuning_Guide-Setting_Semaphores.html
  343. ############################################################
  344. # End __setup_database
  345. ############################################################
  346. ############################################################
  347. # Makes any necessary changes to the client machine that
  348. # should be made before running the tests. Is very similar
  349. # to the server setup, but may also include client specific
  350. # changes.
  351. ############################################################
  352. def __setup_client(self):
  353. p = subprocess.Popen(self.client_ssh_string, stdin=subprocess.PIPE, shell=True)
  354. p.communicate("""
  355. sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
  356. sudo sysctl -w net.core.somaxconn=65535
  357. sudo -s ulimit -n 65535
  358. sudo sysctl net.ipv4.tcp_tw_reuse=1
  359. sudo sysctl net.ipv4.tcp_tw_recycle=1
  360. sudo sysctl -w kernel.shmmax=2147483648
  361. sudo sysctl -w kernel.shmall=2097152
  362. """)
  363. ############################################################
  364. # End __setup_client
  365. ############################################################
  366. ############################################################
  367. # __run_tests
  368. #
  369. # 2013-10-02 ASB Calls each test passed in tests to
  370. # __run_test in a separate process. Each
  371. # test is given a set amount of time and if
  372. # kills the child process (and subsequently
  373. # all of its child processes). Uses
  374. # multiprocessing module.
  375. ############################################################
  376. def __run_tests(self, tests):
  377. if len(tests) == 0:
  378. return 0
  379. logging.debug("Start __run_tests.")
  380. logging.debug("__name__ = %s",__name__)
  381. error_happened = False
  382. if self.os.lower() == 'windows':
  383. logging.debug("Executing __run_tests on Windows")
  384. for test in tests:
  385. with open(self.current_benchmark, 'w') as benchmark_resume_file:
  386. benchmark_resume_file.write(test.name)
  387. if self.__run_test(test) != 0:
  388. error_happened = True
  389. else:
  390. logging.debug("Executing __run_tests on Linux")
  391. # Setup a nice progressbar and ETA indicator
  392. widgets = [self.mode, ': ', progressbar.Percentage(),
  393. ' ', progressbar.Bar(),
  394. ' Rough ', progressbar.ETA()]
  395. pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(tests)).start()
  396. pbar_test = 0
  397. # These features do not work on Windows
  398. for test in tests:
  399. pbar.update(pbar_test)
  400. pbar_test = pbar_test + 1
  401. if __name__ == 'benchmark.benchmarker':
  402. print header("Running Test: %s" % test.name)
  403. with open(self.current_benchmark, 'w') as benchmark_resume_file:
  404. benchmark_resume_file.write(test.name)
  405. test_process = Process(target=self.__run_test, name="Test Runner (%s)" % test.name, args=(test,))
  406. test_process.start()
  407. test_process.join(self.run_test_timeout_seconds)
  408. self.__load_results() # Load intermediate result from child process
  409. if(test_process.is_alive()):
  410. logging.debug("Child process for {name} is still alive. Terminating.".format(name=test.name))
  411. self.__write_intermediate_results(test.name,"__run_test timeout (="+ str(self.run_test_timeout_seconds) + " seconds)")
  412. test_process.terminate()
  413. test_process.join()
  414. if test_process.exitcode != 0:
  415. error_happened = True
  416. pbar.finish()
  417. if os.path.isfile(self.current_benchmark):
  418. os.remove(self.current_benchmark)
  419. logging.debug("End __run_tests.")
  420. if error_happened:
  421. return 1
  422. return 0
  423. ############################################################
  424. # End __run_tests
  425. ############################################################
  426. ############################################################
  427. # __run_test
  428. # 2013-10-02 ASB Previously __run_tests. This code now only
  429. # processes a single test.
  430. #
  431. # Ensures that the system has all necessary software to run
  432. # the tests. This does not include that software for the individual
  433. # test, but covers software such as curl and weighttp that
  434. # are needed.
  435. ############################################################
  436. def __run_test(self, test):
  437. # Used to capture return values
  438. def exit_with_code(code):
  439. if self.os.lower() == 'windows':
  440. return code
  441. else:
  442. sys.exit(code)
  443. logDir = os.path.join(self.full_results_directory(), test.name.lower())
  444. try:
  445. os.makedirs(logDir)
  446. except Exception:
  447. pass
  448. with open(os.path.join(logDir, 'out.txt'), 'w') as out:
  449. if test.os.lower() != self.os.lower() or test.database_os.lower() != self.database_os.lower():
  450. out.write("OS or Database OS specified in benchmark_config.json does not match the current environment. Skipping.\n")
  451. return exit_with_code(0)
  452. # If the test is in the excludes list, we skip it
  453. if self.exclude != None and test.name in self.exclude:
  454. out.write("Test {name} has been added to the excludes list. Skipping.\n".format(name=test.name))
  455. return exit_with_code(0)
  456. out.write("test.os.lower() = {os} test.database_os.lower() = {dbos}\n".format(os=test.os.lower(),dbos=test.database_os.lower()))
  457. out.write("self.results['frameworks'] != None: {val}\n".format(val=str(self.results['frameworks'] != None)))
  458. out.write("test.name: {name}\n".format(name=str(test.name)))
  459. out.write("self.results['completed']: {completed}\n".format(completed=str(self.results['completed'])))
  460. if self.results['frameworks'] != None and test.name in self.results['completed']:
  461. out.write('Framework {name} found in latest saved data. Skipping.\n'.format(name=str(test.name)))
  462. print 'WARNING: Test {test} exists in the results directory; this must be removed before running a new test.\n'.format(test=str(test.name))
  463. return exit_with_code(1)
  464. out.flush()
  465. out.write(header("Beginning %s" % test.name, top='='))
  466. out.flush()
  467. ##########################
  468. # Start this test
  469. ##########################
  470. out.write(header("Starting %s" % test.name))
  471. out.flush()
  472. try:
  473. if test.requires_database():
  474. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, stdout=out, stderr=out, shell=True)
  475. p.communicate("""
  476. sudo restart mysql
  477. sudo restart mongod
  478. sudo service postgresql restart
  479. sudo service cassandra restart
  480. /opt/elasticsearch/elasticsearch restart
  481. """)
  482. time.sleep(10)
  483. st = verify_database_connections([
  484. ("mysql", self.database_host, 3306),
  485. ("mongodb", self.database_host, 27017),
  486. ("postgresql", self.database_host, 5432),
  487. ("cassandra", self.database_host, 9160),
  488. ("elasticsearch", self.database_host, 9200)
  489. ])
  490. print "database connection test results:\n" + "\n".join(st[1])
  491. self.__cleanup_leftover_processes_before_test();
  492. if self.__is_port_bound(test.port):
  493. # We gave it our all
  494. self.__write_intermediate_results(test.name, "port " + str(test.port) + " is not available before start")
  495. out.write(header("Error: Port %s is not available, cannot start %s" % (test.port, test.name)))
  496. out.flush()
  497. print "Error: Unable to recover port, cannot start test"
  498. return exit_with_code(1)
  499. result, process = test.start(out)
  500. if result != 0:
  501. self.__stop_test(out, process)
  502. time.sleep(5)
  503. out.write( "ERROR: Problem starting {name}\n".format(name=test.name) )
  504. out.flush()
  505. self.__write_intermediate_results(test.name,"<setup.py>#start() returned non-zero")
  506. return exit_with_code(1)
  507. logging.info("Sleeping %s seconds to ensure framework is ready" % self.sleep)
  508. time.sleep(self.sleep)
  509. ##########################
  510. # Verify URLs
  511. ##########################
  512. logging.info("Verifying framework URLs")
  513. passed_verify = test.verify_urls(logDir)
  514. ##########################
  515. # Nuke /tmp
  516. ##########################
  517. try:
  518. subprocess.check_call('sudo rm -rf /tmp/*', shell=True, stderr=out, stdout=out)
  519. except Exception:
  520. out.write(header("Error: Could not empty /tmp"))
  521. ##########################
  522. # Benchmark this test
  523. ##########################
  524. if self.mode == "benchmark":
  525. logging.info("Benchmarking")
  526. out.write(header("Benchmarking %s" % test.name))
  527. out.flush()
  528. test.benchmark(logDir)
  529. ##########################
  530. # Stop this test
  531. ##########################
  532. out.write(header("Stopping %s" % test.name))
  533. out.flush()
  534. self.__stop_test(out, process)
  535. out.flush()
  536. time.sleep(5)
  537. if self.__is_port_bound(test.port):
  538. # This can happen sometimes - let's try again
  539. self.__stop_test(out, process)
  540. out.flush()
  541. time.sleep(5)
  542. if self.__is_port_bound(test.port):
  543. # We gave it our all
  544. self.__write_intermediate_results(test.name, "port " + str(test.port) + " was not released by stop")
  545. out.write(header("Error: Port %s was not released by stop %s" % (test.port, test.name)))
  546. out.flush()
  547. return exit_with_code(1)
  548. out.write(header("Stopped %s" % test.name))
  549. out.flush()
  550. ##########################################################
  551. # Remove contents of /tmp folder
  552. ##########################################################
  553. if self.clear_tmp:
  554. try:
  555. filelist = [ f for f in os.listdir("/tmp") ]
  556. for f in filelist:
  557. try:
  558. os.remove("/tmp/" + f)
  559. except OSError as err:
  560. print "Failed to remove " + str(f) + " from /tmp directory: " + str(err)
  561. except OSError:
  562. print "Failed to remove contents of /tmp directory."
  563. ##########################################################
  564. # Save results thus far into the latest results directory
  565. ##########################################################
  566. out.write(header("Saving results through %s" % test.name))
  567. out.flush()
  568. self.__write_intermediate_results(test.name,time.strftime("%Y%m%d%H%M%S", time.localtime()))
  569. if self.mode == "verify" and not passed_verify:
  570. print "Failed verify!"
  571. return exit_with_code(1)
  572. except (OSError, IOError, subprocess.CalledProcessError) as e:
  573. self.__write_intermediate_results(test.name,"<setup.py> raised an exception")
  574. out.write(header("Subprocess Error %s" % test.name))
  575. traceback.print_exc(file=out)
  576. out.flush()
  577. try:
  578. self.__stop_test(out, process)
  579. except (subprocess.CalledProcessError) as e:
  580. self.__write_intermediate_results(test.name,"<setup.py>#stop() raised an error")
  581. out.write(header("Subprocess Error: Test .stop() raised exception %s" % test.name))
  582. traceback.print_exc(file=out)
  583. out.flush()
  584. out.close()
  585. return exit_with_code(1)
  586. # TODO - subprocess should not catch this exception!
  587. # Parent process should catch it and cleanup/exit
  588. except (KeyboardInterrupt) as e:
  589. self.__stop_test(out, process)
  590. out.write(header("Cleaning up..."))
  591. out.flush()
  592. self.__finish()
  593. sys.exit(1)
  594. out.close()
  595. return exit_with_code(0)
  596. ############################################################
  597. # End __run_tests
  598. ############################################################
  599. ############################################################
  600. # __stop_test(benchmarker)
  601. # Stops all running tests
  602. ############################################################
  603. def __stop_test(self, out, process):
  604. if process is not None and process.poll() is None:
  605. # Stop
  606. pids = self.__find_child_processes(process.pid)
  607. if pids is not None:
  608. stop = ['kill', '-STOP'] + pids
  609. subprocess.call(stop, stderr=out, stdout=out)
  610. pids = self.__find_child_processes(process.pid)
  611. if pids is not None:
  612. term = ['kill', '-TERM'] + pids
  613. subprocess.call(term, stderr=out, stdout=out)
  614. # Okay, if there are any more PIDs, kill them harder
  615. pids = self.__find_child_processes(process.pid)
  616. if pids is not None:
  617. kill = ['kill', '-KILL'] + pids
  618. subprocess.call(kill, stderr=out, stdout=out)
  619. process.terminate()
  620. ############################################################
  621. # End __stop_test
  622. ############################################################
  623. ############################################################
  624. # __find_child_processes
  625. # Recursively finds all child processes for the given PID.
  626. ############################################################
  627. def __find_child_processes(self, pid):
  628. toRet = []
  629. try:
  630. pids = subprocess.check_output(['pgrep','-P',str(pid)]).split()
  631. toRet.extend(pids)
  632. for aPid in pids:
  633. toRet.extend(self.__find_child_processes(aPid))
  634. except:
  635. # pgrep will return a non-zero status code if there are no
  636. # processes who have a PPID of PID.
  637. pass
  638. return toRet
  639. ############################################################
  640. # End __find_child_processes
  641. ############################################################
  642. def is_port_bound(self, port):
  643. return self.__is_port_bound(port)
  644. ############################################################
  645. # __is_port_bound
  646. # Check if the requested port is available. If it
  647. # isn't available, then a previous test probably didn't
  648. # shutdown properly.
  649. ############################################################
  650. def __is_port_bound(self, port):
  651. port = int(port)
  652. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  653. try:
  654. # Try to bind to all IP addresses, this port
  655. s.bind(("", port))
  656. # If we get here, we were able to bind successfully,
  657. # which means the port is free.
  658. except socket.error:
  659. # If we get an exception, it might be because the port is still bound
  660. # which would be bad, or maybe it is a privileged port (<1024) and we
  661. # are not running as root, or maybe the server is gone, but sockets are
  662. # still in TIME_WAIT (SO_REUSEADDR). To determine which scenario, try to
  663. # connect.
  664. try:
  665. s.connect(("127.0.0.1", port))
  666. # If we get here, we were able to connect to something, which means
  667. # that the port is still bound.
  668. return True
  669. except socket.error:
  670. # An exception means that we couldn't connect, so a server probably
  671. # isn't still running on the port.
  672. pass
  673. finally:
  674. s.close()
  675. return False
  676. ############################################################
  677. # End __is_port_bound
  678. ############################################################
  679. ############################################################
  680. # __parse_results
  681. # Ensures that the system has all necessary software to run
  682. # the tests. This does not include that software for the individual
  683. # test, but covers software such as curl and weighttp that
  684. # are needed.
  685. ############################################################
  686. def __parse_results(self, tests):
  687. # Run the method to get the commmit count of each framework.
  688. self.__count_commits()
  689. # Call the method which counts the sloc for each framework
  690. self.__count_sloc()
  691. # Time to create parsed files
  692. # Aggregate JSON file
  693. with open(os.path.join(self.full_results_directory(), "results.json"), "w") as f:
  694. f.write(json.dumps(self.results, indent=2))
  695. ############################################################
  696. # End __parse_results
  697. ############################################################
  698. #############################################################
  699. # __count_sloc
  700. #############################################################
  701. def __count_sloc(self):
  702. frameworks = gather_frameworks(include=self.test,
  703. exclude=self.exclude, benchmarker=self)
  704. jsonResult = {}
  705. for framework, testlist in frameworks.iteritems():
  706. if not os.path.exists(os.path.join(testlist[0].directory, "source_code")):
  707. logging.warn("Cannot count lines of code for %s - no 'source_code' file", framework)
  708. continue
  709. # Unfortunately the source_code files use lines like
  710. # ./cpoll_cppsp/www/fortune_old instead of
  711. # ./www/fortune_old
  712. # so we have to back our working dir up one level
  713. wd = os.path.dirname(testlist[0].directory)
  714. try:
  715. command = "cloc --list-file=%s/source_code --yaml" % testlist[0].directory
  716. if os.path.exists(os.path.join(testlist[0].directory, "cloc_defs.txt")):
  717. command += " --read-lang-def %s" % os.path.join(testlist[0].directory, "cloc_defs.txt")
  718. logging.info("Using custom cloc definitions for %s", framework)
  719. # Find the last instance of the word 'code' in the yaml output. This should
  720. # be the line count for the sum of all listed files or just the line count
  721. # for the last file in the case where there's only one file listed.
  722. command = command + "| grep code | tail -1 | cut -d: -f 2"
  723. logging.debug("Running \"%s\" (cwd=%s)", command, wd)
  724. lineCount = subprocess.check_output(command, cwd=wd, shell=True)
  725. jsonResult[framework] = int(lineCount)
  726. except subprocess.CalledProcessError:
  727. continue
  728. except ValueError as ve:
  729. logging.warn("Unable to get linecount for %s due to error '%s'", framework, ve)
  730. self.results['rawData']['slocCounts'] = jsonResult
  731. ############################################################
  732. # End __count_sloc
  733. ############################################################
  734. ############################################################
  735. # __count_commits
  736. #
  737. ############################################################
  738. def __count_commits(self):
  739. frameworks = gather_frameworks(include=self.test,
  740. exclude=self.exclude, benchmarker=self)
  741. def count_commit(directory, jsonResult):
  742. command = "git rev-list HEAD -- " + directory + " | sort -u | wc -l"
  743. try:
  744. commitCount = subprocess.check_output(command, shell=True)
  745. jsonResult[framework] = int(commitCount)
  746. except subprocess.CalledProcessError:
  747. pass
  748. # Because git can be slow when run in large batches, this
  749. # calls git up to 4 times in parallel. Normal improvement is ~3-4x
  750. # in my trials, or ~100 seconds down to ~25
  751. # This is safe to parallelize as long as each thread only
  752. # accesses one key in the dictionary
  753. threads = []
  754. jsonResult = {}
  755. t1 = datetime.now()
  756. for framework, testlist in frameworks.iteritems():
  757. directory = testlist[0].directory
  758. t = threading.Thread(target=count_commit, args=(directory,jsonResult))
  759. t.start()
  760. threads.append(t)
  761. # Git has internal locks, full parallel will just cause contention
  762. # and slowness, so we rate-limit a bit
  763. if len(threads) >= 4:
  764. threads[0].join()
  765. threads.remove(threads[0])
  766. # Wait for remaining threads
  767. for t in threads:
  768. t.join()
  769. t2 = datetime.now()
  770. # print "Took %s seconds " % (t2 - t1).seconds
  771. self.results['rawData']['commitCounts'] = jsonResult
  772. self.commits = jsonResult
  773. ############################################################
  774. # End __count_commits
  775. ############################################################
  776. ############################################################
  777. # __write_intermediate_results
  778. ############################################################
  779. def __write_intermediate_results(self,test_name,status_message):
  780. try:
  781. self.results["completed"][test_name] = status_message
  782. with open(os.path.join(self.full_results_directory(), 'results.json'), 'w') as f:
  783. f.write(json.dumps(self.results, indent=2))
  784. except (IOError):
  785. logging.error("Error writing results.json")
  786. ############################################################
  787. # End __write_intermediate_results
  788. ############################################################
  789. def __load_results(self):
  790. try:
  791. with open(os.path.join(self.full_results_directory(), 'results.json')) as f:
  792. self.results = json.load(f)
  793. except (ValueError, IOError):
  794. pass
  795. ############################################################
  796. # __finish
  797. ############################################################
  798. def __finish(self):
  799. if not self.list_tests and not self.parse:
  800. tests = self.__gather_tests
  801. # Normally you don't have to use Fore.BLUE before each line, but
  802. # Travis-CI seems to reset color codes on newline (see travis-ci/travis-ci#2692)
  803. # or stream flush, so we have to ensure that the color code is printed repeatedly
  804. prefix = Fore.CYAN
  805. for line in header("Verification Summary", top='=', bottom='').split('\n'):
  806. print prefix + line
  807. for test in tests:
  808. print prefix + "| Test: %s" % test.name
  809. if test.name in self.results['verify'].keys():
  810. for test_type, result in self.results['verify'][test.name].iteritems():
  811. if result.upper() == "PASS":
  812. color = Fore.GREEN
  813. elif result.upper() == "WARN":
  814. color = Fore.YELLOW
  815. else:
  816. color = Fore.RED
  817. print prefix + "| " + test_type.ljust(11) + ' : ' + color + result.upper()
  818. else:
  819. print prefix + "| " + Fore.RED + "NO RESULTS (Did framework launch?)"
  820. print prefix + header('', top='', bottom='=') + Style.RESET_ALL
  821. print "Time to complete: " + str(int(time.time() - self.start_time)) + " seconds"
  822. print "Results are saved in " + os.path.join(self.result_directory, self.timestamp)
  823. ############################################################
  824. # End __finish
  825. ############################################################
  826. ##########################################################################################
  827. # Constructor
  828. ##########################################################################################
  829. ############################################################
  830. # Initialize the benchmarker. The args are the arguments
  831. # parsed via argparser.
  832. ############################################################
  833. def __init__(self, args):
  834. # Map type strings to their objects
  835. types = dict()
  836. types['json'] = JsonTestType()
  837. types['db'] = DBTestType()
  838. types['query'] = QueryTestType()
  839. types['fortune'] = FortuneTestType()
  840. types['update'] = UpdateTestType()
  841. types['plaintext'] = PlaintextTestType()
  842. # Turn type into a map instead of a string
  843. if args['type'] == 'all':
  844. args['types'] = types
  845. else:
  846. args['types'] = { args['type'] : types[args['type']] }
  847. del args['type']
  848. args['max_threads'] = args['threads']
  849. args['max_concurrency'] = max(args['concurrency_levels'])
  850. self.__dict__.update(args)
  851. # pprint(self.__dict__)
  852. self.start_time = time.time()
  853. self.run_test_timeout_seconds = 7200
  854. # setup logging
  855. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  856. # setup some additional variables
  857. if self.database_user == None: self.database_user = self.client_user
  858. if self.database_host == None: self.database_host = self.client_host
  859. if self.database_identity_file == None: self.database_identity_file = self.client_identity_file
  860. # Remember root directory
  861. self.fwroot = setup_util.get_fwroot()
  862. # setup current_benchmark.txt location
  863. self.current_benchmark = "/tmp/current_benchmark.txt"
  864. if hasattr(self, 'parse') and self.parse != None:
  865. self.timestamp = self.parse
  866. else:
  867. self.timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
  868. # setup results and latest_results directories
  869. self.result_directory = os.path.join(self.fwroot, "results")
  870. if (args['clean'] or args['clean_all']) and os.path.exists(os.path.join(self.fwroot, "results")):
  871. shutil.rmtree(os.path.join(self.fwroot, "results"))
  872. # remove installs directories if --clean-all provided
  873. self.install_root = "%s/%s" % (self.fwroot, "installs")
  874. if args['clean_all']:
  875. os.system("sudo rm -rf " + self.install_root)
  876. os.mkdir(self.install_root)
  877. self.results = None
  878. try:
  879. with open(os.path.join(self.full_results_directory(), 'results.json'), 'r') as f:
  880. #Load json file into results object
  881. self.results = json.load(f)
  882. except IOError:
  883. logging.warn("results.json for test not found.")
  884. if self.results == None:
  885. self.results = dict()
  886. self.results['concurrencyLevels'] = self.concurrency_levels
  887. self.results['queryIntervals'] = self.query_levels
  888. self.results['frameworks'] = [t.name for t in self.__gather_tests]
  889. self.results['duration'] = self.duration
  890. self.results['rawData'] = dict()
  891. self.results['rawData']['json'] = dict()
  892. self.results['rawData']['db'] = dict()
  893. self.results['rawData']['query'] = dict()
  894. self.results['rawData']['fortune'] = dict()
  895. self.results['rawData']['update'] = dict()
  896. self.results['rawData']['plaintext'] = dict()
  897. self.results['completed'] = dict()
  898. self.results['succeeded'] = dict()
  899. self.results['succeeded']['json'] = []
  900. self.results['succeeded']['db'] = []
  901. self.results['succeeded']['query'] = []
  902. self.results['succeeded']['fortune'] = []
  903. self.results['succeeded']['update'] = []
  904. self.results['succeeded']['plaintext'] = []
  905. self.results['failed'] = dict()
  906. self.results['failed']['json'] = []
  907. self.results['failed']['db'] = []
  908. self.results['failed']['query'] = []
  909. self.results['failed']['fortune'] = []
  910. self.results['failed']['update'] = []
  911. self.results['failed']['plaintext'] = []
  912. self.results['verify'] = dict()
  913. else:
  914. #for x in self.__gather_tests():
  915. # if x.name not in self.results['frameworks']:
  916. # self.results['frameworks'] = self.results['frameworks'] + [x.name]
  917. # Always overwrite framework list
  918. self.results['frameworks'] = [t.name for t in self.__gather_tests]
  919. # Setup the ssh command string
  920. self.database_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.database_user + "@" + self.database_host
  921. self.client_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.client_user + "@" + self.client_host
  922. if self.database_identity_file != None:
  923. self.database_ssh_string = self.database_ssh_string + " -i " + self.database_identity_file
  924. if self.client_identity_file != None:
  925. self.client_ssh_string = self.client_ssh_string + " -i " + self.client_identity_file
  926. ############################################################
  927. # End __init__
  928. ############################################################