framework_test.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import importlib
  2. import os
  3. import subprocess
  4. import time
  5. import re
  6. import pprint
  7. import sys
  8. import traceback
  9. class FrameworkTest:
  10. ##########################################################################################
  11. # Class variables
  12. ##########################################################################################
  13. headers_template = "-H 'Host: localhost' -H '{accept}' -H 'Connection: keep-alive'"
  14. headers_full_template = "-H 'Host: localhost' -H '{accept}' -H 'Accept-Language: en-US,en;q=0.5' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) Gecko/20130501 Firefox/30.0 AppleWebKit/600.00 Chrome/30.0.0000.0 Trident/10.0 Safari/600.00' -H 'Cookie: uid=12345678901234567890; __utma=1.1234567890.1234567890.1234567890.1234567890.12; wd=2560x1600' -H 'Connection: keep-alive'"
  15. accept_json = "Accept: application/json,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7"
  16. accept_html = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  17. accept_plaintext = "Accept: text/plain,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7"
  18. concurrency_template = """
  19. echo ""
  20. echo "---------------------------------------------------------"
  21. echo " Running Primer {name}"
  22. echo " {wrk} {headers} -d 5 -c 8 -t 8 \"http://{server_host}:{port}{url}\""
  23. echo "---------------------------------------------------------"
  24. echo ""
  25. {wrk} {headers} -d 5 -c 8 -t 8 "http://{server_host}:{port}{url}"
  26. sleep 5
  27. echo ""
  28. echo "---------------------------------------------------------"
  29. echo " Running Warmup {name}"
  30. echo " {wrk} {headers} -d {duration} -c {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}\""
  31. echo "---------------------------------------------------------"
  32. echo ""
  33. {wrk} {headers} -d {duration} -c {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}"
  34. sleep 5
  35. for c in {interval}
  36. do
  37. echo ""
  38. echo "---------------------------------------------------------"
  39. echo " Concurrency: $c for {name}"
  40. echo " {wrk} {headers} {pipeline} -d {duration} -c $c -t $(($c>{max_threads}?{max_threads}:$c)) \"http://{server_host}:{port}{url}\""
  41. echo "---------------------------------------------------------"
  42. echo ""
  43. {wrk} {headers} {pipeline} -d {duration} -c "$c" -t "$(($c>{max_threads}?{max_threads}:$c))" http://{server_host}:{port}{url}
  44. sleep 2
  45. done
  46. """
  47. query_template = """
  48. echo ""
  49. echo "---------------------------------------------------------"
  50. echo " Running Primer {name}"
  51. echo " wrk {headers} -d 5 -c 8 -t 8 \"http://{server_host}:{port}{url}2\""
  52. echo "---------------------------------------------------------"
  53. echo ""
  54. wrk {headers} -d 5 -c 8 -t 8 "http://{server_host}:{port}{url}2"
  55. sleep 5
  56. echo ""
  57. echo "---------------------------------------------------------"
  58. echo " Running Warmup {name}"
  59. echo " wrk {headers} -d {duration} -c {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}2\""
  60. echo "---------------------------------------------------------"
  61. echo ""
  62. wrk {headers} -d {duration} -c {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}2"
  63. sleep 5
  64. for c in {interval}
  65. do
  66. echo ""
  67. echo "---------------------------------------------------------"
  68. echo " Queries: $c for {name}"
  69. echo " wrk {headers} -d {duration} -c {max_concurrency} -t {max_threads} \"http://{server_host}:{port}{url}$c\""
  70. echo "---------------------------------------------------------"
  71. echo ""
  72. wrk {headers} -d {duration} -c {max_concurrency} -t {max_threads} "http://{server_host}:{port}{url}$c"
  73. sleep 2
  74. done
  75. """
  76. language = None
  77. platform = None
  78. webserver = None
  79. classification = None
  80. database = None
  81. approach = None
  82. orm = None
  83. framework = None
  84. os = None
  85. database_os = None
  86. display_name = None
  87. notes = None
  88. versus = None
  89. ##########################################################################################
  90. # Public Methods
  91. ##########################################################################################
  92. ############################################################
  93. # start(benchmarker)
  94. # Start the test using it's setup file
  95. ############################################################
  96. def start(self):
  97. return self.setup_module.start(self.benchmarker)
  98. ############################################################
  99. # End start
  100. ############################################################
  101. ############################################################
  102. # stop(benchmarker)
  103. # Stops the test using it's setup file
  104. ############################################################
  105. def stop(self):
  106. return self.setup_module.stop()
  107. ############################################################
  108. # End stop
  109. ############################################################
  110. ############################################################
  111. # verify_urls
  112. # Verifys each of the URLs for this test. THis will sinply
  113. # curl the URL and check for it's return status.
  114. # For each url, a flag will be set on this object for whether
  115. # or not it passed
  116. ############################################################
  117. def verify_urls(self):
  118. # JSON
  119. try:
  120. print "VERIFYING JSON (" + self.json_url + ") ..."
  121. url = self.benchmarker.generate_url(self.json_url, self.port)
  122. self.__curl_url(url)
  123. self.json_url_passed = True
  124. except (AttributeError, subprocess.CalledProcessError) as e:
  125. self.json_url_passed = False
  126. # DB
  127. try:
  128. print "VERIFYING DB (" + self.db_url + ") ..."
  129. url = self.benchmarker.generate_url(self.db_url, self.port)
  130. self.__curl_url(url)
  131. self.db_url_passed = True
  132. except (AttributeError, subprocess.CalledProcessError) as e:
  133. self.db_url_passed = False
  134. # Query
  135. try:
  136. print "VERIFYING Query (" + self.query_url + "2) ..."
  137. url = self.benchmarker.generate_url(self.query_url + "2", self.port)
  138. self.__curl_url(url)
  139. self.query_url_passed = True
  140. except (AttributeError, subprocess.CalledProcessError) as e:
  141. self.query_url_passed = False
  142. # Fortune
  143. try:
  144. print "VERIFYING Fortune (" + self.fortune_url + ") ..."
  145. url = self.benchmarker.generate_url(self.fortune_url, self.port)
  146. self.__curl_url(url)
  147. self.fortune_url_passed = True
  148. except (AttributeError, subprocess.CalledProcessError) as e:
  149. self.fortune_url_passed = False
  150. # Update
  151. try:
  152. print "VERIFYING Update (" + self.update_url + "2) ..."
  153. url = self.benchmarker.generate_url(self.update_url + "2", self.port)
  154. self.__curl_url(url)
  155. self.update_url_passed = True
  156. except (AttributeError, subprocess.CalledProcessError) as e:
  157. self.update_url_passed = False
  158. # plaintext
  159. try:
  160. print "VERIFYING Plaintext (" + self.plaintext_url + ") ..."
  161. url = self.benchmarker.generate_url(self.plaintext_url, self.port)
  162. self.__curl_url(url)
  163. self.plaintext_url_passed = True
  164. except (AttributeError, subprocess.CalledProcessError) as e:
  165. self.plaintext_url_passed = False
  166. ############################################################
  167. # End verify_urls
  168. ############################################################
  169. ############################################################
  170. # contains_type(type)
  171. # true if this test contains an implementation of the given
  172. # test type (json, db, etc.)
  173. ############################################################
  174. def contains_type(self, type):
  175. try:
  176. if type == 'json' and self.json_url is not None:
  177. return True
  178. if type == 'db' and self.db_url is not None:
  179. return True
  180. if type == 'query' and self.query_url is not None:
  181. return True
  182. if type == 'fortune' and self.fortune_url is not None:
  183. return True
  184. if type == 'update' and self.update_url is not None:
  185. return True
  186. if type == 'plaintext' and self.plaintext_url is not None:
  187. return True
  188. except AttributeError:
  189. pass
  190. return False
  191. ############################################################
  192. # End stop
  193. ############################################################
  194. ############################################################
  195. # benchmark
  196. # Runs the benchmark for each type of test that it implements
  197. # JSON/DB/Query.
  198. ############################################################
  199. def benchmark(self):
  200. # JSON
  201. try:
  202. if self.json_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "json"):
  203. sys.stdout.write("BENCHMARKING JSON ... ")
  204. sys.stdout.flush()
  205. remote_script = self.__generate_concurrency_script(self.json_url, self.port, self.accept_json)
  206. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'json'))
  207. results = self.__parse_test('json')
  208. self.benchmarker.report_results(framework=self, test="json", results=results['results'])
  209. print "Complete"
  210. except AttributeError:
  211. pass
  212. # DB
  213. try:
  214. if self.db_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "db"):
  215. sys.stdout.write("BENCHMARKING DB ... ")
  216. sys.stdout.flush()
  217. remote_script = self.__generate_concurrency_script(self.db_url, self.port, self.accept_json)
  218. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'db'))
  219. results = self.__parse_test('db')
  220. self.benchmarker.report_results(framework=self, test="db", results=results['results'])
  221. print "Complete"
  222. except AttributeError:
  223. traceback.print_exc()
  224. pass
  225. # Query
  226. try:
  227. if self.query_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "query"):
  228. sys.stdout.write("BENCHMARKING Query ... ")
  229. sys.stdout.flush()
  230. remote_script = self.__generate_query_script(self.query_url, self.port, self.accept_json)
  231. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'query'))
  232. results = self.__parse_test('query')
  233. self.benchmarker.report_results(framework=self, test="query", results=results['results'])
  234. print "Complete"
  235. except AttributeError:
  236. traceback.print_exc()
  237. pass
  238. # fortune
  239. try:
  240. if self.fortune_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "fortune"):
  241. sys.stdout.write("BENCHMARKING Fortune ... ")
  242. sys.stdout.flush()
  243. remote_script = self.__generate_concurrency_script(self.fortune_url, self.port, self.accept_html)
  244. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'fortune'))
  245. results = self.__parse_test('fortune')
  246. self.benchmarker.report_results(framework=self, test="fortune", results=results['results'])
  247. print "Complete"
  248. except AttributeError:
  249. traceback.print_exc()
  250. pass
  251. # update
  252. try:
  253. if self.update_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "update"):
  254. sys.stdout.write("BENCHMARKING Update ... ")
  255. sys.stdout.flush()
  256. remote_script = self.__generate_query_script(self.update_url, self.port, self.accept_json)
  257. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'update'))
  258. results = self.__parse_test('update')
  259. self.benchmarker.report_results(framework=self, test="update", results=results['results'])
  260. print "Complete"
  261. except AttributeError:
  262. traceback.print_exc()
  263. pass
  264. # plaintext
  265. try:
  266. if self.plaintext_url_passed and (self.benchmarker.type == "all" or self.benchmarker.type == "plaintext"):
  267. sys.stdout.write("BENCHMARKING Plaintext ... ")
  268. sys.stdout.flush()
  269. remote_script = self.__generate_concurrency_script(self.plaintext_url, self.port, self.accept_plaintext, wrk_command="wrk-pipeline", intervals=[256,1024,4096,16384], pipeline="--pipeline 16")
  270. self.__run_benchmark(remote_script, self.benchmarker.output_file(self.name, 'plaintext'))
  271. results = self.__parse_test('plaintext')
  272. self.benchmarker.report_results(framework=self, test="plaintext", results=results['results'])
  273. print "Complete"
  274. except AttributeError:
  275. traceback.print_exc()
  276. pass
  277. ############################################################
  278. # End benchmark
  279. ############################################################
  280. ############################################################
  281. # parse_all
  282. # Method meant to be run for a given timestamp
  283. ############################################################
  284. def parse_all(self):
  285. # JSON
  286. if os.path.exists(self.benchmarker.output_file(self.name, 'json')):
  287. results = self.__parse_test('json')
  288. self.benchmarker.report_results(framework=self, test="json", results=results['results'])
  289. # DB
  290. if os.path.exists(self.benchmarker.output_file(self.name, 'db')):
  291. results = self.__parse_test('db')
  292. self.benchmarker.report_results(framework=self, test="db", results=results['results'])
  293. # Query
  294. if os.path.exists(self.benchmarker.output_file(self.name, 'query')):
  295. results = self.__parse_test('query')
  296. self.benchmarker.report_results(framework=self, test="query", results=results['results'])
  297. # Fortune
  298. if os.path.exists(self.benchmarker.output_file(self.name, 'fortune')):
  299. results = self.__parse_test('fortune')
  300. self.benchmarker.report_results(framework=self, test="fortune", results=results['results'])
  301. # Update
  302. if os.path.exists(self.benchmarker.output_file(self.name, 'update')):
  303. results = self.__parse_test('update')
  304. self.benchmarker.report_results(framework=self, test="update", results=results['results'])
  305. # Plaintext
  306. if os.path.exists(self.benchmarker.output_file(self.name, 'plaintext')):
  307. results = self.__parse_test('plaintext')
  308. self.benchmarker.report_results(framework=self, test="plaintext", results=results['results'])
  309. ############################################################
  310. # End parse_all
  311. ############################################################
  312. ############################################################
  313. # __parse_test(test_type)
  314. ############################################################
  315. def __parse_test(self, test_type):
  316. try:
  317. results = dict()
  318. results['results'] = []
  319. with open(self.benchmarker.output_file(self.name, test_type)) as raw_data:
  320. is_warmup = True
  321. rawData = None
  322. for line in raw_data:
  323. if "Queries:" in line or "Concurrency:" in line:
  324. is_warmup = False
  325. rawData = None
  326. continue
  327. if "Warmup" in line or "Primer" in line:
  328. is_warmup = True
  329. continue
  330. if not is_warmup:
  331. if rawData == None:
  332. rawData = dict()
  333. results['results'].append(rawData)
  334. #if "Requests/sec:" in line:
  335. # m = re.search("Requests/sec:\s+([0-9]+)", line)
  336. # rawData['reportedResults'] = m.group(1)
  337. # search for weighttp data such as succeeded and failed.
  338. if "Latency" in line:
  339. m = re.findall("([0-9]+\.*[0-9]*[us|ms|s|m|%]+)", line)
  340. if len(m) == 4:
  341. rawData['latencyAvg'] = m[0]
  342. rawData['latencyStdev'] = m[1]
  343. rawData['latencyMax'] = m[2]
  344. # rawData['latencyStdevPercent'] = m[3]
  345. #if "Req/Sec" in line:
  346. # m = re.findall("([0-9]+\.*[0-9]*[k|%]*)", line)
  347. # if len(m) == 4:
  348. # rawData['requestsAvg'] = m[0]
  349. # rawData['requestsStdev'] = m[1]
  350. # rawData['requestsMax'] = m[2]
  351. # rawData['requestsStdevPercent'] = m[3]
  352. #if "requests in" in line:
  353. # m = re.search("requests in ([0-9]+\.*[0-9]*[ms|s|m|h]+)", line)
  354. # if m != None:
  355. # # parse out the raw time, which may be in minutes or seconds
  356. # raw_time = m.group(1)
  357. # if "ms" in raw_time:
  358. # rawData['total_time'] = float(raw_time[:len(raw_time)-2]) / 1000.0
  359. # elif "s" in raw_time:
  360. # rawData['total_time'] = float(raw_time[:len(raw_time)-1])
  361. # elif "m" in raw_time:
  362. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 60.0
  363. # elif "h" in raw_time:
  364. # rawData['total_time'] = float(raw_time[:len(raw_time)-1]) * 3600.0
  365. if "requests in" in line:
  366. m = re.search("([0-9]+) requests in", line)
  367. if m != None:
  368. rawData['totalRequests'] = int(m.group(1))
  369. if "Socket errors" in line:
  370. if "connect" in line:
  371. m = re.search("connect ([0-9]+)", line)
  372. rawData['connect'] = int(m.group(1))
  373. if "read" in line:
  374. m = re.search("read ([0-9]+)", line)
  375. rawData['read'] = int(m.group(1))
  376. if "write" in line:
  377. m = re.search("write ([0-9]+)", line)
  378. rawData['write'] = int(m.group(1))
  379. if "timeout" in line:
  380. m = re.search("timeout ([0-9]+)", line)
  381. rawData['timeout'] = int(m.group(1))
  382. if "Non-2xx" in line:
  383. m = re.search("Non-2xx or 3xx responses: ([0-9]+)", line)
  384. if m != None:
  385. rawData['5xx'] = int(m.group(1))
  386. return results
  387. except IOError:
  388. return None
  389. ############################################################
  390. # End benchmark
  391. ############################################################
  392. ##########################################################################################
  393. # Private Methods
  394. ##########################################################################################
  395. ############################################################
  396. # __run_benchmark(script, output_file)
  397. # Runs a single benchmark using the script which is a bash
  398. # template that uses weighttp to run the test. All the results
  399. # outputed to the output_file.
  400. ############################################################
  401. def __run_benchmark(self, script, output_file):
  402. with open(output_file, 'w') as raw_file:
  403. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE, stdout=raw_file, stderr=raw_file)
  404. p.communicate(script)
  405. ############################################################
  406. # End __run_benchmark
  407. ############################################################
  408. ############################################################
  409. # __generate_concurrency_script(url, port)
  410. # Generates the string containing the bash script that will
  411. # be run on the client to benchmark a single test. This
  412. # specifically works for the variable concurrency tests (JSON
  413. # and DB)
  414. ############################################################
  415. def __generate_concurrency_script(self, url, port, accept_header, wrk_command="wrk", intervals=[], pipeline=""):
  416. if len(intervals) == 0:
  417. intervals = self.benchmarker.concurrency_levels
  418. headers = self.__get_request_headers(accept_header)
  419. return self.concurrency_template.format(max_concurrency=self.benchmarker.max_concurrency,
  420. max_threads=self.benchmarker.max_threads, name=self.name, duration=self.benchmarker.duration,
  421. interval=" ".join("{}".format(item) for item in intervals),
  422. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers, wrk=wrk_command,
  423. pipeline=pipeline)
  424. ############################################################
  425. # End __generate_concurrency_script
  426. ############################################################
  427. ############################################################
  428. # __generate_query_script(url, port)
  429. # Generates the string containing the bash script that will
  430. # be run on the client to benchmark a single test. This
  431. # specifically works for the variable query tests (Query)
  432. ############################################################
  433. def __generate_query_script(self, url, port, accept_header):
  434. headers = self.__get_request_headers(accept_header)
  435. return self.query_template.format(max_concurrency=self.benchmarker.max_concurrency,
  436. max_threads=self.benchmarker.max_threads, name=self.name, duration=self.benchmarker.duration,
  437. interval=" ".join("{}".format(item) for item in self.benchmarker.query_intervals),
  438. server_host=self.benchmarker.server_host, port=port, url=url, headers=headers)
  439. ############################################################
  440. # End __generate_query_script
  441. ############################################################
  442. ############################################################
  443. # __get_request_headers(accept_header)
  444. # Generates the complete HTTP header string
  445. ############################################################
  446. def __get_request_headers(self, accept_header):
  447. return self.headers_template.format(accept=accept_header)
  448. ############################################################
  449. # End __format_request_headers
  450. ############################################################
  451. ############################################################
  452. # __curl_url
  453. # Dump HTTP response and headers. Throw exception if there
  454. # is an HTTP error.
  455. ############################################################
  456. def __curl_url(self, url):
  457. # Use -i to output response with headers.
  458. # Don't use -f so that the HTTP response code is ignored.
  459. # Use --stderr - to redirect stderr to stdout so we get
  460. # error output for sure in stdout.
  461. # Use -sS to hide progress bar, but show errors.
  462. subprocess.check_call(["curl", "-i", "--stderr", "-", "-sS", url])
  463. # HTTP output may not end in a newline, so add that here.
  464. print ""
  465. # In the curl invocation above we could not use -f because
  466. # then the HTTP response would not be output, so use -f in
  467. # an additional invocation so that if there is an HTTP error,
  468. # subprocess.CalledProcessError will be thrown. Note that this
  469. # uses check_output() instead of check_call() so that we can
  470. # ignore the HTTP response because we already output that in
  471. # the first curl invocation.
  472. subprocess.check_output(["curl", "-fsS", url])
  473. ##############################################################
  474. # End __curl_url
  475. ##############################################################
  476. def requires_database(self):
  477. """Returns True/False if this test requires a database"""
  478. return self.contains_type('fortune') or self.contains_type('database') or self.contains_type('query')
  479. ##########################################################################################
  480. # Constructor
  481. ##########################################################################################
  482. def __init__(self, name, directory, benchmarker, args):
  483. self.name = name
  484. self.directory = directory
  485. self.benchmarker = benchmarker
  486. self.__dict__.update(args)
  487. # ensure directory has __init__.py file so that we can use it as a Python package
  488. if not os.path.exists(os.path.join(directory, "__init__.py")):
  489. open(os.path.join(directory, "__init__.py"), 'w').close()
  490. self.setup_module = setup_module = importlib.import_module(directory + '.' + self.setup_file)
  491. ############################################################
  492. # End __init__
  493. ############################################################
  494. ############################################################
  495. # End FrameworkTest
  496. ############################################################
  497. ##########################################################################################
  498. # Static methods
  499. ##########################################################################################
  500. ##############################################################
  501. # parse_config(config, directory, benchmarker)
  502. # parses a config file and returns a list of FrameworkTest
  503. # objects based on that config file.
  504. ##############################################################
  505. def parse_config(config, directory, benchmarker):
  506. tests = []
  507. # The config object can specify multiple tests, we neep to loop
  508. # over them and parse them out
  509. for test in config['tests']:
  510. for key, value in test.iteritems():
  511. test_name = config['framework']
  512. # if the test uses the 'defualt' keywork, then we don't
  513. # append anything to it's name. All configs should only have 1 default
  514. if key != 'default':
  515. # we need to use the key in the test_name
  516. test_name = test_name + "-" + key
  517. tests.append(FrameworkTest(test_name, directory, benchmarker, value))
  518. return tests
  519. ##############################################################
  520. # End parse_config
  521. ##############################################################