Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created July 19, 2020 03:24
Show Gist options
  • Save ashleysommer/b4e17be4afb8989348d892f001289074 to your computer and use it in GitHub Desktop.
Save ashleysommer/b4e17be4afb8989348d892f001289074 to your computer and use it in GitHub Desktop.
Benchmarking the new Memory2 rdflib triplestore implementation
import random
from string import ascii_uppercase, ascii_lowercase, digits
from rdflib import Graph, ConjunctiveGraph, URIRef, BNode, Literal
import timeit
ALPHABET = ascii_uppercase + ascii_lowercase + digits
def make_word(length=8):
return ''.join(random.choice(ALPHABET) for _ in range(length))
def make_random_uriref():
return URIRef("http://"
+ make_word()
+ (".com/" if random.choice("CO") == "C" else ".org/")
+ make_word()
+ ("#" if random.choice("HS") == "H" else "/")
+ make_word())
def make_random_literal():
l_type = random.choice("SIB")
if l_type == "S":
return Literal(make_word())
elif l_type == "I":
return Literal(random.randint(1, 1000000))
else:
return Literal(random.choice([True, False]))
def make_triple():
# subject is a bnode or uri
if random.choice('BU') == 'B':
s = BNode()
else:
s = make_random_uriref()
# predicate is a uri
p = make_random_uriref()
# object is a bnode or uri or literal
p_choice = random.choice('BUL')
if p_choice == "B":
o = BNode()
elif p_choice == "U":
o = make_random_uriref()
else:
o = make_random_literal()
return (s, p, o)
global_triples = [make_triple() for _ in range(100000)]
_setup = r"""
triples = global_triples
"""
_run1 = r"""
g = Graph("Memory1")
for t in triples:
g.add(t)
_ = list(g.triples((None, None, None)))
g.remove((None, None, None))
del g
"""
_run2 = r"""
g = Graph("Memory2")
for t in triples:
g.add(t)
_ = list(g.triples((None, None, None)))
g.remove((None, None, None))
del g
"""
_run3 = r"""
g = Graph("IOMemory")
for t in triples:
g.add(t)
_ = list(g.triples((None, None, None)))
g.remove((None, None, None))
del g
"""
a = timeit.timeit(_run1, setup=_setup, number=10, globals={"Graph": Graph, "global_triples": global_triples})
b = timeit.timeit(_run2, setup=_setup, number=10, globals={"Graph": Graph, "global_triples": global_triples})
c = timeit.timeit(_run3, setup=_setup, number=10, globals={"Graph": Graph, "global_triples": global_triples})
print(a/10.0)
print(b/10.0)
print(c/10.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment