benchmark.py 2.4 KB

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