framework_test.py 34 KB

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