Skip to content

Instantly share code, notes, and snippets.

@woodsaj
Last active November 30, 2017 09:53
Show Gist options
  • Save woodsaj/316d64ac56e7d66adafd2908304f891f to your computer and use it in GitHub Desktop.
Save woodsaj/316d64ac56e7d66adafd2908304f891f to your computer and use it in GitHub Desktop.
bufferedHTTPResonse
import sys
sys.path.append('/opt/graphite/webapp')
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings')
from graphite.http_pool import http
from graphite.util import unpickle
import time
import io
now = int(time.time())
url = "%s%s" % ('http://mt-read-12574-medium-ops:6060', '/render')
query_params = [
('format', 'pickle'),
('local', '1'),
('noCache', '1'),
('from', now-3600),
('until', now)
]
query_params.append(('target', 'snap.gceprod.grafanalabs.kubestate.pod.hosted-grafana.*.*.status.condition.ready'))
result = http.request(
'POST',
url,
fields=query_params,
headers={"x-org-id": "12574", "accept-encoding": ""},
timeout=60,
preload_content=False)
print result.getheaders()
start = time.time()
class BufferedHTTPResponse(io.IOBase):
def __init__(self, response, buffersize=1048576):
self.response=response
self.buffersize = buffersize
self.buffer = ''
self.pos = 0
def read(self, amt=None):
#print "read for %d bytes made" % amt
if amt is None:
return self.response.read()
if len(self.buffer) - self.pos < amt:
print "reading in buffer"
self.buffer = self.buffer[self.pos:]
self.pos = 0
pre = time.time()
self.buffer += self.response.read(self.buffersize)
print "buffer size %d after %fs" % (len(self.buffer), time.time() - pre)
data = self.buffer[self.pos:self.pos+amt]
self.pos += amt
if self.pos == len(self.buffer):
self.pos=0
self.buffer = ''
#print "returning %d bytes. buffer has %d bytes remaining" % (len(data), len(self.buffer)-self.pos)
return data
data = unpickle.load(BufferedHTTPResponse(result))
print "Got all results in %fs" % (time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment