benchmark.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.append('.')
  4. from uuid import uuid4
  5. import arrow
  6. import requests
  7. from lakesuperior.util.generators import (
  8. random_image, random_graph, random_utf8_string)
  9. __doc__ = '''
  10. Benchmark script to measure write performance.
  11. '''
  12. default_n = 10000
  13. #webroot = 'http://localhost:8080/rest'
  14. webroot = 'http://localhost:8000/ldp'
  15. #webroot = 'http://localhost:5000/ldp'
  16. container_uri = webroot + '/pomegranate'
  17. def run():
  18. sys.stdout.write('How many children? [{}] >'.format(default_n))
  19. choice = input().lower()
  20. n = int(choice) if choice else default_n
  21. sys.stdout.write('Delete container? [n] >')
  22. choice = input().lower()
  23. del_cont = choice or 'n'
  24. sys.stdout.write('POST or PUT? [PUT] >')
  25. choice = input().lower()
  26. if choice and choice.lower() not in ('post', 'put'):
  27. raise ValueError('Not a valid verb.')
  28. method = choice.lower() or 'put'
  29. sys.stdout.write('RDF Sources (r), Non-RDF (n), or Both 50/50 (b)? [r] >')
  30. choice = input().lower()
  31. res_type = choice or 'r'
  32. if del_cont == 'y':
  33. requests.delete(container_uri, headers={'prefer': 'no-tombstone'})
  34. requests.put(container_uri)
  35. print('Inserting {} children.'.format(n))
  36. # URI used to establish an in-repo relationship.
  37. ref = container_uri
  38. size = 200 # Size of graph.
  39. wclock_start = arrow.utcnow()
  40. try:
  41. for i in range(1, n + 1):
  42. url = '{}/{}'.format(container_uri, uuid4()) if method == 'put' \
  43. else container_uri
  44. if res_type == 'r' or (res_type == 'b' and i % 2 == 0):
  45. data = random_graph(size, ref).serialize(format='ttl')
  46. headers = {'content-type': 'text/turtle'}
  47. else:
  48. img = random_image(name=uuid4(), ts=16, ims=512)
  49. data = img['content']
  50. data.seek(0)
  51. headers = {
  52. 'content-type': 'image/png',
  53. 'content-disposition': 'attachment; filename="{}"'
  54. .format(uuid4())}
  55. #import pdb; pdb.set_trace()
  56. # Start timing after generating the data.
  57. ckpt = arrow.utcnow()
  58. if i == 1:
  59. tcounter = ckpt - ckpt
  60. prev_tcounter = tcounter
  61. rsp = requests.request(method, url, data=data, headers=headers)
  62. tdelta = arrow.utcnow() - ckpt
  63. tcounter += tdelta
  64. rsp.raise_for_status()
  65. ref = rsp.headers['location']
  66. if i % 10 == 0:
  67. print(
  68. f'Record: {i}\tTime elapsed: {tcounter}\t'
  69. f'Per resource: {(tcounter - prev_tcounter) / 10}')
  70. prev_tcounter = tcounter
  71. except KeyboardInterrupt:
  72. print('Interrupted after {} iterations.'.format(i))
  73. wclock = arrow.utcnow() - wclock_start
  74. print(f'Total elapsed time: {wclock}')
  75. print(f'Total time spent ingesting resources: {tcounter}')
  76. print(f'Average time per resource: {tcounter.total_seconds()/i}')
  77. if __name__ == '__main__':
  78. run()