benchmark.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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:8000/ldp'
  14. container_uri = webroot + '/pomegranate'
  15. def run():
  16. sys.stdout.write('How many children? [{}] >'.format(default_n))
  17. choice = input().lower()
  18. n = int(choice) if choice else default_n
  19. sys.stdout.write('Delete container? [n] >')
  20. choice = input().lower()
  21. del_cont = choice or 'n'
  22. sys.stdout.write('POST or PUT? [PUT] >')
  23. choice = input().lower()
  24. if choice and choice.lower() not in ('post', 'put'):
  25. raise ValueError('Not a valid verb.')
  26. method = choice.lower() or 'put'
  27. sys.stdout.write('RDF Sources (r), Non-RDF (n), or Both 50/50 (b)? [b] >')
  28. choice = input().lower()
  29. res_type = choice or 'b'
  30. if del_cont == 'y':
  31. requests.delete(container_uri, headers={'prefer': 'no-tombstone'})
  32. requests.put(container_uri)
  33. start = arrow.utcnow()
  34. ckpt = start
  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. try:
  40. for i in range(1, n + 1):
  41. url = '{}/{}'.format(container_uri, uuid4()) if method == 'put' \
  42. else container_uri
  43. if res_type == 'r' or (res_type == 'b' and i % 2 == 0):
  44. data = random_graph(size, ref).serialize(format='ttl')
  45. headers = {'content-type': 'text/turtle'}
  46. else:
  47. img = random_image(name=uuid4(), ts=16, ims=512)
  48. data = img['content']
  49. data.seek(0)
  50. headers = {
  51. 'content-type': 'image/png',
  52. 'content-disposition': 'attachment; filename="{}"'
  53. .format(uuid4())}
  54. #import pdb; pdb.set_trace()
  55. rsp = requests.request(method, url, data=data, headers=headers)
  56. rsp.raise_for_status()
  57. ref = rsp.headers['location']
  58. if i % 10 == 0:
  59. now = arrow.utcnow()
  60. tdelta = now - ckpt
  61. ckpt = now
  62. print('Record: {}\tTime elapsed: {}'.format(i, tdelta))
  63. except KeyboardInterrupt:
  64. print('Interrupted after {} iterations.'.format(i))
  65. tdelta = arrow.utcnow() - start
  66. print('Total elapsed time: {}'.format(tdelta))
  67. print('Average time per resource: {}'.format(tdelta.total_seconds()/i))
  68. if __name__ == '__main__':
  69. run()