framework_test.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. from benchmark.fortune_html_parser import FortuneHTMLParser
  2. from setup.linux import setup_util
  3. from benchmark.test_types import *
  4. import importlib
  5. import os
  6. import subprocess
  7. import time
  8. import re
  9. from pprint import pprint
  10. import sys
  11. import traceback
  12. import json
  13. import logging
  14. import csv
  15. import shlex
  16. import math
  17. from threading import Thread
  18. from threading import Event
  19. from utils import header
  20. class FrameworkTest:
  21. headers_template = "-H 'Host: localhost' -H '{accept}' -H 'Connection: keep-alive'"
  22. # Used for test types that do not require a database -
  23. # These tests are run at multiple concurrency levels
  24. concurrency_template = """
  25. echo ""
  26. echo "---------------------------------------------------------"
  27. echo " Running Primer {name}"
  28. echo " {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 \"http://{server_host}:{port}{url}\""
  29. echo "---------------------------------------------------------"
  30. echo ""
  31. {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 "http://{server_host}:{port}{url}"
  32. sleep 5
  33. echo ""
  34. echo "---------------------------------------------------------"
  35. echo " Running Warmup {name}"
  36. echo " {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}\""
  37. echo "---------------------------------------------------------"
  38. echo ""
  39. {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}"
  40. sleep 5
  41. echo ""
  42. echo "---------------------------------------------------------"
  43. echo " Synchronizing time"
  44. echo "---------------------------------------------------------"
  45. echo ""
  46. ntpdate -s pool.ntp.org
  47. for c in {interval}
  48. do
  49. echo ""
  50. echo "---------------------------------------------------------"
  51. echo " Concurrency: $c for {name}"
  52. echo " {wrk} {headers} -d {duration} -c $c --timeout $c -t $(($c>{max_threads}?{max_threads}:$c)) \"http://{server_host}:{port}{url}\" -s ~/pipeline.lua -- {pipeline}"
  53. echo "---------------------------------------------------------"
  54. echo ""
  55. STARTTIME=$(date +"%s")
  56. {wrk} {headers} -d {duration} -c $c --timeout $c -t "$(($c>{max_threads}?{max_threads}:$c))" http://{server_host}:{port}{url} -s ~/pipeline.lua -- {pipeline}
  57. echo "STARTTIME $STARTTIME"
  58. echo "ENDTIME $(date +"%s")"
  59. sleep 2
  60. done
  61. """
  62. # Used for test types that require a database -
  63. # These tests run at a static concurrency level and vary the size of
  64. # the query sent with each request
  65. query_template = """
  66. echo ""
  67. echo "---------------------------------------------------------"
  68. echo " Running Primer {name}"
  69. echo " wrk {headers} -d 5 -c 8 --timeout 8 -t 8 \"http://{server_host}:{port}{url}2\""
  70. echo "---------------------------------------------------------"
  71. echo ""
  72. wrk {headers} -d 5 -c 8 --timeout 8 -t 8 "http://{server_host}:{port}{url}2"
  73. sleep 5
  74. echo ""
  75. echo "---------------------------------------------------------"
  76. echo " Running Warmup {name}"
  77. echo " wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}2\""
  78. echo "---------------------------------------------------------"
  79. echo ""
  80. wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}2"
  81. sleep 5
  82. echo ""
  83. echo "---------------------------------------------------------"
  84. echo " Synchronizing time"
  85. echo "---------------------------------------------------------"
  86. echo ""
  87. ntpdate -s pool.ntp.org
  88. for c in {interval}
  89. do
  90. echo ""
  91. echo "---------------------------------------------------------"
  92. echo " Queries: $c for {name}"
  93. echo " wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}$c\""
  94. echo "---------------------------------------------------------"
  95. echo ""
  96. STARTTIME=$(date +"%s")
  97. wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}$c"
  98. echo "STARTTIME $STARTTIME"
  99. echo "ENDTIME $(date +"%s")"
  100. sleep 2
  101. done
  102. """
  103. ############################################################
  104. # Parses the given HTML string and asks a FortuneHTMLParser
  105. # whether the parsed string is a valid fortune return.
  106. ############################################################
  107. def validateFortune(self, htmlString, out, err):
  108. err_str = ""
  109. if htmlString is None or len(htmlString) == 0:
  110. err_str += "Empty Response"
  111. return (False, err_str)
  112. try:
  113. parser = FortuneHTMLParser()
  114. parser.feed(htmlString)
  115. valid = parser.isValidFortune(out)
  116. return (valid, '' if valid else 'Did not pass validation')
  117. except:
  118. print "Got exception when trying to validate the fortune test: {exception} ".format(exception=traceback.format_exc())
  119. return (False, err_str)
  120. ############################################################
  121. # Validates the jsonString is an array with a length of
  122. # 2, that each entry in the array is a JSON object, that
  123. # each object has an "id" and a "randomNumber" key, and that
  124. # both keys map to integers.
  125. ############################################################
  126. def validateUpdate(self, jsonString, out, err):
  127. err_str = ""
  128. if jsonString is None or len(jsonString) == 0:
  129. err_str += "Empty Response"
  130. return (False, err_str)
  131. try:
  132. arr = [{k.lower(): v for k,v in d.iteritems()} for d in json.loads(jsonString)]
  133. if len(arr) != 2:
  134. err_str += "Expected array of length 2. Got length {length}.\n".format(length=len(arr))
  135. for obj in arr:
  136. id_ret_val = True
  137. random_num_ret_val = True
  138. if "id" not in obj or "randomnumber" not in obj:
  139. err_str += "Expected keys id and randomNumber to be in JSON string.\n"
  140. return (False, err_str)
  141. try:
  142. if not isinstance(float(obj["id"]), float):
  143. id_ret_val=False
  144. except:
  145. id_ret_val=False
  146. if not id_ret_val:
  147. err_str += "Expected id to be type int or float, got '{rand}'.\n".format(rand=obj["randomnumber"])
  148. try:
  149. if not isinstance(float(obj["randomnumber"]), float):
  150. random_num_ret_val=False
  151. except:
  152. random_num_ret_val=False
  153. if not random_num_ret_val:
  154. err_str += "Expected randomNumber to be type int or float, got '{rand}'.\n".format(rand=obj["randomnumber"])
  155. except:
  156. err_str += "Got exception when trying to validate the update test: {exception}\n".format(exception=traceback.format_exc())
  157. return (True, ) if len(err_str) == 0 else (False, err_str)
  158. ############################################################
  159. # start(benchmarker)
  160. # Start the test using it's setup file
  161. ############################################################
  162. def start(self, out, err):
  163. # Load profile for this installation
  164. profile="%s/bash_profile.sh" % self.directory
  165. if not os.path.exists(profile):
  166. logging.warning("Directory %s does not have a bash_profile.sh" % self.directory)
  167. profile="$FWROOT/config/benchmark_profile"
  168. # Setup variables for TROOT and IROOT
  169. setup_util.replace_environ(config=profile,
  170. command='export TROOT=%s && export IROOT=%s' %
  171. (self.directory, self.install_root))
  172. # Because start can take so long, we print a dot to let the user know
  173. # we are working
  174. class ProgressPrinterThread(Thread):
  175. def __init__(self, event):
  176. Thread.__init__(self)
  177. self.stopped = event
  178. def run(self):
  179. while not self.stopped.wait(20):
  180. sys.stderr.write("Waiting for start to return...\n")
  181. stopFlag = Event()
  182. thread = ProgressPrinterThread(stopFlag)
  183. thread.start()
  184. # Run the module start (inside parent of TROOT)
  185. # - we use the parent as a historical accident - a lot of tests
  186. # use subprocess's cwd argument already
  187. previousDir = os.getcwd()
  188. os.chdir(os.path.dirname(self.troot))
  189. logging.info("Running setup module start (cwd=%s)", os.path.dirname(self.troot))
  190. try:
  191. retcode = self.setup_module.start(self, out, err)
  192. if retcode == None:
  193. retcode = 0
  194. except Exception:
  195. retcode = 1
  196. st = traceback.format_exc()
  197. st = '\n'.join((4 * ' ') + x for x in st.splitlines())
  198. st = "Start exception:\n%s" % st
  199. logging.info(st)
  200. err.write(st + '\n')
  201. os.chdir(previousDir)
  202. # Stop the progress printer
  203. stopFlag.set()
  204. logging.info("Called setup.py start")
  205. return retcode
  206. ############################################################
  207. # End start
  208. ############################################################
  209. ############################################################
  210. # stop(benchmarker)
  211. # Stops the test using it's setup file
  212. ############################################################
  213. def stop(self, out, err):
  214. # Load profile for this installation
  215. profile="%s/bash_profile.sh" % self.directory
  216. if not os.path.exists(profile):
  217. logging.warning("Directory %s does not have a bash_profile.sh" % self.directory)
  218. profile="$FWROOT/config/benchmark_profile"
  219. setup_util.replace_environ(config=profile,
  220. command='export TROOT=%s && export IROOT=%s' %
  221. (self.directory, self.install_root))
  222. # Run the module stop (inside parent of TROOT)
  223. # - we use the parent as a historical accident - a lot of tests
  224. # use subprocess's cwd argument already
  225. previousDir = os.getcwd()
  226. os.chdir(os.path.dirname(self.troot))
  227. logging.info("Running setup module stop (cwd=%s)", os.path.dirname(self.troot))
  228. try:
  229. retcode = self.setup_module.stop(out, err)
  230. if retcode == None:
  231. retcode = 0
  232. except Exception:
  233. retcode = 1
  234. st = traceback.format_exc()
  235. st = '\n'.join((4 * ' ') + x for x in st.splitlines())
  236. st = "Stop exception:\n%s\n" % st
  237. logging.info(st)
  238. err.write(st + '\n')
  239. os.chdir(previousDir)
  240. # Give processes sent a SIGTERM a moment to shut down gracefully
  241. time.sleep(5)
  242. return retcode
  243. ############################################################
  244. # End stop
  245. ############################################################
  246. ############################################################
  247. # verify_urls
  248. # Verifys each of the URLs for this test. THis will sinply
  249. # curl the URL and check for it's return status.
  250. # For each url, a flag will be set on this object for whether
  251. # or not it passed
  252. # Returns True if all verifications succeeded
  253. ############################################################
  254. def verify_urls(self, out, err):
  255. result = True
  256. def verify_type(test_type):
  257. test = self.runTests[test_type]
  258. test.setup_out_err(out, err)
  259. out.write(header("VERIFYING %s" % test_type.upper()))
  260. base_url = "http://%s:%s" % (self.benchmarker.server_host, self.port)
  261. try:
  262. results = test.verify(base_url)
  263. except Exception as e:
  264. results = [('fail',"""Caused Exception in TFB
  265. This almost certainly means your return value is incorrect,
  266. but also that you have found a bug. Please submit an issue
  267. including this message: %s\n%s""" % (e, traceback.format_exc()),
  268. base_url)]
  269. logging.warning("Verifying test %s for %s caused an exception: %s", test_type, self.name, e)
  270. traceback.format_exc()
  271. test.failed = any(result is 'fail' for (result, reason, url) in results)
  272. test.warned = any(result is 'warn' for (result, reason, url) in results)
  273. test.passed = all(result is 'pass' for (result, reason, url) in results)
  274. def output_result(result, reason, url):
  275. out.write(" %s for %s\n" % (result.upper(), url))
  276. print " %s for %s" % (result.upper(), url)
  277. if reason is not None and len(reason) != 0:
  278. for line in reason.splitlines():
  279. out.write(" " + line + '\n')
  280. print " " + line
  281. [output_result(r1,r2,url) for (r1, r2, url) in results]
  282. if test.failed:
  283. self.benchmarker.report_verify_results(self, test_type, 'fail')
  284. elif test.warned:
  285. self.benchmarker.report_verify_results(self, test_type, 'warn')
  286. elif test.passed:
  287. self.benchmarker.report_verify_results(self, test_type, 'pass')
  288. else:
  289. raise Exception("What the hell")
  290. result = True
  291. for test_type in self.runTests:
  292. verify_type(test_type)
  293. if self.runTests[test_type].failed:
  294. result = False
  295. return result
  296. ############################################################
  297. # End verify_urls
  298. ############################################################
  299. ############################################################
  300. # benchmark
  301. # Runs the benchmark for each type of test that it implements
  302. # JSON/DB/Query.
  303. ############################################################
  304. def benchmark(self, out, err):
  305. def benchmark_type(test_type):
  306. out.write("BENCHMARKING %s ... " % test_type.upper())
  307. test = self.runTests[test_type]
  308. test.setup_out_err(out, err)
  309. output_file = self.benchmarker.output_file(self.name, test_type)
  310. if not os.path.exists(output_file):
  311. # Open to create the empty file
  312. with open(output_file, 'w'):
  313. pass
  314. if test.passed:
  315. if test.requires_db:
  316. remote_script = self.__generate_query_script(test.get_url(), self.port, test.accept_header)
  317. else:
  318. remote_script = self.__generate_concurrency_script(test.get_url(), self.port, test.accept_header)
  319. self.__begin_logging(test_type)
  320. self.__run_benchmark(remote_script, output_file, err)
  321. self.__end_logging()
  322. results = self.__parse_test(test_type)
  323. print "Benchmark results:"
  324. pprint(results)
  325. self.benchmarker.report_benchmark_results(framework=self, test=test_type, results=results['results'])
  326. out.write( "Complete\n" )
  327. out.flush()
  328. for test_type in self.runTests:
  329. benchmark_type(test_type)
  330. ############################################################
  331. # End benchmark
  332. ############################################################
  333. ############################################################
  334. # parse_all
  335. # Method meant to be run for a given timestamp
  336. ############################################################
  337. def parse_all(self):
  338. for test_type in self.runTests:
  339. if os.path.exists(self.benchmarker.get_output_file(self.name, test_type)):
  340. results = self.__parse_test(test_type)
  341. self.benchmarker.report_benchmark_results(framework=self, test=test_type, results=results['results'])
  342. ############################################################
  343. # __parse_test(test_type)
  344. ############################################################
  345. def __parse_test(self, test_type):
  346. try:
  347. results = dict()
  348. results['results'] = []
  349. stats = []
  350. if os.path.exists(self.benchmarker.get_output_file(self.name, test_type)):
  351. with open(self.benchmarker.output_file(self.name, test_type)) as raw_data:
  352. is_warmup = True
  353. rawData = None
  354. for line in raw_data:
  355. if "Queries:" in line or "Concurrency:" in line:
  356. is_warmup = False
  357. rawData = None
  358. continue
  359. if "Warmup" in line or "Primer" in line:
  360. is_warmup = True
  361. continue
  362. if not is_warmup:
  363. if rawData == None:
  364. rawData = dict()
  365. results['results'].append(rawData)
  366. #if "Requests/sec:" in line:
  367. # m = re.search("Requests/sec:\s+([0-9]+)", line)
  368. # rawData['reportedResults'] = m.group(1)
  369. # search for weighttp data such as succeeded and failed.
  370. if "Latency" in line:
  371. m = re.findall("([0-9]+\.*[0-9]*[us|ms|s|m|%]+)", line)
  372. if len(m) == 4:
  373. rawData['latencyAvg'] = m[0]
  374. rawData['latencyStdev'] = m[1]
  375. rawData['latencyMax'] = m[2]
  376. # rawData['latencyStdevPercent'] = m[3]
  377. #if "Req/Sec" in line:
  378. # m = re.findall("([0-9]+\.*[0-9]*[k|%]*)", line)
  379. # if len(m) == 4:
  380. # rawData['requestsAvg'] = m[0]
  381. # rawData['requestsStdev'] = m[1]
  382. # rawData['requestsMax'] = m[2]
  383. # rawData['requestsStdevPercent'] = m[3]
  384. #if "requests in" in line:
  385. # m = re.search("requests in ([0-9]+\.*[0-9]*[ms|s|m|h]+)", line)
  386. # if m != None:
  387. # # parse out the raw time, which may be in minutes or seconds
  388. # raw_time = m.group(1)
  389. # if "ms" in raw_time:
  390. # rawData['total_time'] = float(raw_time[:len(raw_time)-2]) / 1000.0
  391. # elif "s" in raw_time:
  392. # rawData['total_time'] = float(raw_time[:len(raw_time)-1])
  393. # elif "m" in raw_time:
  394. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 60.0
  395. # elif "h" in raw_time:
  396. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 3600.0
  397. if "requests in" in line:
  398. m = re.search("([0-9]+) requests in", line)
  399. if m != None:
  400. rawData['totalRequests'] = int(m.group(1))
  401. if "Socket errors" in line:
  402. if "connect" in line:
  403. m = re.search("connect ([0-9]+)", line)
  404. rawData['connect'] = int(m.group(1))
  405. if "read" in line:
  406. m = re.search("read ([0-9]+)", line)
  407. rawData['read'] = int(m.group(1))
  408. if "write" in line:
  409. m = re.search("write ([0-9]+)", line)
  410. rawData['write'] = int(m.group(1))
  411. if "timeout" in line:
  412. m = re.search("timeout ([0-9]+)", line)
  413. rawData['timeout'] = int(m.group(1))
  414. if "Non-2xx" in line:
  415. m = re.search("Non-2xx or 3xx responses: ([0-9]+)", line)
  416. if m != None:
  417. rawData['5xx'] = int(m.group(1))
  418. if "STARTTIME" in line:
  419. m = re.search("[0-9]+", line)
  420. rawData["startTime"] = int(m.group(0))
  421. if "ENDTIME" in line:
  422. m = re.search("[0-9]+", line)
  423. rawData["endTime"] = int(m.group(0))
  424. test_stats = self.__parse_stats(test_type, rawData["startTime"], rawData["endTime"], 1)
  425. # rawData["averageStats"] = self.__calculate_average_stats(test_stats)
  426. stats.append(test_stats)
  427. with open(self.benchmarker.stats_file(self.name, test_type) + ".json", "w") as stats_file:
  428. json.dump(stats, stats_file)
  429. return results
  430. except IOError:
  431. return None
  432. ############################################################
  433. # End benchmark
  434. ############################################################
  435. ##########################################################################################
  436. # Private Methods
  437. ##########################################################################################
  438. ############################################################
  439. # __run_benchmark(script, output_file)
  440. # Runs a single benchmark using the script which is a bash
  441. # template that uses weighttp to run the test. All the results
  442. # outputed to the output_file.
  443. ############################################################
  444. def __run_benchmark(self, script, output_file, err):
  445. with open(output_file, 'w') as raw_file:
  446. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE, stdout=raw_file, stderr=err)
  447. p.communicate(script)
  448. err.flush()
  449. ############################################################
  450. # End __run_benchmark
  451. ############################################################
  452. ############################################################
  453. # __generate_concurrency_script(url, port)
  454. # Generates the string containing the bash script that will
  455. # be run on the client to benchmark a single test. This
  456. # specifically works for the variable concurrency tests (JSON
  457. # and DB)
  458. ############################################################
  459. def __generate_concurrency_script(self, url, port, accept_header, wrk_command="wrk", intervals=[], pipeline=""):
  460. if len(intervals) == 0:
  461. intervals = self.benchmarker.concurrency_levels
  462. headers = self.__get_request_headers(accept_header)
  463. return self.concurrency_template.format(max_concurrency=self.benchmarker.max_concurrency,
  464. max_threads=self.benchmarker.max_threads, name=self.name, duration=self.benchmarker.duration,
  465. interval=" ".join("{}".format(item) for item in intervals),
  466. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers, wrk=wrk_command,
  467. pipeline=pipeline)
  468. ############################################################
  469. # End __generate_concurrency_script
  470. ############################################################
  471. ############################################################
  472. # __generate_query_script(url, port)
  473. # Generates the string containing the bash script that will
  474. # be run on the client to benchmark a single test. This
  475. # specifically works for the variable query tests (Query)
  476. ############################################################
  477. def __generate_query_script(self, url, port, accept_header):
  478. headers = self.__get_request_headers(accept_header)
  479. return self.query_template.format(max_concurrency=self.benchmarker.max_concurrency,
  480. max_threads=self.benchmarker.max_threads, name=self.name, duration=self.benchmarker.duration,
  481. interval=" ".join("{}".format(item) for item in self.benchmarker.query_intervals),
  482. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers)
  483. ############################################################
  484. # End __generate_query_script
  485. ############################################################
  486. ############################################################
  487. # __get_request_headers(accept_header)
  488. # Generates the complete HTTP header string
  489. ############################################################
  490. def __get_request_headers(self, accept_header):
  491. return self.headers_template.format(accept=accept_header)
  492. ############################################################
  493. # End __format_request_headers
  494. ############################################################
  495. def requires_database(self):
  496. '''Returns True/False if this test requires a database'''
  497. return any(tobj.requires_db for (ttype,tobj) in self.runTests.iteritems())
  498. ############################################################
  499. # __begin_logging
  500. # Starts a thread to monitor the resource usage, to be synced with the client's time
  501. # TODO: MySQL and InnoDB are possible. Figure out how to implement them.
  502. ############################################################
  503. def __begin_logging(self, test_type):
  504. output_file = "{file_name}".format(file_name=self.benchmarker.get_stats_file(self.name, test_type))
  505. dstat_string = "dstat -afilmprsT --aio --fs --ipc --lock --raw --socket --tcp \
  506. --raw --socket --tcp --udp --unix --vm --disk-util \
  507. --rpc --rpcd --output {output_file}".format(output_file=output_file)
  508. cmd = shlex.split(dstat_string)
  509. dev_null = open(os.devnull, "w")
  510. self.subprocess_handle = subprocess.Popen(cmd, stdout=dev_null)
  511. ##############################################################
  512. # End __begin_logging
  513. ##############################################################
  514. ##############################################################
  515. # Begin __end_logging
  516. # Stops the logger thread and blocks until shutdown is complete.
  517. ##############################################################
  518. def __end_logging(self):
  519. self.subprocess_handle.terminate()
  520. self.subprocess_handle.communicate()
  521. ##############################################################
  522. # End __end_logging
  523. ##############################################################
  524. ##############################################################
  525. # Begin __parse_stats
  526. # For each test type, process all the statistics, and return a multi-layered dictionary
  527. # that has a structure as follows:
  528. # (timestamp)
  529. # | (main header) - group that the stat is in
  530. # | | (sub header) - title of the stat
  531. # | | | (stat) - the stat itself, usually a floating point number
  532. ##############################################################
  533. def __parse_stats(self, test_type, start_time, end_time, interval):
  534. stats_dict = dict()
  535. stats_file = self.benchmarker.stats_file(self.name, test_type)
  536. with open(stats_file) as stats:
  537. while(stats.next() != "\n"): # dstat doesn't output a completely compliant CSV file - we need to strip the header
  538. pass
  539. stats_reader = csv.reader(stats)
  540. main_header = stats_reader.next()
  541. sub_header = stats_reader.next()
  542. time_row = sub_header.index("epoch")
  543. int_counter = 0
  544. for row in stats_reader:
  545. time = float(row[time_row])
  546. int_counter+=1
  547. if time < start_time:
  548. continue
  549. elif time > end_time:
  550. return stats_dict
  551. if int_counter % interval != 0:
  552. continue
  553. row_dict = dict()
  554. for nextheader in main_header:
  555. if nextheader != "":
  556. row_dict[nextheader] = dict()
  557. header = ""
  558. for item_num, column in enumerate(row):
  559. if(len(main_header[item_num]) != 0):
  560. header = main_header[item_num]
  561. row_dict[header][sub_header[item_num]] = float(column) # all the stats are numbers, so we want to make sure that they stay that way in json
  562. stats_dict[time] = row_dict
  563. return stats_dict
  564. ##############################################################
  565. # End __parse_stats
  566. ##############################################################
  567. def __getattr__(self, name):
  568. """For backwards compatibility, we used to pass benchmarker
  569. as the argument to the setup.py files"""
  570. try:
  571. x = getattr(self.benchmarker, name)
  572. except AttributeError:
  573. print "AttributeError: %s not a member of FrameworkTest or Benchmarker" % name
  574. print "This is probably a bug"
  575. raise
  576. return x
  577. ##############################################################
  578. # Begin __calculate_average_stats
  579. # We have a large amount of raw data for the statistics that
  580. # may be useful for the stats nerds, but most people care about
  581. # a couple of numbers. For now, we're only going to supply:
  582. # * Average CPU
  583. # * Average Memory
  584. # * Total network use
  585. # * Total disk use
  586. # More may be added in the future. If they are, please update
  587. # the above list.
  588. # Note: raw_stats is directly from the __parse_stats method.
  589. # Recall that this consists of a dictionary of timestamps,
  590. # each of which contain a dictionary of stat categories which
  591. # contain a dictionary of stats
  592. ##############################################################
  593. def __calculate_average_stats(self, raw_stats):
  594. raw_stat_collection = dict()
  595. for timestamp, time_dict in raw_stats.items():
  596. for main_header, sub_headers in time_dict.items():
  597. item_to_append = None
  598. if 'cpu' in main_header:
  599. # We want to take the idl stat and subtract it from 100
  600. # to get the time that the CPU is NOT idle.
  601. item_to_append = sub_headers['idl'] - 100.0
  602. elif main_header == 'memory usage':
  603. item_to_append = sub_headers['used']
  604. elif 'net' in main_header:
  605. # Network stats have two parts - recieve and send. We'll use a tuple of
  606. # style (recieve, send)
  607. item_to_append = (sub_headers['recv'], sub_headers['send'])
  608. elif 'dsk' or 'io' in main_header:
  609. # Similar for network, except our tuple looks like (read, write)
  610. item_to_append = (sub_headers['read'], sub_headers['writ'])
  611. if item_to_append is not None:
  612. if main_header not in raw_stat_collection:
  613. raw_stat_collection[main_header] = list()
  614. raw_stat_collection[main_header].append(item_to_append)
  615. # Simple function to determine human readable size
  616. # http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
  617. def sizeof_fmt(num):
  618. # We'll assume that any number we get is convertable to a float, just in case
  619. num = float(num)
  620. for x in ['bytes','KB','MB','GB']:
  621. if num < 1024.0 and num > -1024.0:
  622. return "%3.1f%s" % (num, x)
  623. num /= 1024.0
  624. return "%3.1f%s" % (num, 'TB')
  625. # Now we have our raw stats in a readable format - we need to format it for display
  626. # We need a floating point sum, so the built in sum doesn't cut it
  627. display_stat_collection = dict()
  628. for header, values in raw_stat_collection.items():
  629. display_stat = None
  630. if 'cpu' in header:
  631. display_stat = sizeof_fmt(math.fsum(values) / len(values))
  632. elif main_header == 'memory usage':
  633. display_stat = sizeof_fmt(math.fsum(values) / len(values))
  634. elif 'net' in main_header:
  635. receive, send = zip(*values) # unzip
  636. display_stat = {'receive': sizeof_fmt(math.fsum(receive)), 'send': sizeof_fmt(math.fsum(send))}
  637. else: # if 'dsk' or 'io' in header:
  638. read, write = zip(*values) # unzip
  639. display_stat = {'read': sizeof_fmt(math.fsum(read)), 'write': sizeof_fmt(math.fsum(write))}
  640. display_stat_collection[header] = display_stat
  641. return display_stat
  642. ###########################################################################################
  643. # End __calculate_average_stats
  644. #########################################################################################
  645. ##########################################################################################
  646. # Constructor
  647. ##########################################################################################
  648. def __init__(self, name, directory, benchmarker, runTests, args):
  649. self.name = name
  650. self.directory = directory
  651. self.benchmarker = benchmarker
  652. self.runTests = runTests
  653. self.fwroot = benchmarker.fwroot
  654. # setup logging
  655. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  656. self.install_root="%s/%s" % (self.fwroot, "installs")
  657. if benchmarker.install_strategy is 'pertest':
  658. self.install_root="%s/pertest/%s" % (self.install_root, name)
  659. # Used in setup.py scripts for consistency with
  660. # the bash environment variables
  661. self.troot = self.directory
  662. self.iroot = self.install_root
  663. self.__dict__.update(args)
  664. # ensure directory has __init__.py file so that we can use it as a Python package
  665. if not os.path.exists(os.path.join(directory, "__init__.py")):
  666. logging.warning("Please add an empty __init__.py file to directory %s", directory)
  667. open(os.path.join(directory, "__init__.py"), 'w').close()
  668. # Import the module (TODO - consider using sys.meta_path)
  669. # Note: You can see the log output if you really want to, but it's a *ton*
  670. dir_rel_to_fwroot = os.path.relpath(os.path.dirname(directory), self.fwroot)
  671. if dir_rel_to_fwroot != ".":
  672. sys.path.append("%s/%s" % (self.fwroot, dir_rel_to_fwroot))
  673. logging.log(0, "Adding %s to import %s.%s", dir_rel_to_fwroot, os.path.basename(directory), self.setup_file)
  674. self.setup_module = setup_module = importlib.import_module(os.path.basename(directory) + '.' + self.setup_file)
  675. sys.path.remove("%s/%s" % (self.fwroot, dir_rel_to_fwroot))
  676. else:
  677. logging.log(0, "Importing %s.%s", directory, self.setup_file)
  678. self.setup_module = setup_module = importlib.import_module(os.path.basename(directory) + '.' + self.setup_file)
  679. ############################################################
  680. # End __init__
  681. ############################################################
  682. ############################################################
  683. # End FrameworkTest
  684. ############################################################
  685. ##########################################################################################
  686. # Static methods
  687. ##########################################################################################
  688. ##############################################################
  689. # parse_config(config, directory, benchmarker)
  690. # parses a config file and returns a list of FrameworkTest
  691. # objects based on that config file.
  692. ##############################################################
  693. def parse_config(config, directory, benchmarker):
  694. tests = []
  695. # The config object can specify multiple tests
  696. # Loop over them and parse each into a FrameworkTest
  697. for test in config['tests']:
  698. for test_name, test_keys in test.iteritems():
  699. # Prefix all test names with framework except 'default' test
  700. if test_name == 'default':
  701. test_name = config['framework']
  702. else:
  703. test_name = "%s-%s" % (config['framework'], test_name)
  704. # Ensure FrameworkTest.framework is available
  705. if not test_keys['framework']:
  706. test_keys['framework'] = config['framework']
  707. #if test_keys['framework'].lower() != config['framework'].lower():
  708. # print Exception("benchmark_config for test %s is invalid - test framework '%s' must match benchmark_config framework '%s'" %
  709. # (test_name, test_keys['framework'], config['framework']))
  710. # Confirm required keys are present
  711. # TODO have a TechEmpower person confirm this list - I don't know what the website requires....
  712. required = ['language','webserver','classification','database','approach','orm','framework','os','database_os']
  713. if not all (key in test_keys for key in required):
  714. raise Exception("benchmark_config for test %s is invalid - missing required keys" % test_name)
  715. # Map test type to a parsed FrameworkTestType object
  716. runTests = dict()
  717. for type_name, type_obj in benchmarker.types.iteritems():
  718. try:
  719. runTests[type_name] = type_obj.copy().parse(test_keys)
  720. except AttributeError as ae:
  721. # This is quite common - most tests don't support all types
  722. # Quitely log it and move on
  723. logging.debug("Missing arguments for test type %s for framework test %s", type_name, test_name)
  724. pass
  725. # By passing the entire set of keys, each FrameworkTest will have a member for each key
  726. tests.append(FrameworkTest(test_name, directory, benchmarker, runTests, test_keys))
  727. return tests
  728. ##############################################################
  729. # End parse_config
  730. ##############################################################