bdb.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. import sys
  3. from random import randrange
  4. from uuid import uuid4
  5. import arrow
  6. from rdflib import Dataset
  7. from rdflib import plugin
  8. from rdflib.store import Store
  9. from rdflib.term import URIRef
  10. default_n = 10000
  11. sys.stdout.write('How many resources? [{}] >'.format(default_n))
  12. choice = input().lower()
  13. n = int(choice) if choice else default_n
  14. store_uid = randrange(8192)
  15. store_name = '/tmp/lsup_{}.db'.format(store_uid)
  16. store = plugin.get('Sleepycat', Store)()
  17. ds = Dataset(store)
  18. store.open(store_name)
  19. start = arrow.utcnow()
  20. ckpt = start
  21. for i in range(1, n):
  22. try:
  23. subj = URIRef('http://ex.org/rdf/{}'.format(uuid4()))
  24. pomegranate = URIRef('http://ex.org/pomegranate')
  25. #gr = ds.graph('http://ex.org/graph#g{}'.format(i))
  26. gr = ds.graph('http://ex.org/graph#g1')
  27. for ii in range(1, 100):
  28. gr.add((subj, URIRef('http://ex.org/p1'),
  29. URIRef('http://ex.org/random#'.format(randrange(2048)))))
  30. gr.add((pomegranate, URIRef('http://ex.org/p2'), subj))
  31. q = '''
  32. CONSTRUCT {
  33. ?meta_s ?meta_p ?meta_o .
  34. ?s ?p ?o .
  35. ?s <info:fcrepo#writable> true .
  36. }
  37. WHERE {
  38. GRAPH ?mg {
  39. ?meta_s ?meta_p ?meta_o .
  40. }
  41. OPTIONAL {
  42. GRAPH ?sg {
  43. ?s ?p ?o .
  44. FILTER ( ?p != <http://ex.org/p2> )
  45. }
  46. }
  47. }
  48. '''
  49. qres = ds.query(q, initBindings={'s': pomegranate, 'mg': gr, 'sg': gr})
  50. if i % 100 == 0:
  51. now = arrow.utcnow()
  52. tdelta = now - ckpt
  53. ckpt = now
  54. print('Record: {}\tTime this round: {}'.format(i, tdelta))
  55. #print('Qres size: {}'.format(len(qres)))
  56. except KeyboardInterrupt:
  57. print('Interrupted after {} iterations.'.format(i))
  58. break
  59. tdelta = arrow.utcnow() - start
  60. print('Store name: {}'.format(store_name))
  61. print('Total elapsed time: {}'.format(tdelta))
  62. print('Average time per resource: {}'.format(tdelta.total_seconds()/i))
  63. print('Graph size: {}'.format(len(gr)))
  64. store.close()