benchmarker.py 48 KB

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