benchmarker.py 42 KB

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