bdb.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 = 100000
  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. gr = ds.graph('http://ex.org/graph#g{}'.format(i))
  25. for ii in range(1, 100):
  26. gr.add((subj, URIRef('http://ex.org/p1'),
  27. URIRef('http://ex.org/random#'.format(randrange(2048)))))
  28. gr.add((URIRef('http://ex.org/s1'), URIRef('http://ex.org/p2'), subj))
  29. now = arrow.utcnow()
  30. tdelta = now - ckpt
  31. ckpt = now
  32. if i % 100 == 0:
  33. print('Record: {}\tTime elapsed: {}'.format(i, tdelta))
  34. except KeyboardInterrupt:
  35. print('Interrupted after {} iterations.'.format(i))
  36. break
  37. tdelta = arrow.utcnow() - start
  38. print('Store name: {}'.format(store_name))
  39. print('Total elapsed time: {}'.format(tdelta))
  40. print('Average time per resource: {}'.format(tdelta.total_seconds()/i))
  41. print('Graph size: {}'.format(len(gr)))
  42. store.close()