benchmarker.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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.txt
  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.txt")
  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.txt
  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/stats.txt
  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.txt")
  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/stats.txt
  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', 'sysctl', 'net.ipv4.tcp_tw_reuse=1'], stdout=self.quiet_out, stderr=subprocess.STDOUT)
  303. subprocess.call(['sudo', 'sysctl', 'net.ipv4.tcp_tw_recycle=1'], stdout=self.quiet_out, stderr=subprocess.STDOUT)
  304. subprocess.call(['sudo', 'sysctl', '-w', 'kernel.shmmax=134217728'], stdout=self.quiet_out, stderr=subprocess.STDOUT)
  305. subprocess.call(['sudo', 'sysctl', '-w', 'kernel.shmall=2097152'], stdout=self.quiet_out, stderr=subprocess.STDOUT)
  306. with open(os.path.join(self.full_results_directory(), 'sysctl.txt'), 'w') as f:
  307. f.write(subprocess.check_output(['sudo','sysctl','-a']))
  308. except subprocess.CalledProcessError:
  309. return False
  310. ############################################################
  311. # End __setup_server
  312. ############################################################
  313. ############################################################
  314. # Clean up any processes that run with root privileges
  315. ############################################################
  316. def __cleanup_leftover_processes_before_test(self):
  317. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  318. p.communicate("""
  319. sudo service mysql stop
  320. sudo service mongod stop
  321. sudo kill -9 $(pgrep postgres)
  322. sudo kill -9 $(pgrep mysql)
  323. sudo kill -9 $(pgrep mongo)
  324. """)
  325. ############################################################
  326. # Makes any necessary changes to the database machine that
  327. # should be made before running the tests. Is very similar
  328. # to the server setup, but may also include database specific
  329. # changes.
  330. ############################################################
  331. def __setup_database(self):
  332. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  333. p.communicate("""
  334. sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
  335. sudo sysctl -w net.core.somaxconn=65535
  336. sudo sysctl -w kernel.sched_autogroup_enabled=0
  337. sudo -s ulimit -n 65535
  338. sudo sysctl net.ipv4.tcp_tw_reuse=1
  339. sudo sysctl net.ipv4.tcp_tw_recycle=1
  340. sudo sysctl -w kernel.shmmax=2147483648
  341. sudo sysctl -w kernel.shmall=2097152
  342. sudo sysctl -w kernel.sem="250 32000 256 512"
  343. """)
  344. # TODO - print kernel configuration to file
  345. # echo "Printing kernel configuration:" && sudo sysctl -a
  346. # Explanations:
  347. # net.ipv4.tcp_max_syn_backlog, net.core.somaxconn, kernel.sched_autogroup_enabled: http://tweaked.io/guide/kernel/
  348. # ulimit -n: http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/
  349. # net.ipv4.tcp_tw_*: http://www.linuxbrigade.com/reduce-time_wait-socket-connections/
  350. # kernel.shm*: http://seriousbirder.com/blogs/linux-understanding-shmmax-and-shmall-settings/
  351. # 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
  352. ############################################################
  353. # End __setup_database
  354. ############################################################
  355. ############################################################
  356. ############################################################
  357. def __setup_database_container(self, database, port):
  358. def __scp_string(files):
  359. scpstr = ["scp", "-i", self.database_identity_file]
  360. for file in files:
  361. scpstr.append(file)
  362. scpstr.append("%s@%s:~/" % (self.database_user, self.database_host))
  363. return scpstr
  364. dbpath = os.path.join(self.fwroot, "toolset", "setup", "linux", "docker", "databases", database)
  365. dbfiles = ""
  366. for dbfile in os.listdir(dbpath):
  367. dbfiles += "%s " % os.path.join(dbpath,dbfile)
  368. p = subprocess.Popen(__scp_string(dbfiles.split()), stdin=subprocess.PIPE, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  369. p.communicate()
  370. p = subprocess.Popen(self.database_ssh_string, shell=True, stdin=subprocess.PIPE, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  371. p.communicate("docker build -f ~/%s.dockerfile -t %s ~/" % (database, database))
  372. if p.returncode != 0:
  373. return None
  374. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  375. (out,err) = p.communicate("docker run -d --rm -p %s:%s --network=host %s" % (port,port,database))
  376. return out.splitlines()[len(out.splitlines()) - 1]
  377. ############################################################
  378. ############################################################
  379. ############################################################
  380. # Makes any necessary changes to the client machine that
  381. # should be made before running the tests. Is very similar
  382. # to the server setup, but may also include client specific
  383. # changes.
  384. ############################################################
  385. def __setup_client(self):
  386. p = subprocess.Popen(self.client_ssh_string, stdin=subprocess.PIPE, shell=True, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  387. p.communicate("""
  388. sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
  389. sudo sysctl -w net.core.somaxconn=65535
  390. sudo -s ulimit -n 65535
  391. sudo sysctl net.ipv4.tcp_tw_reuse=1
  392. sudo sysctl net.ipv4.tcp_tw_recycle=1
  393. sudo sysctl -w kernel.shmmax=2147483648
  394. sudo sysctl -w kernel.shmall=2097152
  395. """)
  396. ############################################################
  397. # End __setup_client
  398. ############################################################
  399. ############################################################
  400. # __run_tests
  401. #
  402. # 2013-10-02 ASB Calls each test passed in tests to
  403. # __run_test in a separate process. Each
  404. # test is given a set amount of time and if
  405. # kills the child process (and subsequently
  406. # all of its child processes). Uses
  407. # multiprocessing module.
  408. ############################################################
  409. def __run_tests(self, tests):
  410. if len(tests) == 0:
  411. return 0
  412. logging.debug("Start __run_tests.")
  413. logging.debug("__name__ = %s",__name__)
  414. error_happened = False
  415. if self.os.lower() == 'windows':
  416. logging.debug("Executing __run_tests on Windows")
  417. for test in tests:
  418. with open(self.current_benchmark, 'w') as benchmark_resume_file:
  419. benchmark_resume_file.write(test.name)
  420. with self.quiet_out.enable():
  421. if self.__run_test(test) != 0:
  422. error_happened = True
  423. else:
  424. logging.debug("Executing __run_tests on Linux")
  425. # Setup a nice progressbar and ETA indicator
  426. widgets = [self.mode, ': ', progressbar.Percentage(),
  427. ' ', progressbar.Bar(),
  428. ' Rough ', progressbar.ETA()]
  429. pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(tests)).start()
  430. pbar_test = 0
  431. # These features do not work on Windows
  432. for test in tests:
  433. pbar.update(pbar_test)
  434. pbar_test = pbar_test + 1
  435. if __name__ == 'benchmark.benchmarker':
  436. print(header("Running Test: %s" % test.name))
  437. with open(self.current_benchmark, 'w') as benchmark_resume_file:
  438. benchmark_resume_file.write(test.name)
  439. with self.quiet_out.enable():
  440. test_process = Process(target=self.__run_test, name="Test Runner (%s)" % test.name, args=(test,))
  441. test_process.start()
  442. test_process.join(self.run_test_timeout_seconds)
  443. self.__load_results() # Load intermediate result from child process
  444. if(test_process.is_alive()):
  445. logging.debug("Child process for {name} is still alive. Terminating.".format(name=test.name))
  446. self.__write_intermediate_results(test.name,"__run_test timeout (="+ str(self.run_test_timeout_seconds) + " seconds)")
  447. test_process.terminate()
  448. test_process.join()
  449. if test_process.exitcode != 0:
  450. error_happened = True
  451. pbar.finish()
  452. if os.path.isfile(self.current_benchmark):
  453. os.remove(self.current_benchmark)
  454. logging.debug("End __run_tests.")
  455. if error_happened:
  456. return 1
  457. return 0
  458. ############################################################
  459. # End __run_tests
  460. ############################################################
  461. ############################################################
  462. # __run_test
  463. # 2013-10-02 ASB Previously __run_tests. This code now only
  464. # processes a single test.
  465. #
  466. # Ensures that the system has all necessary software to run
  467. # the tests. This does not include that software for the individual
  468. # test, but covers software such as curl and weighttp that
  469. # are needed.
  470. ############################################################
  471. def __run_test(self, test):
  472. logDir = os.path.join(self.full_results_directory(), test.name.lower())
  473. try:
  474. os.makedirs(logDir)
  475. except Exception:
  476. pass
  477. with open(os.path.join(logDir, 'out.txt'), 'w') as out:
  478. if test.os.lower() != self.os.lower() or test.database_os.lower() != self.database_os.lower():
  479. out.write("OS or Database OS specified in benchmark_config.json does not match the current environment. Skipping.\n")
  480. return sys.exit(0)
  481. # If the test is in the excludes list, we skip it
  482. if self.exclude != None and test.name in self.exclude:
  483. out.write("Test {name} has been added to the excludes list. Skipping.\n".format(name=test.name))
  484. return sys.exit(0)
  485. out.write("test.os.lower() = {os} test.database_os.lower() = {dbos}\n".format(os=test.os.lower(),dbos=test.database_os.lower()))
  486. out.write("self.results['frameworks'] != None: {val}\n".format(val=str(self.results['frameworks'] != None)))
  487. out.write("test.name: {name}\n".format(name=str(test.name)))
  488. out.write("self.results['completed']: {completed}\n".format(completed=str(self.results['completed'])))
  489. if self.results['frameworks'] != None and test.name in self.results['completed']:
  490. out.write('Framework {name} found in latest saved data. Skipping.\n'.format(name=str(test.name)))
  491. print('WARNING: Test {test} exists in the results directory; this must be removed before running a new test.\n'.format(test=str(test.name)))
  492. return sys.exit(1)
  493. out.flush()
  494. out.write(header("Beginning %s" % test.name, top='='))
  495. out.flush()
  496. ##########################
  497. # Start this test
  498. ##########################
  499. out.write(header("Starting %s" % test.name))
  500. out.flush()
  501. database_container_id = None
  502. try:
  503. # self.__cleanup_leftover_processes_before_test()
  504. if self.__is_port_bound(test.port):
  505. time.sleep(60)
  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 sys.exit(1)
  513. ##########################
  514. # Start database container
  515. ##########################
  516. if test.database != "None":
  517. # TODO: this is horrible... how should we really do it?
  518. ports = {
  519. "mysql": 3306,
  520. "postgres": 5432
  521. }
  522. database_container_id = self.__setup_database_container(test.database.lower(), ports[test.database.lower()])
  523. if not database_container_id:
  524. out.write("ERROR: Problem building/running database container")
  525. out.flush()
  526. self.__write_intermediate_results(test.name,"ERROR: Problem starting")
  527. return sys.exit(1)
  528. ##########################
  529. # Start webapp
  530. ##########################
  531. result = test.start(out)
  532. if result != 0:
  533. self.__stop_test(database_container_id, test, out)
  534. time.sleep(5)
  535. out.write( "ERROR: Problem starting {name}\n".format(name=test.name) )
  536. out.flush()
  537. self.__write_intermediate_results(test.name,"ERROR: Problem starting")
  538. return sys.exit(1)
  539. logging.info("Sleeping %s seconds to ensure framework is ready" % self.sleep)
  540. time.sleep(self.sleep)
  541. ##########################
  542. # Verify URLs
  543. ##########################
  544. if self.mode == "debug":
  545. logging.info("Entering debug mode. Server has started. CTRL-c to stop.")
  546. while True:
  547. time.sleep(1)
  548. else:
  549. logging.info("Verifying framework URLs")
  550. passed_verify = test.verify_urls(logDir)
  551. ##########################
  552. # Benchmark this test
  553. ##########################
  554. if self.mode == "benchmark":
  555. logging.info("Benchmarking")
  556. out.write(header("Benchmarking %s" % test.name))
  557. out.flush()
  558. test.benchmark(logDir)
  559. ##########################
  560. # Stop this test
  561. ##########################
  562. self.__stop_test(database_container_id, test, out)
  563. self.__stop_database(database_container_id, out)
  564. out.write(header("Stopped %s" % test.name))
  565. out.flush()
  566. ##########################################################
  567. # Remove contents of /tmp folder
  568. ##########################################################
  569. try:
  570. subprocess.check_call('sudo rm -rf /tmp/*', shell=True, stderr=out, stdout=out)
  571. except Exception:
  572. out.write(header("Error: Could not empty /tmp"))
  573. ##########################################################
  574. # Remove apt sources to avoid pkg errors and collisions
  575. ##########################################################
  576. os.system("sudo rm -rf /etc/apt/sources.list.d/*")
  577. ##########################################################
  578. # Save results thus far into the latest results directory
  579. ##########################################################
  580. out.write(header("Saving results through %s" % test.name))
  581. out.flush()
  582. self.__write_intermediate_results(test.name,time.strftime("%Y%m%d%H%M%S", time.localtime()))
  583. ##########################################################
  584. # Upload the results thus far to another server (optional)
  585. ##########################################################
  586. self.__upload_results()
  587. if self.mode == "verify" and not passed_verify:
  588. print("Failed verify!")
  589. return sys.exit(1)
  590. except KeyboardInterrupt:
  591. self.__stop_test(database_container_id, test, out)
  592. self.__stop_database(database_container_id, out)
  593. except (OSError, IOError, subprocess.CalledProcessError) as e:
  594. self.__write_intermediate_results(test.name,"<setup.py> raised an exception")
  595. out.write(header("Subprocess Error %s" % test.name))
  596. traceback.print_exc(file=out)
  597. out.flush()
  598. out.close()
  599. return sys.exit(1)
  600. out.close()
  601. return sys.exit(0)
  602. ############################################################
  603. # End __run_tests
  604. ############################################################
  605. ############################################################
  606. # __stop_database
  607. # Attempts to stop the running database container.
  608. ############################################################
  609. def __stop_database(self, database_container_id, out):
  610. if database_container_id:
  611. p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True, stdout=self.quiet_out, stderr=subprocess.STDOUT)
  612. p.communicate("docker stop %s" % database_container_id)
  613. ############################################################
  614. # __stop_test
  615. # Attempts to stop the running test container.
  616. ############################################################
  617. def __stop_test(self, database_container_id, test, out):
  618. docker_ids = subprocess.check_output(["docker", "ps", "-q"]).splitlines()
  619. for docker_id in docker_ids:
  620. # This check is in case the database and server machines are the same
  621. if docker_id:
  622. if not database_container_id or docker_id not in database_container_id:
  623. subprocess.check_output(["docker", "kill", docker_id])
  624. slept = 0
  625. while(slept < 300 and docker_id is ''):
  626. time.sleep(1)
  627. slept += 1
  628. docker_id = subprocess.check_output(["docker", "ps", "-q"]).strip()
  629. # We still need to sleep a bit before removing the image
  630. time.sleep(5)
  631. subprocess.check_output(["docker", "image", "rm", "tfb-test-%s" % test.name])
  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. ############################################################
  689. # End __parse_results
  690. ############################################################
  691. #############################################################
  692. # __count_sloc
  693. #############################################################
  694. def __count_sloc(self):
  695. frameworks = gather_frameworks(include=self.test,
  696. exclude=self.exclude, benchmarker=self)
  697. jsonResult = {}
  698. for framework, testlist in frameworks.iteritems():
  699. if not os.path.exists(os.path.join(testlist[0].directory, "source_code")):
  700. logging.warn("Cannot count lines of code for %s - no 'source_code' file", framework)
  701. continue
  702. # Unfortunately the source_code files use lines like
  703. # ./cpoll_cppsp/www/fortune_old instead of
  704. # ./www/fortune_old
  705. # so we have to back our working dir up one level
  706. wd = os.path.dirname(testlist[0].directory)
  707. try:
  708. command = "cloc --list-file=%s/source_code --yaml" % testlist[0].directory
  709. if os.path.exists(os.path.join(testlist[0].directory, "cloc_defs.txt")):
  710. command += " --read-lang-def %s" % os.path.join(testlist[0].directory, "cloc_defs.txt")
  711. logging.info("Using custom cloc definitions for %s", framework)
  712. # Find the last instance of the word 'code' in the yaml output. This should
  713. # be the line count for the sum of all listed files or just the line count
  714. # for the last file in the case where there's only one file listed.
  715. command = command + "| grep code | tail -1 | cut -d: -f 2"
  716. logging.debug("Running \"%s\" (cwd=%s)", command, wd)
  717. lineCount = subprocess.check_output(command, cwd=wd, shell=True)
  718. jsonResult[framework] = int(lineCount)
  719. except subprocess.CalledProcessError:
  720. continue
  721. except ValueError as ve:
  722. logging.warn("Unable to get linecount for %s due to error '%s'", framework, ve)
  723. self.results['rawData']['slocCounts'] = jsonResult
  724. ############################################################
  725. # End __count_sloc
  726. ############################################################
  727. ############################################################
  728. # __count_commits
  729. #
  730. ############################################################
  731. def __count_commits(self):
  732. frameworks = gather_frameworks(include=self.test,
  733. exclude=self.exclude, benchmarker=self)
  734. def count_commit(directory, jsonResult):
  735. command = "git rev-list HEAD -- " + directory + " | sort -u | wc -l"
  736. try:
  737. commitCount = subprocess.check_output(command, shell=True)
  738. jsonResult[framework] = int(commitCount)
  739. except subprocess.CalledProcessError:
  740. pass
  741. # Because git can be slow when run in large batches, this
  742. # calls git up to 4 times in parallel. Normal improvement is ~3-4x
  743. # in my trials, or ~100 seconds down to ~25
  744. # This is safe to parallelize as long as each thread only
  745. # accesses one key in the dictionary
  746. threads = []
  747. jsonResult = {}
  748. t1 = datetime.now()
  749. for framework, testlist in frameworks.iteritems():
  750. directory = testlist[0].directory
  751. t = threading.Thread(target=count_commit, args=(directory,jsonResult))
  752. t.start()
  753. threads.append(t)
  754. # Git has internal locks, full parallel will just cause contention
  755. # and slowness, so we rate-limit a bit
  756. if len(threads) >= 4:
  757. threads[0].join()
  758. threads.remove(threads[0])
  759. # Wait for remaining threads
  760. for t in threads:
  761. t.join()
  762. t2 = datetime.now()
  763. # print "Took %s seconds " % (t2 - t1).seconds
  764. self.results['rawData']['commitCounts'] = jsonResult
  765. self.commits = jsonResult
  766. ############################################################
  767. # End __count_commits
  768. ############################################################
  769. def __write_intermediate_results(self,test_name,status_message):
  770. self.results["completed"][test_name] = status_message
  771. self.__write_results()
  772. def __write_results(self):
  773. try:
  774. with open(os.path.join(self.full_results_directory(), 'results.json'), 'w') as f:
  775. f.write(json.dumps(self.results, indent=2))
  776. except (IOError):
  777. logging.error("Error writing results.json")
  778. def __set_completion_time(self):
  779. self.results['completionTime'] = int(round(time.time() * 1000))
  780. self.__write_results()
  781. def __upload_results(self):
  782. if self.results_upload_uri != None:
  783. try:
  784. requests.post(self.results_upload_uri, headers={ 'Content-Type': 'application/json' }, data=json.dumps(self.results, indent=2))
  785. except (Exception):
  786. logging.error("Error uploading results.json")
  787. def __load_results(self):
  788. try:
  789. with open(os.path.join(self.full_results_directory(), 'results.json')) as f:
  790. self.results = json.load(f)
  791. except (ValueError, IOError):
  792. pass
  793. def __get_git_commit_id(self):
  794. return subprocess.check_output('git rev-parse HEAD', shell=True).strip()
  795. def __get_git_repository_url(self):
  796. return subprocess.check_output('git config --get remote.origin.url', shell=True).strip()
  797. ############################################################
  798. # __finish
  799. ############################################################
  800. def __finish(self):
  801. if not self.list_tests and not self.parse:
  802. tests = self.__gather_tests
  803. # Normally you don't have to use Fore.BLUE before each line, but
  804. # Travis-CI seems to reset color codes on newline (see travis-ci/travis-ci#2692)
  805. # or stream flush, so we have to ensure that the color code is printed repeatedly
  806. prefix = Fore.CYAN
  807. for line in header("Verification Summary", top='=', bottom='').split('\n'):
  808. print(prefix + line)
  809. for test in tests:
  810. print(prefix + "| Test: {!s}".format(test.name))
  811. if test.name in self.results['verify'].keys():
  812. for test_type, result in self.results['verify'][test.name].iteritems():
  813. if result.upper() == "PASS":
  814. color = Fore.GREEN
  815. elif result.upper() == "WARN":
  816. color = Fore.YELLOW
  817. else:
  818. color = Fore.RED
  819. print(prefix + "| " + test_type.ljust(13) + ' : ' + color + result.upper())
  820. else:
  821. print(prefix + "| " + Fore.RED + "NO RESULTS (Did framework launch?)")
  822. print(prefix + header('', top='', bottom='=') + Style.RESET_ALL)
  823. print("Time to complete: " + str(int(time.time() - self.start_time)) + " seconds")
  824. print("Results are saved in " + os.path.join(self.result_directory, self.timestamp))
  825. ############################################################
  826. # End __finish
  827. ############################################################
  828. ##########################################################################################
  829. # Constructor
  830. ##########################################################################################
  831. ############################################################
  832. # Initialize the benchmarker. The args are the arguments
  833. # parsed via argparser.
  834. ############################################################
  835. def __init__(self, args):
  836. # Map type strings to their objects
  837. types = dict()
  838. types['json'] = JsonTestType()
  839. types['db'] = DBTestType()
  840. types['query'] = QueryTestType()
  841. types['fortune'] = FortuneTestType()
  842. types['update'] = UpdateTestType()
  843. types['plaintext'] = PlaintextTestType()
  844. types['cached_query'] = CachedQueryTestType()
  845. # Turn type into a map instead of a string
  846. if args['type'] == 'all':
  847. args['types'] = types
  848. else:
  849. args['types'] = { args['type'] : types[args['type']] }
  850. del args['type']
  851. args['max_concurrency'] = max(args['concurrency_levels'])
  852. if 'pipeline_concurrency_levels' not in args:
  853. args['pipeline_concurrency_levels'] = [256,1024,4096,16384]
  854. self.__dict__.update(args)
  855. # pprint(self.__dict__)
  856. self.quiet_out = QuietOutputStream(self.quiet)
  857. self.start_time = time.time()
  858. self.run_test_timeout_seconds = 7200
  859. # setup logging
  860. logging.basicConfig(stream=self.quiet_out, level=logging.INFO)
  861. # setup some additional variables
  862. if self.database_user == None: self.database_user = self.client_user
  863. if self.database_host == None: self.database_host = self.client_host
  864. if self.database_identity_file == None: self.database_identity_file = self.client_identity_file
  865. # Remember root directory
  866. self.fwroot = setup_util.get_fwroot()
  867. # setup current_benchmark.txt location
  868. self.current_benchmark = "/tmp/current_benchmark.txt"
  869. if hasattr(self, 'parse') and self.parse != None:
  870. self.timestamp = self.parse
  871. else:
  872. self.timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
  873. # setup results and latest_results directories
  874. self.result_directory = os.path.join(self.fwroot, "results")
  875. if (args['clean'] or args['clean_all']) and os.path.exists(os.path.join(self.fwroot, "results")):
  876. os.system("sudo rm -rf " + self.result_directory + "/*")
  877. # TODO: remove this as installs goes away with docker implementation
  878. # remove installs directories if --clean-all provided
  879. self.install_root = "%s/%s" % (self.fwroot, "installs")
  880. if args['clean_all']:
  881. os.system("sudo rm -rf " + self.install_root)
  882. os.mkdir(self.install_root)
  883. self.results = None
  884. try:
  885. with open(os.path.join(self.full_results_directory(), 'results.json'), 'r') as f:
  886. #Load json file into results object
  887. self.results = json.load(f)
  888. except IOError:
  889. logging.warn("results.json for test not found.")
  890. if self.results == None:
  891. self.results = dict()
  892. self.results['uuid'] = str(uuid.uuid4())
  893. self.results['name'] = datetime.now().strftime(self.results_name)
  894. self.results['environmentDescription'] = self.results_environment
  895. try:
  896. self.results['git'] = dict()
  897. self.results['git']['commitId'] = self.__get_git_commit_id()
  898. self.results['git']['repositoryUrl'] = self.__get_git_repository_url()
  899. except Exception as e:
  900. logging.debug('Could not read local git repository, which is fine. The error was: %s', e)
  901. self.results['git'] = None
  902. self.results['startTime'] = int(round(time.time() * 1000))
  903. self.results['completionTime'] = None
  904. self.results['concurrencyLevels'] = self.concurrency_levels
  905. self.results['pipelineConcurrencyLevels'] = self.pipeline_concurrency_levels
  906. self.results['queryIntervals'] = self.query_levels
  907. self.results['cachedQueryIntervals'] = self.cached_query_levels
  908. self.results['frameworks'] = [t.name for t in self.__gather_tests]
  909. self.results['duration'] = self.duration
  910. self.results['rawData'] = dict()
  911. self.results['rawData']['json'] = dict()
  912. self.results['rawData']['db'] = dict()
  913. self.results['rawData']['query'] = dict()
  914. self.results['rawData']['fortune'] = dict()
  915. self.results['rawData']['update'] = dict()
  916. self.results['rawData']['plaintext'] = dict()
  917. self.results['rawData']['cached_query'] = dict()
  918. self.results['completed'] = dict()
  919. self.results['succeeded'] = dict()
  920. self.results['succeeded']['json'] = []
  921. self.results['succeeded']['db'] = []
  922. self.results['succeeded']['query'] = []
  923. self.results['succeeded']['fortune'] = []
  924. self.results['succeeded']['update'] = []
  925. self.results['succeeded']['plaintext'] = []
  926. self.results['succeeded']['cached_query'] = []
  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['failed']['cached_query'] = []
  935. self.results['verify'] = dict()
  936. else:
  937. #for x in self.__gather_tests():
  938. # if x.name not in self.results['frameworks']:
  939. # self.results['frameworks'] = self.results['frameworks'] + [x.name]
  940. # Always overwrite framework list
  941. self.results['frameworks'] = [t.name for t in self.__gather_tests]
  942. # Setup the ssh command string
  943. self.database_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.database_user + "@" + self.database_host
  944. self.client_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.client_user + "@" + self.client_host
  945. if self.database_identity_file != None:
  946. self.database_ssh_string = self.database_ssh_string + " -i " + self.database_identity_file
  947. if self.client_identity_file != None:
  948. self.client_ssh_string = self.client_ssh_string + " -i " + self.client_identity_file
  949. self.__process = None
  950. ############################################################
  951. # End __init__
  952. ############################################################
  953. class QuietOutputStream:
  954. def __init__(self, is_quiet):
  955. self.is_quiet = is_quiet
  956. self.null_out = open(os.devnull, 'w')
  957. def fileno(self):
  958. with self.enable():
  959. return sys.stdout.fileno()
  960. def write(self, message):
  961. with self.enable():
  962. sys.stdout.write(message)
  963. @contextmanager
  964. def enable(self):
  965. if self.is_quiet:
  966. old_out = sys.stdout
  967. old_err = sys.stderr
  968. try:
  969. sys.stdout = self.null_out
  970. sys.stderr = self.null_out
  971. yield
  972. finally:
  973. sys.stdout = old_out
  974. sys.stderr = old_err
  975. else:
  976. yield