framework_test.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 collections import OrderedDict
  18. from threading import Thread
  19. from threading import Event
  20. from utils import header
  21. from datetime import datetime
  22. from datetime import timedelta
  23. from threading import Thread
  24. from Queue import Queue, Empty
  25. class NonBlockingStreamReader:
  26. def __init__(self, stream, eof_message = None):
  27. '''
  28. stream: the stream to read from.
  29. Usually a process' stdout or stderr.
  30. '''
  31. self._s = stream
  32. self._q = Queue()
  33. self._eof_message = eof_message
  34. def _populateQueue(stream, queue):
  35. '''
  36. Collect lines from 'stream' and put them in 'queue'.
  37. '''
  38. while True:
  39. line = stream.readline()
  40. if line:
  41. queue.put(line)
  42. else:
  43. if self._eof_message:
  44. sys.stdout.write(self._eof_message + '\n')
  45. return
  46. self._t = Thread(target = _populateQueue,
  47. args = (self._s, self._q))
  48. self._t.daemon = True
  49. self._t.start() #start collecting lines from the stream
  50. def readline(self, timeout = None):
  51. try:
  52. return self._q.get(block = timeout is not None,
  53. timeout = timeout)
  54. except Empty:
  55. return None
  56. class FrameworkTest:
  57. headers_template = "-H 'Host: localhost' -H '{accept}' -H 'Connection: keep-alive'"
  58. # Used for test types that require no pipelining or query string params.
  59. concurrency_template = """
  60. echo ""
  61. echo "---------------------------------------------------------"
  62. echo " Running Primer {name}"
  63. echo " {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 \"http://{server_host}:{port}{url}\""
  64. echo "---------------------------------------------------------"
  65. echo ""
  66. {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 "http://{server_host}:{port}{url}"
  67. sleep 5
  68. echo ""
  69. echo "---------------------------------------------------------"
  70. echo " Running Warmup {name}"
  71. echo " {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}\""
  72. echo "---------------------------------------------------------"
  73. echo ""
  74. {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}"
  75. sleep 5
  76. echo ""
  77. echo "---------------------------------------------------------"
  78. echo " Synchronizing time"
  79. echo "---------------------------------------------------------"
  80. echo ""
  81. ntpdate -s pool.ntp.org
  82. for c in {levels}
  83. do
  84. echo ""
  85. echo "---------------------------------------------------------"
  86. echo " Concurrency: $c for {name}"
  87. echo " {wrk} {headers} -d {duration} -c $c --timeout $c -t $(($c>{max_threads}?{max_threads}:$c)) \"http://{server_host}:{port}{url}\""
  88. echo "---------------------------------------------------------"
  89. echo ""
  90. STARTTIME=$(date +"%s")
  91. {wrk} {headers} -d {duration} -c $c --timeout $c -t "$(($c>{max_threads}?{max_threads}:$c))" http://{server_host}:{port}{url}
  92. echo "STARTTIME $STARTTIME"
  93. echo "ENDTIME $(date +"%s")"
  94. sleep 2
  95. done
  96. """
  97. # Used for test types that require pipelining.
  98. pipeline_template = """
  99. echo ""
  100. echo "---------------------------------------------------------"
  101. echo " Running Primer {name}"
  102. echo " {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 \"http://{server_host}:{port}{url}\""
  103. echo "---------------------------------------------------------"
  104. echo ""
  105. {wrk} {headers} -d 5 -c 8 --timeout 8 -t 8 "http://{server_host}:{port}{url}"
  106. sleep 5
  107. echo ""
  108. echo "---------------------------------------------------------"
  109. echo " Running Warmup {name}"
  110. echo " {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}\""
  111. echo "---------------------------------------------------------"
  112. echo ""
  113. {wrk} {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}"
  114. sleep 5
  115. echo ""
  116. echo "---------------------------------------------------------"
  117. echo " Synchronizing time"
  118. echo "---------------------------------------------------------"
  119. echo ""
  120. ntpdate -s pool.ntp.org
  121. for c in {levels}
  122. do
  123. echo ""
  124. echo "---------------------------------------------------------"
  125. echo " Concurrency: $c for {name}"
  126. 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}"
  127. echo "---------------------------------------------------------"
  128. echo ""
  129. STARTTIME=$(date +"%s")
  130. {wrk} {headers} -d {duration} -c $c --timeout $c -t "$(($c>{max_threads}?{max_threads}:$c))" http://{server_host}:{port}{url} -s ~/pipeline.lua -- {pipeline}
  131. echo "STARTTIME $STARTTIME"
  132. echo "ENDTIME $(date +"%s")"
  133. sleep 2
  134. done
  135. """
  136. # Used for test types that require a database -
  137. # These tests run at a static concurrency level and vary the size of
  138. # the query sent with each request
  139. query_template = """
  140. echo ""
  141. echo "---------------------------------------------------------"
  142. echo " Running Primer {name}"
  143. echo " wrk {headers} -d 5 -c 8 --timeout 8 -t 8 \"http://{server_host}:{port}{url}2\""
  144. echo "---------------------------------------------------------"
  145. echo ""
  146. wrk {headers} -d 5 -c 8 --timeout 8 -t 8 "http://{server_host}:{port}{url}2"
  147. sleep 5
  148. echo ""
  149. echo "---------------------------------------------------------"
  150. echo " Running Warmup {name}"
  151. echo " wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}2\""
  152. echo "---------------------------------------------------------"
  153. echo ""
  154. wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}2"
  155. sleep 5
  156. echo ""
  157. echo "---------------------------------------------------------"
  158. echo " Synchronizing time"
  159. echo "---------------------------------------------------------"
  160. echo ""
  161. ntpdate -s pool.ntp.org
  162. for c in {levels}
  163. do
  164. echo ""
  165. echo "---------------------------------------------------------"
  166. echo " Queries: $c for {name}"
  167. echo " wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}$c\""
  168. echo "---------------------------------------------------------"
  169. echo ""
  170. STARTTIME=$(date +"%s")
  171. wrk {headers} -d {duration} -c {max_concurrency} --timeout {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}$c"
  172. echo "STARTTIME $STARTTIME"
  173. echo "ENDTIME $(date +"%s")"
  174. sleep 2
  175. done
  176. """
  177. ############################################################
  178. # start(benchmarker)
  179. # Start the test using it's setup file
  180. ############################################################
  181. def start(self, out, err):
  182. # Load profile for this installation
  183. profile="$FWROOT/config/benchmark_profile"
  184. # Setup variables for TROOT and IROOT
  185. setup_util.replace_environ(config=profile,
  186. command='export TROOT=%s && export IROOT=%s && export DBHOST=%s && export MAX_THREADS=%s && export OUT=%s && export ERR=%s' %
  187. (self.directory, self.install_root, self.database_host, self.benchmarker.threads, os.path.join(self.fwroot, out.name), os.path.join(self.fwroot, err.name)))
  188. # Run the module start (inside parent of TROOT)
  189. # - we use the parent as a historical accident - a lot of tests
  190. # use subprocess's cwd argument already
  191. previousDir = os.getcwd()
  192. os.chdir(os.path.dirname(self.troot))
  193. logging.info("Running setup module start (cwd=%s)", self.directory)
  194. # Run the start script for the test as the "testrunner" user.
  195. #
  196. # `sudo` - Switching user requires superuser privs
  197. # -u [username] The username
  198. # -E Preserves the current environment variables
  199. # -H Forces the home var (~) to be reset to the user specified
  200. # `stdbuf` - Disable buffering, send output to python ASAP
  201. # -o0 zero-sized buffer for stdout
  202. # -e0 zero-sized buffer for stderr
  203. # `bash` - Run the setup.sh script using bash
  204. # -e Force bash to exit on first error
  205. # -x Turn on bash tracing e.g. print commands before running
  206. #
  207. # Most servers do not output to stdout/stderr while
  208. # serving requests so there is no performance hit from disabling
  209. # output buffering. Disabling is necessary to
  210. # a) allowing us to show output in real time b) avoiding lost
  211. # output in the buffer when the testrunner user is forcibly killed
  212. # See http://www.pixelbeat.org/programming/stdio_buffering/
  213. command = 'sudo -u %s -E -H stdbuf -o0 -e0 bash -ex %s.sh' % (self.benchmarker.runner_user, self.setup_file)
  214. debug_command = '''\
  215. export FWROOT=%s && \\
  216. export TROOT=%s && \\
  217. export IROOT=%s && \\
  218. export DBHOST=%s && \\
  219. export MAX_THREADS=%s && \\
  220. export OUT=%s && \\
  221. export ERR=%s && \\
  222. cd %s && \\
  223. %s''' % (self.fwroot,
  224. self.directory,
  225. self.install_root,
  226. self.database_host,
  227. self.benchmarker.threads,
  228. os.path.join(self.fwroot, out.name),
  229. os.path.join(self.fwroot, err.name),
  230. self.directory,
  231. command)
  232. logging.info("To run framework manually, copy/paste this:\n%s", debug_command)
  233. def tee_output(prefix, line):
  234. # Log to current terminal
  235. # Needs to be one atomic write, so we join because
  236. # list operations are faster than string concat
  237. sys.stdout.write(u''.encode('utf-8').join([prefix, line]))
  238. sys.stdout.flush()
  239. # logging.error("".join([prefix, line]))
  240. # Goal: Stream output of both benchmark toolset and
  241. # server to the console and to a file
  242. # Problem: Capturing output of subprocess and children
  243. # Solution: Use pipes provided by python
  244. # Future-proof: Add unit tests that ensure this code works in all situations
  245. #
  246. # https://blogs.gnome.org/markmc/2013/06/04/async-io-and-python/
  247. # http://eyalarubas.com/python-subproc-nonblock.html
  248. p = subprocess.Popen(command, cwd=self.directory,
  249. shell=True, stdout=subprocess.PIPE, bufsize=0,
  250. stderr=subprocess.STDOUT)
  251. nbsr = NonBlockingStreamReader(p.stdout, "Processes for %s have terminated" % self.name)
  252. timeout = datetime.now() + timedelta(minutes = 10)
  253. time_remaining = timeout - datetime.now()
  254. # Flush output until setup.sh process is finished. This is
  255. # either a) when setup.sh exits b) when the port is bound
  256. # c) when we run out of time
  257. #
  258. # Note: child processes forked using & will still be alive
  259. # and directing their output to the pipes. E.g. even after
  260. # this loop dies the pipes are used to capture stdout/err from
  261. # the running server
  262. #
  263. # Explicitly set our prefix encoding
  264. prefix = (u"Setup %s: " % self.name).encode('utf-8')
  265. while not (p.poll()
  266. or self.benchmarker.is_port_bound(self.port)
  267. or time_remaining.total_seconds() < 0):
  268. # The conditions above are slow to check, so
  269. # we miss many lines of output if we only
  270. # print one line per condition check. Adding a
  271. # tight loop here mitigates the effect
  272. for i in xrange(10):
  273. line = nbsr.readline(0.05)
  274. if line:
  275. tee_output(prefix, line)
  276. time_remaining = timeout - datetime.now()
  277. # Were we timed out?
  278. if time_remaining.total_seconds() < 0:
  279. print "Setup.sh timed out!!"
  280. p.kill()
  281. return 1
  282. # If setup.sh exited, use the return code
  283. # Else return 0 if the port was bound
  284. retcode = (p.poll() or 0 if self.benchmarker.is_port_bound(self.port) else 1)
  285. if p.poll():
  286. print "Setup.sh exited with %s" % p.poll()
  287. if self.benchmarker.is_port_bound(self.port):
  288. print "Setup.sh exited due to bound port"
  289. # Before we return control to the benchmarker, spin up a
  290. # thread to keep an eye on the pipes in case the server
  291. # spits anything to stdout/stderr
  292. # TODO add exit condition
  293. def watch_child_pipes(nbsr, prefix):
  294. while True:
  295. line = nbsr.readline(0.1)
  296. if line:
  297. tee_output(prefix, line)
  298. prefix = (u"Server %s: " % self.name).encode('utf-8')
  299. watch_thread = Thread(target = watch_child_pipes,
  300. args = (nbsr, prefix))
  301. watch_thread.daemon = True
  302. watch_thread.start()
  303. logging.info("Executed %s.sh, returning %s", self.setup_file, retcode)
  304. os.chdir(previousDir)
  305. return retcode
  306. ############################################################
  307. # End start
  308. ############################################################
  309. ############################################################
  310. # verify_urls
  311. # Verifys each of the URLs for this test. THis will sinply
  312. # curl the URL and check for it's return status.
  313. # For each url, a flag will be set on this object for whether
  314. # or not it passed
  315. # Returns True if all verifications succeeded
  316. ############################################################
  317. def verify_urls(self, out, err):
  318. result = True
  319. def verify_type(test_type):
  320. test = self.runTests[test_type]
  321. test.setup_out_err(out, err)
  322. out.write(header("VERIFYING %s" % test_type.upper()))
  323. base_url = "http://%s:%s" % (self.benchmarker.server_host, self.port)
  324. try:
  325. results = test.verify(base_url)
  326. except Exception as e:
  327. results = [('fail',"""Caused Exception in TFB
  328. This almost certainly means your return value is incorrect,
  329. but also that you have found a bug. Please submit an issue
  330. including this message: %s\n%s""" % (e, traceback.format_exc()),
  331. base_url)]
  332. logging.warning("Verifying test %s for %s caused an exception: %s", test_type, self.name, e)
  333. traceback.format_exc()
  334. test.failed = any(result is 'fail' for (result, reason, url) in results)
  335. test.warned = any(result is 'warn' for (result, reason, url) in results)
  336. test.passed = all(result is 'pass' for (result, reason, url) in results)
  337. def output_result(result, reason, url):
  338. out.write(" %s for %s\n" % (result.upper(), url))
  339. print " %s for %s" % (result.upper(), url)
  340. if reason is not None and len(reason) != 0:
  341. for line in reason.splitlines():
  342. out.write(" " + line + '\n')
  343. print " " + line
  344. [output_result(r1,r2,url) for (r1, r2, url) in results]
  345. if test.failed:
  346. self.benchmarker.report_verify_results(self, test_type, 'fail')
  347. elif test.warned:
  348. self.benchmarker.report_verify_results(self, test_type, 'warn')
  349. elif test.passed:
  350. self.benchmarker.report_verify_results(self, test_type, 'pass')
  351. else:
  352. raise Exception("Unknown error - test did not pass,warn,or fail")
  353. result = True
  354. for test_type in self.runTests:
  355. verify_type(test_type)
  356. if self.runTests[test_type].failed:
  357. result = False
  358. return result
  359. ############################################################
  360. # End verify_urls
  361. ############################################################
  362. ############################################################
  363. # benchmark
  364. # Runs the benchmark for each type of test that it implements
  365. # JSON/DB/Query.
  366. ############################################################
  367. def benchmark(self, out, err):
  368. def benchmark_type(test_type):
  369. out.write("BENCHMARKING %s ... " % test_type.upper())
  370. test = self.runTests[test_type]
  371. test.setup_out_err(out, err)
  372. output_file = self.benchmarker.output_file(self.name, test_type)
  373. if not os.path.exists(output_file):
  374. # Open to create the empty file
  375. with open(output_file, 'w'):
  376. pass
  377. if not test.failed:
  378. if test_type == 'plaintext': # One special case
  379. remote_script = self.__generate_pipeline_script(test.get_url(), self.port, test.accept_header)
  380. elif test_type == 'query' or test_type == 'update':
  381. remote_script = self.__generate_query_script(test.get_url(), self.port, test.accept_header)
  382. else:
  383. remote_script = self.__generate_concurrency_script(test.get_url(), self.port, test.accept_header)
  384. # Begin resource usage metrics collection
  385. self.__begin_logging(test_type)
  386. # Run the benchmark
  387. with open(output_file, 'w') as raw_file:
  388. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE, stdout=raw_file, stderr=err)
  389. p.communicate(remote_script)
  390. err.flush()
  391. # End resource usage metrics collection
  392. self.__end_logging()
  393. results = self.__parse_test(test_type)
  394. print "Benchmark results:"
  395. pprint(results)
  396. self.benchmarker.report_benchmark_results(framework=self, test=test_type, results=results['results'])
  397. out.write( "Complete\n" )
  398. out.flush()
  399. for test_type in self.runTests:
  400. benchmark_type(test_type)
  401. ############################################################
  402. # End benchmark
  403. ############################################################
  404. ############################################################
  405. # parse_all
  406. # Method meant to be run for a given timestamp
  407. ############################################################
  408. def parse_all(self):
  409. for test_type in self.runTests:
  410. if os.path.exists(self.benchmarker.get_output_file(self.name, test_type)):
  411. results = self.__parse_test(test_type)
  412. self.benchmarker.report_benchmark_results(framework=self, test=test_type, results=results['results'])
  413. ############################################################
  414. # __parse_test(test_type)
  415. ############################################################
  416. def __parse_test(self, test_type):
  417. try:
  418. results = dict()
  419. results['results'] = []
  420. stats = []
  421. if os.path.exists(self.benchmarker.get_output_file(self.name, test_type)):
  422. with open(self.benchmarker.output_file(self.name, test_type)) as raw_data:
  423. is_warmup = True
  424. rawData = None
  425. for line in raw_data:
  426. if "Queries:" in line or "Concurrency:" in line:
  427. is_warmup = False
  428. rawData = None
  429. continue
  430. if "Warmup" in line or "Primer" in line:
  431. is_warmup = True
  432. continue
  433. if not is_warmup:
  434. if rawData == None:
  435. rawData = dict()
  436. results['results'].append(rawData)
  437. #if "Requests/sec:" in line:
  438. # m = re.search("Requests/sec:\s+([0-9]+)", line)
  439. # rawData['reportedResults'] = m.group(1)
  440. # search for weighttp data such as succeeded and failed.
  441. if "Latency" in line:
  442. m = re.findall("([0-9]+\.*[0-9]*[us|ms|s|m|%]+)", line)
  443. if len(m) == 4:
  444. rawData['latencyAvg'] = m[0]
  445. rawData['latencyStdev'] = m[1]
  446. rawData['latencyMax'] = m[2]
  447. # rawData['latencyStdevPercent'] = m[3]
  448. #if "Req/Sec" in line:
  449. # m = re.findall("([0-9]+\.*[0-9]*[k|%]*)", line)
  450. # if len(m) == 4:
  451. # rawData['requestsAvg'] = m[0]
  452. # rawData['requestsStdev'] = m[1]
  453. # rawData['requestsMax'] = m[2]
  454. # rawData['requestsStdevPercent'] = m[3]
  455. #if "requests in" in line:
  456. # m = re.search("requests in ([0-9]+\.*[0-9]*[ms|s|m|h]+)", line)
  457. # if m != None:
  458. # # parse out the raw time, which may be in minutes or seconds
  459. # raw_time = m.group(1)
  460. # if "ms" in raw_time:
  461. # rawData['total_time'] = float(raw_time[:len(raw_time)-2]) / 1000.0
  462. # elif "s" in raw_time:
  463. # rawData['total_time'] = float(raw_time[:len(raw_time)-1])
  464. # elif "m" in raw_time:
  465. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 60.0
  466. # elif "h" in raw_time:
  467. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 3600.0
  468. if "requests in" in line:
  469. m = re.search("([0-9]+) requests in", line)
  470. if m != None:
  471. rawData['totalRequests'] = int(m.group(1))
  472. if "Socket errors" in line:
  473. if "connect" in line:
  474. m = re.search("connect ([0-9]+)", line)
  475. rawData['connect'] = int(m.group(1))
  476. if "read" in line:
  477. m = re.search("read ([0-9]+)", line)
  478. rawData['read'] = int(m.group(1))
  479. if "write" in line:
  480. m = re.search("write ([0-9]+)", line)
  481. rawData['write'] = int(m.group(1))
  482. if "timeout" in line:
  483. m = re.search("timeout ([0-9]+)", line)
  484. rawData['timeout'] = int(m.group(1))
  485. if "Non-2xx" in line:
  486. m = re.search("Non-2xx or 3xx responses: ([0-9]+)", line)
  487. if m != None:
  488. rawData['5xx'] = int(m.group(1))
  489. if "STARTTIME" in line:
  490. m = re.search("[0-9]+", line)
  491. rawData["startTime"] = int(m.group(0))
  492. if "ENDTIME" in line:
  493. m = re.search("[0-9]+", line)
  494. rawData["endTime"] = int(m.group(0))
  495. test_stats = self.__parse_stats(test_type, rawData["startTime"], rawData["endTime"], 1)
  496. # rawData["averageStats"] = self.__calculate_average_stats(test_stats)
  497. stats.append(test_stats)
  498. with open(self.benchmarker.stats_file(self.name, test_type) + ".json", "w") as stats_file:
  499. json.dump(stats, stats_file, indent=2)
  500. return results
  501. except IOError:
  502. return None
  503. ############################################################
  504. # End benchmark
  505. ############################################################
  506. ##########################################################################################
  507. # Private Methods
  508. ##########################################################################################
  509. ############################################################
  510. # __generate_concurrency_script(url, port)
  511. # Generates the string containing the bash script that will
  512. # be run on the client to benchmark a single test. This
  513. # specifically works for the variable concurrency tests (JSON
  514. # and DB)
  515. ############################################################
  516. def __generate_concurrency_script(self, url, port, accept_header, wrk_command="wrk"):
  517. headers = self.headers_template.format(accept=accept_header)
  518. return self.concurrency_template.format(max_concurrency=max(self.benchmarker.concurrency_levels),
  519. max_threads=self.benchmarker.threads, name=self.name, duration=self.benchmarker.duration,
  520. levels=" ".join("{}".format(item) for item in self.benchmarker.concurrency_levels),
  521. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers, wrk=wrk_command)
  522. ############################################################
  523. # __generate_pipeline_script(url, port)
  524. # Generates the string containing the bash script that will
  525. # be run on the client to benchmark a single pipeline test.
  526. ############################################################
  527. def __generate_pipeline_script(self, url, port, accept_header, wrk_command="wrk"):
  528. headers = self.headers_template.format(accept=accept_header)
  529. return self.pipeline_template.format(max_concurrency=16384,
  530. max_threads=self.benchmarker.threads, name=self.name, duration=self.benchmarker.duration,
  531. levels=" ".join("{}".format(item) for item in [256,1024,4096,16384]),
  532. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers, wrk=wrk_command,
  533. pipeline=16)
  534. ############################################################
  535. # __generate_query_script(url, port)
  536. # Generates the string containing the bash script that will
  537. # be run on the client to benchmark a single test. This
  538. # specifically works for the variable query tests (Query)
  539. ############################################################
  540. def __generate_query_script(self, url, port, accept_header):
  541. headers = self.headers_template.format(accept=accept_header)
  542. return self.query_template.format(max_concurrency=max(self.benchmarker.concurrency_levels),
  543. max_threads=self.benchmarker.threads, name=self.name, duration=self.benchmarker.duration,
  544. levels=" ".join("{}".format(item) for item in self.benchmarker.query_levels),
  545. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers)
  546. ############################################################
  547. # Returns True if any test type this this framework test will use a DB
  548. ############################################################
  549. def requires_database(self):
  550. '''Returns True/False if this test requires a database'''
  551. return any(tobj.requires_db for (ttype,tobj) in self.runTests.iteritems())
  552. ############################################################
  553. # __begin_logging
  554. # Starts a thread to monitor the resource usage, to be synced with the client's time
  555. # TODO: MySQL and InnoDB are possible. Figure out how to implement them.
  556. ############################################################
  557. def __begin_logging(self, test_type):
  558. output_file = "{file_name}".format(file_name=self.benchmarker.get_stats_file(self.name, test_type))
  559. dstat_string = "dstat -afilmprsT --aio --fs --ipc --lock --raw --socket --tcp \
  560. --raw --socket --tcp --udp --unix --vm --disk-util \
  561. --rpc --rpcd --output {output_file}".format(output_file=output_file)
  562. cmd = shlex.split(dstat_string)
  563. dev_null = open(os.devnull, "w")
  564. self.subprocess_handle = subprocess.Popen(cmd, stdout=dev_null)
  565. ##############################################################
  566. # Begin __end_logging
  567. # Stops the logger thread and blocks until shutdown is complete.
  568. ##############################################################
  569. def __end_logging(self):
  570. self.subprocess_handle.terminate()
  571. self.subprocess_handle.communicate()
  572. ##############################################################
  573. # Begin __parse_stats
  574. # For each test type, process all the statistics, and return a multi-layered dictionary
  575. # that has a structure as follows:
  576. # (timestamp)
  577. # | (main header) - group that the stat is in
  578. # | | (sub header) - title of the stat
  579. # | | | (stat) - the stat itself, usually a floating point number
  580. ##############################################################
  581. def __parse_stats(self, test_type, start_time, end_time, interval):
  582. stats_dict = dict()
  583. stats_file = self.benchmarker.stats_file(self.name, test_type)
  584. with open(stats_file) as stats:
  585. while(stats.next() != "\n"): # dstat doesn't output a completely compliant CSV file - we need to strip the header
  586. pass
  587. stats_reader = csv.reader(stats)
  588. main_header = stats_reader.next()
  589. sub_header = stats_reader.next()
  590. time_row = sub_header.index("epoch")
  591. int_counter = 0
  592. for row in stats_reader:
  593. time = float(row[time_row])
  594. int_counter+=1
  595. if time < start_time:
  596. continue
  597. elif time > end_time:
  598. return stats_dict
  599. if int_counter % interval != 0:
  600. continue
  601. row_dict = dict()
  602. for nextheader in main_header:
  603. if nextheader != "":
  604. row_dict[nextheader] = dict()
  605. header = ""
  606. for item_num, column in enumerate(row):
  607. if(len(main_header[item_num]) != 0):
  608. header = main_header[item_num]
  609. 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
  610. stats_dict[time] = row_dict
  611. return stats_dict
  612. ##############################################################
  613. # End __parse_stats
  614. ##############################################################
  615. def __getattr__(self, name):
  616. """For backwards compatibility, we used to pass benchmarker
  617. as the argument to the setup.sh files"""
  618. try:
  619. x = getattr(self.benchmarker, name)
  620. except AttributeError:
  621. print "AttributeError: %s not a member of FrameworkTest or Benchmarker" % name
  622. print "This is probably a bug"
  623. raise
  624. return x
  625. ##############################################################
  626. # Begin __calculate_average_stats
  627. # We have a large amount of raw data for the statistics that
  628. # may be useful for the stats nerds, but most people care about
  629. # a couple of numbers. For now, we're only going to supply:
  630. # * Average CPU
  631. # * Average Memory
  632. # * Total network use
  633. # * Total disk use
  634. # More may be added in the future. If they are, please update
  635. # the above list.
  636. # Note: raw_stats is directly from the __parse_stats method.
  637. # Recall that this consists of a dictionary of timestamps,
  638. # each of which contain a dictionary of stat categories which
  639. # contain a dictionary of stats
  640. ##############################################################
  641. def __calculate_average_stats(self, raw_stats):
  642. raw_stat_collection = dict()
  643. for timestamp, time_dict in raw_stats.items():
  644. for main_header, sub_headers in time_dict.items():
  645. item_to_append = None
  646. if 'cpu' in main_header:
  647. # We want to take the idl stat and subtract it from 100
  648. # to get the time that the CPU is NOT idle.
  649. item_to_append = sub_headers['idl'] - 100.0
  650. elif main_header == 'memory usage':
  651. item_to_append = sub_headers['used']
  652. elif 'net' in main_header:
  653. # Network stats have two parts - recieve and send. We'll use a tuple of
  654. # style (recieve, send)
  655. item_to_append = (sub_headers['recv'], sub_headers['send'])
  656. elif 'dsk' or 'io' in main_header:
  657. # Similar for network, except our tuple looks like (read, write)
  658. item_to_append = (sub_headers['read'], sub_headers['writ'])
  659. if item_to_append is not None:
  660. if main_header not in raw_stat_collection:
  661. raw_stat_collection[main_header] = list()
  662. raw_stat_collection[main_header].append(item_to_append)
  663. # Simple function to determine human readable size
  664. # http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
  665. def sizeof_fmt(num):
  666. # We'll assume that any number we get is convertable to a float, just in case
  667. num = float(num)
  668. for x in ['bytes','KB','MB','GB']:
  669. if num < 1024.0 and num > -1024.0:
  670. return "%3.1f%s" % (num, x)
  671. num /= 1024.0
  672. return "%3.1f%s" % (num, 'TB')
  673. # Now we have our raw stats in a readable format - we need to format it for display
  674. # We need a floating point sum, so the built in sum doesn't cut it
  675. display_stat_collection = dict()
  676. for header, values in raw_stat_collection.items():
  677. display_stat = None
  678. if 'cpu' in header:
  679. display_stat = sizeof_fmt(math.fsum(values) / len(values))
  680. elif main_header == 'memory usage':
  681. display_stat = sizeof_fmt(math.fsum(values) / len(values))
  682. elif 'net' in main_header:
  683. receive, send = zip(*values) # unzip
  684. display_stat = {'receive': sizeof_fmt(math.fsum(receive)), 'send': sizeof_fmt(math.fsum(send))}
  685. else: # if 'dsk' or 'io' in header:
  686. read, write = zip(*values) # unzip
  687. display_stat = {'read': sizeof_fmt(math.fsum(read)), 'write': sizeof_fmt(math.fsum(write))}
  688. display_stat_collection[header] = display_stat
  689. return display_stat
  690. ###########################################################################################
  691. # End __calculate_average_stats
  692. #########################################################################################
  693. ##########################################################################################
  694. # Constructor
  695. ##########################################################################################
  696. def __init__(self, name, directory, benchmarker, runTests, args):
  697. self.name = name
  698. self.directory = directory
  699. self.benchmarker = benchmarker
  700. self.runTests = runTests
  701. self.fwroot = benchmarker.fwroot
  702. self.approach = ""
  703. self.classification = ""
  704. self.database = ""
  705. self.framework = ""
  706. self.language = ""
  707. self.orm = ""
  708. self.platform = ""
  709. self.webserver = ""
  710. self.os = ""
  711. self.database_os = ""
  712. self.display_name = ""
  713. self.notes = ""
  714. self.versus = ""
  715. # setup logging
  716. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  717. self.install_root="%s/%s" % (self.fwroot, "installs")
  718. if benchmarker.install_strategy is 'pertest':
  719. self.install_root="%s/pertest/%s" % (self.install_root, name)
  720. # Used in setup.sh scripts for consistency with
  721. # the bash environment variables
  722. self.troot = self.directory
  723. self.iroot = self.install_root
  724. self.__dict__.update(args)
  725. ############################################################
  726. # End __init__
  727. ############################################################
  728. ############################################################
  729. # End FrameworkTest
  730. ############################################################
  731. ##########################################################################################
  732. # Static methods
  733. ##########################################################################################
  734. ##############################################################
  735. # parse_config(config, directory, benchmarker)
  736. # parses a config file and returns a list of FrameworkTest
  737. # objects based on that config file.
  738. ##############################################################
  739. def parse_config(config, directory, benchmarker):
  740. tests = []
  741. # This sort ordering is set up specifically to return the length
  742. # of the test name. There were SO many problems involved with
  743. # 'plaintext' being run first (rather, just not last) that we
  744. # needed to ensure that it was run last for every framework.
  745. def testOrder(type_name):
  746. return len(type_name)
  747. # The config object can specify multiple tests
  748. # Loop over them and parse each into a FrameworkTest
  749. for test in config['tests']:
  750. names = [name for (name,keys) in test.iteritems()]
  751. if "default" not in names:
  752. logging.warn("Framework %s does not define a default test in benchmark_config", config['framework'])
  753. for test_name, test_keys in test.iteritems():
  754. # Prefix all test names with framework except 'default' test
  755. if test_name == 'default':
  756. test_name = config['framework']
  757. else:
  758. test_name = "%s-%s" % (config['framework'], test_name)
  759. # Ensure FrameworkTest.framework is available
  760. if not test_keys['framework']:
  761. test_keys['framework'] = config['framework']
  762. #if test_keys['framework'].lower() != config['framework'].lower():
  763. # print Exception("benchmark_config for test %s is invalid - test framework '%s' must match benchmark_config framework '%s'" %
  764. # (test_name, test_keys['framework'], config['framework']))
  765. # Confirm required keys are present
  766. # TODO have a TechEmpower person confirm this list - I don't know what the website requires....
  767. required = ['language','webserver','classification','database','approach','orm','framework','os','database_os']
  768. if not all (key in test_keys for key in required):
  769. raise Exception("benchmark_config for test %s is invalid - missing required keys" % test_name)
  770. # Map test type to a parsed FrameworkTestType object
  771. runTests = dict()
  772. for type_name, type_obj in benchmarker.types.iteritems():
  773. try:
  774. runTests[type_name] = type_obj.copy().parse(test_keys)
  775. except AttributeError as ae:
  776. # This is quite common - most tests don't support all types
  777. # Quitely log it and move on (debug logging is on in travis and this causes
  778. # ~1500 lines of debug, so I'm totally ignoring it for now
  779. # logging.debug("Missing arguments for test type %s for framework test %s", type_name, test_name)
  780. pass
  781. # We need to sort by test_type to run
  782. sortedTestKeys = sorted(runTests.keys(), key=testOrder)
  783. sortedRunTests = OrderedDict()
  784. for sortedTestKey in sortedTestKeys:
  785. sortedRunTests[sortedTestKey] = runTests[sortedTestKey]
  786. # By passing the entire set of keys, each FrameworkTest will have a member for each key
  787. tests.append(FrameworkTest(test_name, directory, benchmarker, sortedRunTests, test_keys))
  788. return tests
  789. ##############################################################
  790. # End parse_config
  791. ##############################################################