benchmarker.py 49 KB

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