benchmark.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 rdflib import Graph, URIRef, Literal
  8. from util.generators import random_utf8_string
  9. default_n = 10000
  10. webroot = 'http://localhost:8000/ldp'
  11. #webroot = 'http://localhost:8080/rest'
  12. container_uri = webroot + '/pomegranate'
  13. sys.stdout.write('How many children? [{}] >'.format(default_n))
  14. choice = input().lower()
  15. n = int(choice) if choice else default_n
  16. sys.stdout.write('Delete container? [n] >')
  17. choice = input().lower()
  18. del_cont = choice or 'n'
  19. sys.stdout.write('POST or PUT? [PUT] >')
  20. choice = input().lower()
  21. if choice and choice.lower() not in ('post', 'put'):
  22. raise ValueError('Not a valid verb.')
  23. method = choice.lower() or 'put'
  24. # Generate 10,000 children of root node.
  25. if del_cont == 'y':
  26. requests.delete(container_uri, headers={'prefer': 'no-tombstone'})
  27. requests.put(container_uri)
  28. start = arrow.utcnow()
  29. ckpt = start
  30. print('Inserting {} children.'.format(n))
  31. # URI used to establish an in-repo relationship.
  32. prev_uri = container_uri
  33. size = 50 # Size of graph to be multiplied by 4.
  34. try:
  35. for i in range(1, n):
  36. url = '{}/{}'.format(container_uri, uuid4()) if method == 'put' \
  37. else container_uri
  38. # Generate synthetic graph.
  39. #print('generating graph: {}'.format(i))
  40. g = Graph()
  41. for ii in range(size):
  42. g.add((
  43. URIRef(''),
  44. URIRef('urn:inturi_p:{}'.format(ii % size)),
  45. URIRef(prev_uri)
  46. ))
  47. g.add((
  48. URIRef(''),
  49. URIRef('urn:lit_p:{}'.format(ii % size)),
  50. Literal(random_utf8_string(64))
  51. ))
  52. g.add((
  53. URIRef(''),
  54. URIRef('urn:lit_p:{}'.format(ii % size)),
  55. Literal(random_utf8_string(64))
  56. ))
  57. g.add((
  58. URIRef(''),
  59. URIRef('urn:exturi_p:{}'.format(ii % size)),
  60. URIRef('http://exmple.edu/res/{}'.format(ii // 10))
  61. ))
  62. # Send request.
  63. rsp = requests.request(
  64. method, url, data=g.serialize(format='ttl'),
  65. headers={ 'content-type': 'text/turtle'})
  66. rsp.raise_for_status()
  67. prev_uri = rsp.headers['location']
  68. if i % 10 == 0:
  69. now = arrow.utcnow()
  70. tdelta = now - ckpt
  71. ckpt = now
  72. print('Record: {}\tTime elapsed: {}'.format(i, tdelta))
  73. except KeyboardInterrupt:
  74. print('Interruped after {} iterations.'.format(i))
  75. tdelta = arrow.utcnow() - start
  76. print('Total elapsed time: {}'.format(tdelta))
  77. print('Average time per resource: {}'.format(tdelta.total_seconds()/i))