benchmarker.py 26 KB

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