Skip to content

Commit

Permalink
merge noiostream branch
Browse files Browse the repository at this point in the history
  • Loading branch information
thefab committed Feb 3, 2015
1 parent 567dff1 commit 52d8f97
Show file tree
Hide file tree
Showing 16 changed files with 1,173 additions and 369 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ doc:
cd docs && make html

test:
flake8 --exclude=docs/* .
flake8 --exclude=docs/*,examples/* .
cd tests && nosetests

coveralls:
Expand Down
39 changes: 39 additions & 0 deletions examples/bench100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import tornado
import tornadis
import datetime
import logging

#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.CRITICAL)

@tornado.gen.coroutine
def ping_redis(client):
reply = yield client.call("PING")
if reply != 'PONG':
raise Exception('bad reply')


@tornado.gen.coroutine
def multiple_ping_redis(pool):
client = yield pool.get_connected_client()
before = datetime.datetime.now()
for j in range(0, 1000):
yield [ping_redis(client) for i in range(0, 10)]
after = datetime.datetime.now()
print after - before
pool.destroy()


def stop_loop(future):
excep = future.exception()
loop = tornado.ioloop.IOLoop.instance()
loop.stop()
if excep is not None:
raise(excep)


pool = tornadis.ClientPool(max_size=5)
loop = tornado.ioloop.IOLoop.instance()
future = multiple_ping_redis(pool)
loop.add_future(future, stop_loop)
loop.start()
41 changes: 41 additions & 0 deletions examples/bench11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tornado
import tornadis
import datetime


@tornado.gen.coroutine
def ping_redis(pool):
with (yield pool.connected_client()) as client:
reply = yield client.call("PING")
if reply != 'PONG':
raise Exception('bad reply')


@tornado.gen.coroutine
def multiple_ping_redis(pool):
clients = []
for i in range(0, 100):
client = yield pool.get_connected_client()
clients.append(client)
for i in range(0, 100):
pool.release_client(clients[i])
before = datetime.datetime.now()
yield [ping_redis(pool) for i in range(0, 10000)]
after = datetime.datetime.now()
print after - before
pool.destroy()


def stop_loop(future):
excep = future.exception()
loop = tornado.ioloop.IOLoop.instance()
loop.stop()
if excep is not None:
raise(excep)


pool = tornadis.ClientPool(max_size=100)
loop = tornado.ioloop.IOLoop.instance()
future = multiple_ping_redis(pool)
loop.add_future(future, stop_loop)
loop.start()
40 changes: 40 additions & 0 deletions examples/bench12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import tornado
import tornadis
import datetime
import logging

logging.basicConfig(level=logging.CRITICAL)


@tornado.gen.coroutine
def multiple_ping_redis(pool):
client = yield pool.get_connected_client()
before = datetime.datetime.now()
for i in range(0, 10000):
reply = client.call2("PING")
if not isinstance(reply, basestring):
reply2 = yield reply
if reply2 != "PONG":
raise Exception('bad reply2')
else:
if reply != 'PONG':
logging.warning("reply = %s" % reply)
raise Exception('bad reply')
after = datetime.datetime.now()
print after - before
pool.destroy()


def stop_loop(future):
excep = future.exception()
loop = tornado.ioloop.IOLoop.instance()
loop.stop()
if excep is not None:
raise(excep)


pool = tornadis.ClientPool(max_size=100)
loop = tornado.ioloop.IOLoop.instance()
future = multiple_ping_redis(pool)
loop.add_future(future, stop_loop)
loop.start()
48 changes: 48 additions & 0 deletions examples/bench3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import tornado
import tornadis
import datetime
import random
import logging

BODY_SIZE = 1000000
BODY_TMP = ["%s" % random.randint(0, 9) for _ in range(0, BODY_SIZE)]
BODY = "".join(BODY_TMP)

logging.basicConfig(level=logging.CRITICAL)


@tornado.gen.coroutine
def setget_redis(client):
reply1 = yield client.call("SET", "foo", BODY)
if reply1 != "OK":
# raise Exception('bad ok reply : %s', reply1)
raise Exception('bad ok reply')
reply2 = yield client.call("GET", "foo")
if reply2 != BODY:
# raise Exception('bad reply %i <> %i', len(reply2), len(BODY))
raise Exception('bad reply')


@tornado.gen.coroutine
def multiple_setget_redis(pool):
client = yield pool.get_connected_client()
before = datetime.datetime.now()
yield [setget_redis(client) for i in range(0, 1000)]
after = datetime.datetime.now()
print after - before
pool.destroy()


def stop_loop(future):
excep = future.exception()
loop = tornado.ioloop.IOLoop.instance()
loop.stop()
if excep is not None:
raise(excep)


pool = tornadis.ClientPool(max_size=5)
loop = tornado.ioloop.IOLoop.instance()
future = multiple_setget_redis(pool)
loop.add_future(future, stop_loop)
loop.start()
35 changes: 35 additions & 0 deletions examples/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application, url
import tornado.gen
import tornadis
import logging

logging.basicConfig(level=logging.WARNING)
POOL = tornadis.ClientPool(max_size=15)


class HelloHandler(RequestHandler):

@tornado.gen.coroutine
def get(self):
pong = "PONG"
with (yield POOL.connected_client()) as client:
reply = yield client.call("PING")
reply = yield client.call("PING")
reply = yield client.call("PING")
self.write("Hello, %s" % pong)
self.finish()


def make_app():
return Application([
url(r"/", HelloHandler),
])


def main():
app = make_app()
app.listen(8888)
IOLoop.current().start()

main()
57 changes: 12 additions & 45 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tornado.ioloop

from tornadis.client import Client
from tornadis.exceptions import ClientError
from support import test_redis_or_raise_skiptest


Expand Down Expand Up @@ -32,53 +33,19 @@ def test_ping(self):
yield c.disconnect()

@tornado.testing.gen_test
def test_pubsub(self):
def test_discard(self):
c = Client()
yield c.connect()
c.call('PING', discard_reply=True)
yield c.disconnect()

@tornado.testing.gen_test
def test_discard_and_callback(self):
c = Client()
c2 = Client()
yield c.connect()
yield c2.connect()
try:
yield c.pubsub_pop_message()
raise Exception("exception not raised")
except:
pass
res = yield c.pubsub_subscribe("foo1", "foo2")
self.assertTrue(res)
self.assertTrue(c.subscribed)
self.assertFalse(c2.subscribed)
try:
yield c.call("PING")
raise Exception("exception not raised")
except:
c.call('PING', discard_reply=True, callback=lambda x: x)
raise Exception("ClientError not raised")
except ClientError:
pass
res = yield c.pubsub_psubscribe("bar1*", "bar2*")
self.assertTrue(res)
yield c2.call("PUBLISH", "null", "value0")
yield c2.call("PUBLISH", "foo1", "value1")
yield c2.call("PUBLISH", "foo2", "value2")
yield c2.call("PUBLISH", "bar111", "value3")
yield c2.call("PUBLISH", "bar222", "value4")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[2], b"value1")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[2], b"value2")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[3], b"value3")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[3], b"value4")
msg = yield c.pubsub_pop_message(deadline=1)
self.assertEquals(msg, None)
yield c.pubsub_unsubscribe("foo1")
yield c2.call("PUBLISH", "foo1", "value1")
yield c2.disconnect()
msg = yield c.pubsub_pop_message(deadline=1)
self.assertEquals(msg, None)
yield c.pubsub_unsubscribe("foo2")
yield c.pubsub_unsubscribe("foobar")
yield c.pubsub_punsubscribe("foobar*")
yield c.pubsub_punsubscribe("bar1*")
yield c.pubsub_punsubscribe("bar2*")
self.assertFalse(c.subscribed)
res = yield c.call("PING")
self.assertEquals(res, b"PONG")
yield c.disconnect()

0 comments on commit 52d8f97

Please sign in to comment.