benchmarker.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. from installer import Installer
  2. from framework_test import FrameworkTest
  3. import framework_test
  4. import os
  5. import json
  6. import subprocess
  7. import time
  8. import textwrap
  9. import pprint
  10. import csv
  11. import sys
  12. from datetime import datetime
  13. class Benchmarker:
  14. ##########################################################################################
  15. # Public methods
  16. ##########################################################################################
  17. ############################################################
  18. # Prints all the available tests
  19. ############################################################
  20. def run_list_tests(self):
  21. all_tests = self.__gather_tests()
  22. for test in all_tests:
  23. print str(test.sort) + ": " + test.name
  24. ############################################################
  25. # End run_list_tests
  26. ############################################################
  27. ############################################################
  28. # next_sort
  29. # Prints the next available sort number that should be used
  30. # for any new tests
  31. ############################################################
  32. def next_sort_value(self):
  33. all_tests = self.__gather_tests()
  34. # all_tests is already sorted by sort, so we can just get
  35. # the last one and add one to it.
  36. print " Next sort number is: " + str(all_tests[-1].sort + 1)
  37. self.__finish()
  38. ############################################################
  39. # End next_sort_value
  40. ############################################################
  41. ############################################################
  42. # parse_timestamp
  43. # Re-parses the raw data for a given timestamp
  44. ############################################################
  45. def parse_timestamp(self):
  46. all_tests = self.__gather_tests()
  47. for test in all_tests:
  48. test.parse_all()
  49. self.__parse_results(all_tests)
  50. self.__finish()
  51. ############################################################
  52. # End run_list_tests
  53. ############################################################
  54. ############################################################
  55. # Run the tests:
  56. # This process involves setting up the client/server machines
  57. # with any necessary change. Then going through each test,
  58. # running their setup script, verifying the URLs, and
  59. # running benchmarks against them.
  60. ############################################################
  61. def run(self):
  62. ##########################
  63. # Get a list of all known
  64. # tests that we can run.
  65. ##########################
  66. all_tests = self.__gather_tests()
  67. ##########################
  68. # Setup client/server
  69. ##########################
  70. print textwrap.dedent("""
  71. =====================================================
  72. Preparing up Server and Client ...
  73. =====================================================
  74. """)
  75. self.__setup_server()
  76. self.__setup_client()
  77. ##########################
  78. # Run tests
  79. ##########################
  80. self.__run_tests(all_tests)
  81. ##########################
  82. # Parse results
  83. ##########################
  84. if self.mode == "benchmark":
  85. print textwrap.dedent("""
  86. =====================================================
  87. Parsing Results ...
  88. =====================================================
  89. """)
  90. self.__parse_results(all_tests)
  91. self.__finish()
  92. ############################################################
  93. # End run
  94. ############################################################
  95. ############################################################
  96. # sftp_string(batch_file)
  97. # generates a fully qualified URL for sftp to client
  98. ############################################################
  99. def sftp_string(self, batch_file):
  100. sftp_string = "sftp -oStrictHostKeyChecking=no "
  101. if batch_file != None: sftp_string += " -b " + batch_file + " "
  102. if self.identity_file != None:
  103. sftp_string += " -i " + self.identity_file + " "
  104. return sftp_string + self.client_user + "@" + self.client_host
  105. ############################################################
  106. # End sftp_string
  107. ############################################################
  108. ############################################################
  109. # generate_url(url, port)
  110. # generates a fully qualified URL for accessing a test url
  111. ############################################################
  112. def generate_url(self, url, port):
  113. return self.server_host + ":" + str(port) + url
  114. ############################################################
  115. # End generate_url
  116. ############################################################
  117. ############################################################
  118. # output_file(test_name, test_type)
  119. # returns the output file for this test_name and test_type
  120. # timestamp/test_type/test_name/raw
  121. ############################################################
  122. def output_file(self, test_name, test_type):
  123. path = os.path.join(self.result_directory, self.timestamp, test_type, test_name, "raw")
  124. try:
  125. os.makedirs(os.path.dirname(path))
  126. except OSError:
  127. pass
  128. return path
  129. ############################################################
  130. # End output_file
  131. ############################################################
  132. ############################################################
  133. # full_results_directory
  134. ############################################################
  135. def full_results_directory(self):
  136. path = os.path.join(self.result_directory, self.timestamp)
  137. try:
  138. os.makedirs(path)
  139. except OSError:
  140. pass
  141. return path
  142. ############################################################
  143. # End output_file
  144. ############################################################
  145. ############################################################
  146. # report_results
  147. ############################################################
  148. def report_results(self, framework, test, results):
  149. if test not in self.results['rawData'].keys():
  150. self.results['rawData'][test] = dict()
  151. self.results['rawData'][test][framework.sort] = results
  152. ############################################################
  153. # End report_results
  154. ############################################################
  155. ##########################################################################################
  156. # Private methods
  157. ##########################################################################################
  158. ############################################################
  159. # Gathers all the tests
  160. ############################################################
  161. def __gather_tests(self):
  162. tests = []
  163. # Loop through each directory (we assume we're being run from the benchmarking root)
  164. # and look for the files that signify a benchmark test
  165. for dirname, dirnames, filenames in os.walk('.'):
  166. # Look for the benchmark_config file, this will set up our tests
  167. # It's format looks like this:
  168. #
  169. # {
  170. # "framework": "nodejs",
  171. # "tests": [{
  172. # "default": {
  173. # "setup_file": "setup",
  174. # "json_url": "/json"
  175. # },
  176. # "mysql": {
  177. # "setup_file": "setup",
  178. # "db_url": "/mysql",
  179. # "query_url": "/mysql?queries="
  180. # },
  181. # ...
  182. # }]
  183. # }
  184. if 'benchmark_config' in filenames:
  185. config = None
  186. with open(os.path.join(dirname, 'benchmark_config'), 'r') as config_file:
  187. # Load json file into config object
  188. config = json.load(config_file)
  189. if config == None:
  190. continue
  191. tests = tests + framework_test.parse_config(config, dirname[2:], self)
  192. tests.sort(key=lambda x: x.sort)
  193. return tests
  194. ############################################################
  195. # End __gather_tests
  196. ############################################################
  197. ############################################################
  198. # Gathers all the frameworks
  199. ############################################################
  200. def __gather_frameworks(self):
  201. frameworks = []
  202. # Loop through each directory (we assume we're being run from the benchmarking root)
  203. for dirname, dirnames, filenames in os.walk('.'):
  204. # Look for the benchmark_config file, this will contain our framework name
  205. # It's format looks like this:
  206. #
  207. # {
  208. # "framework": "nodejs",
  209. # "tests": [{
  210. # "default": {
  211. # "setup_file": "setup",
  212. # "json_url": "/json"
  213. # },
  214. # "mysql": {
  215. # "setup_file": "setup",
  216. # "db_url": "/mysql",
  217. # "query_url": "/mysql?queries="
  218. # },
  219. # ...
  220. # }]
  221. # }
  222. if 'benchmark_config' in filenames:
  223. config = None
  224. with open(os.path.join(dirname, 'benchmark_config'), 'r') as config_file:
  225. # Load json file into config object
  226. config = json.load(config_file)
  227. if config == None:
  228. continue
  229. frameworks.append(str(config['framework']))
  230. return frameworks
  231. ############################################################
  232. # End __gather_frameworks
  233. ############################################################
  234. ############################################################
  235. # Makes any necessary changes to the server that should be
  236. # made before running the tests. This involves setting kernal
  237. # settings to allow for more connections, or more file
  238. # descriptiors
  239. #
  240. # http://redmine.lighttpd.net/projects/weighttp/wiki#Troubleshooting
  241. ############################################################
  242. def __setup_server(self):
  243. try:
  244. if os.name == 'nt':
  245. return True
  246. subprocess.check_call("sudo sysctl -w net.core.somaxconn=5000".rsplit(" "))
  247. subprocess.check_call("sudo -s ulimit -n 16384".rsplit(" "))
  248. subprocess.check_call("sudo sysctl net.ipv4.tcp_tw_reuse=1".rsplit(" "))
  249. subprocess.check_call("sudo sysctl net.ipv4.tcp_tw_recycle=1".rsplit(" "))
  250. subprocess.check_call("sudo sysctl -w kernel.shmmax=134217728".rsplit(" "))
  251. subprocess.check_call("sudo sysctl -w kernel.shmall=2097152".rsplit(" "))
  252. except subprocess.CalledProcessError:
  253. return False
  254. ############################################################
  255. # End __setup_server
  256. ############################################################
  257. ############################################################
  258. # Makes any necessary changes to the client machine that
  259. # should be made before running the tests. Is very similar
  260. # to the server setup, but may also include client specific
  261. # changes.
  262. ############################################################
  263. def __setup_client(self):
  264. p = subprocess.Popen(self.ssh_string, stdin=subprocess.PIPE, shell=True)
  265. p.communicate("""
  266. sudo sysctl -w net.core.somaxconn=5000
  267. sudo -s ulimit -n 16384
  268. sudo sysctl net.ipv4.tcp_tw_reuse=1
  269. sudo sysctl net.ipv4.tcp_tw_recycle=1
  270. sudo sysctl -w kernel.shmmax=2147483648
  271. sudo sysctl -w kernel.shmall=2097152
  272. """)
  273. ############################################################
  274. # End __setup_client
  275. ############################################################
  276. ############################################################
  277. # __run_tests
  278. # Ensures that the system has all necessary software to run
  279. # the tests. This does not include that software for the individual
  280. # test, but covers software such as curl and weighttp that
  281. # are needed.
  282. ############################################################
  283. def __run_tests(self, tests):
  284. for test in tests:
  285. if test.os == 'nt' and os.name != 'nt':
  286. # this is a windows only test, but we're not on windows. abort.
  287. continue
  288. # If the user specified which tests to run, then
  289. # we can skip over tests that are not in that list
  290. if self.test != None and test.name not in self.test:
  291. continue
  292. # If the test is in the excludes list, we skip it
  293. if self.exclude != None and test.name in self.exclude:
  294. continue
  295. # If the test does not contain an implementation of the current test-type, skip it
  296. if self.type != 'all' and not test.contains_type(self.type):
  297. continue
  298. print textwrap.dedent("""
  299. =====================================================
  300. Beginning {name}
  301. -----------------------------------------------------
  302. """.format(name=test.name))
  303. ##########################
  304. # Start this test
  305. ##########################
  306. print textwrap.dedent("""
  307. -----------------------------------------------------
  308. Starting {name}
  309. -----------------------------------------------------
  310. """.format(name=test.name))
  311. try:
  312. p = subprocess.Popen(self.ssh_string, stdin=subprocess.PIPE, shell=True)
  313. p.communicate("""
  314. sudo restart mysql
  315. sudo restart mongodb
  316. """)
  317. time.sleep(10)
  318. result = test.start()
  319. if result != 0:
  320. test.stop()
  321. time.sleep(5)
  322. print "ERROR: Problem starting " + test.name
  323. print textwrap.dedent("""
  324. -----------------------------------------------------
  325. Stopped {name}
  326. -----------------------------------------------------
  327. """.format(name=test.name))
  328. continue
  329. time.sleep(self.sleep)
  330. ##########################
  331. # Verify URLs
  332. ##########################
  333. print textwrap.dedent("""
  334. -----------------------------------------------------
  335. Verifying URLs for {name}
  336. -----------------------------------------------------
  337. """.format(name=test.name))
  338. test.verify_urls()
  339. ##########################
  340. # Benchmark this test
  341. ##########################
  342. if self.mode == "benchmark":
  343. print textwrap.dedent("""
  344. -----------------------------------------------------
  345. Benchmarking {name} ...
  346. -----------------------------------------------------
  347. """.format(name=test.name))
  348. test.benchmark()
  349. ##########################
  350. # Stop this test
  351. ##########################
  352. test.stop()
  353. time.sleep(5)
  354. print textwrap.dedent("""
  355. -----------------------------------------------------
  356. Stopped {name}
  357. -----------------------------------------------------
  358. """.format(name=test.name))
  359. time.sleep(5)
  360. except (KeyboardInterrupt, SystemExit):
  361. test.stop()
  362. print """
  363. -----------------------------------------------------
  364. Cleaning up....
  365. -----------------------------------------------------
  366. """
  367. self.__finish()
  368. sys.exit()
  369. ############################################################
  370. # End __run_tests
  371. ############################################################
  372. ############################################################
  373. # __parse_results
  374. # Ensures that the system has all necessary software to run
  375. # the tests. This does not include that software for the individual
  376. # test, but covers software such as curl and weighttp that
  377. # are needed.
  378. ############################################################
  379. def __parse_results(self, tests):
  380. # Run the method to get the commmit count of each framework.
  381. self.__count_commits()
  382. # Time to create parsed files
  383. # Aggregate JSON file
  384. with open(os.path.join(self.full_results_directory(), "results.json"), "w") as f:
  385. f.write(json.dumps(self.results))
  386. # JSON CSV
  387. # with open(os.path.join(self.full_results_directory(), "json.csv"), 'wb') as csvfile:
  388. # writer = csv.writer(csvfile)
  389. # writer.writerow(["Framework"] + self.concurrency_levels)
  390. # for key, value in self.results['rawData']['json'].iteritems():
  391. # framework = self.results['frameworks'][int(key)]
  392. # writer.writerow([framework] + value)
  393. # DB CSV
  394. #with open(os.path.join(self.full_results_directory(), "db.csv"), 'wb') as csvfile:
  395. # writer = csv.writer(csvfile)
  396. # writer.writerow(["Framework"] + self.concurrency_levels)
  397. # for key, value in self.results['rawData']['db'].iteritems():
  398. # framework = self.results['frameworks'][int(key)]
  399. # writer.writerow([framework] + value)
  400. # Query CSV
  401. #with open(os.path.join(self.full_results_directory(), "query.csv"), 'wb') as csvfile:
  402. # writer = csv.writer(csvfile)
  403. # writer.writerow(["Framework"] + self.query_intervals)
  404. # for key, value in self.results['rawData']['query'].iteritems():
  405. # framework = self.results['frameworks'][int(key)]
  406. # writer.writerow([framework] + value)
  407. # Fortune CSV
  408. #with open(os.path.join(self.full_results_directory(), "fortune.csv"), 'wb') as csvfile:
  409. # writer = csv.writer(csvfile)
  410. # writer.writerow(["Framework"] + self.query_intervals)
  411. # if 'fortune' in self.results['rawData'].keys():
  412. # for key, value in self.results['rawData']['fortune'].iteritems():
  413. # framework = self.results['frameworks'][int(key)]
  414. # writer.writerow([framework] + value)
  415. ############################################################
  416. # End __parse_results
  417. ############################################################
  418. ############################################################
  419. # __count_commits
  420. ############################################################
  421. def __count_commits(self):
  422. all_frameworks = self.__gather_frameworks()
  423. jsonResult = {}
  424. for framework in all_frameworks:
  425. try:
  426. command = "git rev-list HEAD -- " + framework + " | sort -u | wc -l"
  427. commitCount = subprocess.check_output(command, shell=True)
  428. jsonResult[framework] = int(commitCount)
  429. except:
  430. continue
  431. self.results['rawData']['commitCounts'] = jsonResult
  432. self.commits = jsonResult
  433. ############################################################
  434. # End __count_commits
  435. ############################################################
  436. ############################################################
  437. # __finish
  438. ############################################################
  439. def __finish(self):
  440. print "Time to complete: " + str(int(time.time() - self.start_time)) + " seconds"
  441. print "Results are saved in " + os.path.join(self.result_directory, self.timestamp)
  442. ############################################################
  443. # End __finish
  444. ############################################################
  445. ##########################################################################################
  446. # Constructor
  447. ##########################################################################################
  448. ############################################################
  449. # Initialize the benchmarker. The args are the arguments
  450. # parsed via argparser.
  451. ############################################################
  452. def __init__(self, args):
  453. self.__dict__.update(args)
  454. self.start_time = time.time()
  455. # setup some additional variables
  456. if self.database_host == None: self.database_host = self.client_host
  457. self.result_directory = os.path.join("results", self.name)
  458. if self.parse != None:
  459. self.timestamp = self.parse
  460. else:
  461. self.timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
  462. # Setup the concurrency levels array. This array goes from
  463. # starting_concurrency to max concurrency, doubling each time
  464. self.concurrency_levels = []
  465. concurrency = self.starting_concurrency
  466. while concurrency <= self.max_concurrency:
  467. self.concurrency_levels.append(concurrency)
  468. concurrency = concurrency * 2
  469. # Setup query interval array
  470. # starts at 1, and goes up to max_queries, using the query_interval
  471. self.query_intervals = []
  472. queries = 1
  473. while queries <= self.max_queries:
  474. self.query_intervals.append(queries)
  475. if queries == 1:
  476. queries = 0
  477. queries = queries + self.query_interval
  478. # Load the latest data
  479. self.latest = None
  480. try:
  481. with open('latest.json', 'r') as f:
  482. # Load json file into config object
  483. self.latest = json.load(f)
  484. except IOError:
  485. pass
  486. self.results = None
  487. try:
  488. if self.latest != None and self.name in self.latest.keys():
  489. with open(os.path.join(self.result_directory, str(self.latest[self.name]), 'results.json'), 'r') as f:
  490. # Load json file into config object
  491. self.results = json.load(f)
  492. except IOError:
  493. pass
  494. if self.results == None:
  495. self.results = dict()
  496. self.results['concurrencyLevels'] = self.concurrency_levels
  497. self.results['queryIntervals'] = self.query_intervals
  498. self.results['frameworks'] = [t.name for t in self.__gather_tests()]
  499. self.results['duration'] = self.duration
  500. self.results['rawData'] = dict()
  501. self.results['rawData']['json'] = dict()
  502. self.results['rawData']['db'] = dict()
  503. self.results['rawData']['query'] = dict()
  504. self.results['rawData']['fortune'] = dict()
  505. self.results['rawData']['update'] = dict()
  506. self.results['rawData']['plaintext'] = dict()
  507. else:
  508. #for x in self.__gather_tests():
  509. # if x.name not in self.results['frameworks']:
  510. # self.results['frameworks'] = self.results['frameworks'] + [x.name]
  511. # Always overwrite framework list
  512. self.results['frameworks'] = [t.name for t in self.__gather_tests()]
  513. # Setup the ssh command string
  514. self.ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.client_user + "@" + self.client_host
  515. if self.identity_file != None:
  516. self.ssh_string = self.ssh_string + " -i " + self.identity_file
  517. if self.install_software:
  518. install = Installer(self)
  519. install.install_software()
  520. ############################################################
  521. # End __init__
  522. ############################################################