Skip to content

Instantly share code, notes, and snippets.

@jmandel
Created March 30, 2012 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmandel/2253815 to your computer and use it in GitHub Desktop.
Save jmandel/2253815 to your computer and use it in GitHub Desktop.
Binary search to find max working graph size
import sys
import urllib
import urllib2
REPOSITORY = "http://localhost:8080/openrdf-sesame/repositories/test/statements"
head="""INSERT DATA {
GRAPH <http://one-graph-to-break-them-all> { """
tail=""" }
}"""
def graph_of_size(n):
middle = []
for j in range(n):
middle.append("<http://one-node> <http://sees> <http://%s>."%j)
return head + "\n".join(middle) + tail
def request_for_n(n):
values = {'update': graph_of_size(n)}
data = urllib.urlencode(values)
req = urllib2.Request(REPOSITORY, data)
response = urllib2.urlopen(req)
print response.info()
def binary_search(lower, upper):
while (lower < upper):
mid = (lower+upper)/2
try:
request_for_n(mid)
print "Succeed, moving lower to", mid
lower = mid
except Exception as e:
print e, "Fail, moving upper to", mid
upper = mid-1
print "converged to", lower
return lower
lower=0
upper = 1000000
binary_search(lower,upper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment