| 1 |
from __future__ import print_function |
| 2 |
import unittest |
| 3 |
import gc |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
from time import time |
| 7 |
from tempfile import mkdtemp |
| 8 |
|
| 9 |
from rdflib import Graph |
| 10 |
from six.moves.urllib.request import pathname2url |
| 11 |
|
| 12 |
try: |
| 13 |
from rdflib.plugins.stores.memory import Memory |
| 14 |
except ImportError: |
| 15 |
|
| 16 |
from rdflib.plugins.memory import IOMemory as Memory |
| 17 |
|
| 18 |
|
| 19 |
class StoreTestCase(unittest.TestCase): |
| 20 |
|
| 21 |
""" |
| 22 |
Test case for testing store performance... probably should be |
| 23 |
something other than a unit test... but for now we'll add it as a |
| 24 |
unit test. |
| 25 |
""" |
| 26 |
store = None |
| 27 |
path = None |
| 28 |
storetest = True |
| 29 |
performancetest = True |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
self.gcold = gc.isenabled() |
| 33 |
gc.collect() |
| 34 |
gc.disable() |
| 35 |
if self.store is None: |
| 36 |
store = Memory() |
| 37 |
else: |
| 38 |
store = self.store |
| 39 |
self.graph = Graph(store=store) |
| 40 |
self.tempdir = None |
| 41 |
if not self.path: |
| 42 |
self.tempdir = mkdtemp() |
| 43 |
path = pathname2url(self.tempdir) |
| 44 |
else: |
| 45 |
path = self.path |
| 46 |
self.path = path |
| 47 |
self.graph.open(self.path, create=True) |
| 48 |
self.input = Graph() |
| 49 |
|
| 50 |
def tearDown(self): |
| 51 |
self.graph.close() |
| 52 |
if self.gcold: |
| 53 |
gc.enable() |
| 54 |
self.graph.close() |
| 55 |
del self.graph |
| 56 |
tempdir = getattr(self, 'tempdir', None) |
| 57 |
if tempdir is not None: |
| 58 |
shutil.rmtree(tempdir) |
| 59 |
|
| 60 |
def testTime(self): |
| 61 |
|
| 62 |
print('"Load %s": [' % self.store) |
| 63 |
for i in ['500triples', '1ktriples', '2ktriples', |
| 64 |
'3ktriples', '5ktriples', '10ktriples', |
| 65 |
'25ktriples']: |
| 66 |
inputloc = os.getcwd() + '/test/sp2b/%s.n3' % i |
| 67 |
|
| 68 |
|
| 69 |
self.input = Graph() |
| 70 |
self.graph.remove((None, None, None)) |
| 71 |
res = self._testInput(inputloc) |
| 72 |
print("Loaded %5d triples in %ss" % (len(self.graph), res.strip())) |
| 73 |
print("],") |
| 74 |
print('"Read %s": [' % self.store) |
| 75 |
t0 = time() |
| 76 |
for _i in self.graph.triples((None, None, None)): |
| 77 |
pass |
| 78 |
self.assertEqual(len(self.graph), 25161) |
| 79 |
t1 = time() |
| 80 |
print("%.3gs" % (t1 - t0)) |
| 81 |
print("],") |
| 82 |
print('"Delete %s": [' % self.store) |
| 83 |
t0 = time() |
| 84 |
self.graph.remove((None, None, None)) |
| 85 |
self.assertEqual(len(self.graph), 0) |
| 86 |
t1 = time() |
| 87 |
print("%.3g " % (t1 - t0)) |
| 88 |
print("],") |
| 89 |
|
| 90 |
def _testInput(self, inputloc): |
| 91 |
store = self.graph |
| 92 |
self.input.parse(location=inputloc, format="n3") |
| 93 |
|
| 94 |
t0 = time() |
| 95 |
store.addN(tuple(t) + (store,) for t in self.input) |
| 96 |
t1 = time() |
| 97 |
return "%.3g " % (t1 - t0) |
| 98 |
|
| 99 |
|
| 100 |
class SQLAlchemyStoreTestCase(StoreTestCase): |
| 101 |
"""SQLAlchemy Store.""" |
| 102 |
|
| 103 |
store = "SQLAlchemy" |
| 104 |
|
| 105 |
def setUp(self): |
| 106 |
"""Setup.""" |
| 107 |
self.store = "SQLAlchemy" |
| 108 |
self.path = "sqlite:///%(here)s/test/tmpdb.sqlite" % { |
| 109 |
"here": os.getcwd() |
| 110 |
} |
| 111 |
StoreTestCase.setUp(self) |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
unittest.main() |