Created
March 30, 2012 18:34
-
-
Save jmandel/2253815 to your computer and use it in GitHub Desktop.
Binary search to find max working graph size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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