Last active
July 19, 2017 02:01
-
-
Save beltran/bd669216b5d6b7343547b5a925e638a5 to your computer and use it in GitHub Desktop.
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 asyncore, socket | |
import time | |
_dispatcher_map = {} | |
def create_socket(): | |
host = "127.0.0.1" | |
port = 5007 | |
addresses = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM) | |
sockerr = None | |
for (af, socktype, proto, canonname, sockaddr) in addresses: | |
try: | |
_socket = socket.socket(af, socktype, proto) | |
_socket.settimeout(None) | |
_socket.connect(sockaddr) | |
_socket.settimeout(None) | |
sockerr = None | |
break | |
except socket.error as err: | |
if _socket: | |
_socket.close() | |
_socket = None | |
sockerr = err | |
if sockerr: | |
raise sockerr | |
_socket.setblocking(0) | |
return _socket | |
class Timer(object): | |
def __init__(self, text, verbose=True): | |
self.text = text | |
self.verbose = verbose | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
self.end = time.time() | |
self.secs = self.end - self.start | |
self.msecs = self.secs * 1000 # millisecs | |
if self.verbose: | |
print(self.text) | |
print('elapsed time: %f ms' % self.msecs) | |
print("") | |
class Client(asyncore.dispatcher): | |
total_count = 0 | |
def __init__(self): | |
self._socket = create_socket() | |
asyncore.dispatcher.__init__(self, self._socket, _dispatcher_map) | |
def handle_close(self): | |
raise Exception("handle_close shouldn't be called") | |
def handle_read(self): | |
Client.total_count += 1 | |
A = self.recv(1024) | |
def handle_write(self): | |
Client.total_count += 1 | |
self.send(b"A" * 100) | |
TOTAL_COUNT = 1e7 | |
use_poll = True | |
Client.total_count = 0 | |
cs = [Client() for _ in range(200)] | |
with Timer("map_copy fix, use_poll: {}".format(use_poll)) as t: | |
while Client.total_count < TOTAL_COUNT: | |
asyncore.loop(timeout=0.0, use_poll=use_poll, count=1, map=_dispatcher_map) | |
for c in cs: | |
c.close() | |
use_poll = False | |
cs = [Client() for _ in range(200)] | |
Client.total_count = 0 | |
with Timer("map_copy fix, use_poll: {}".format(use_poll)) as t: | |
while Client.total_count < TOTAL_COUNT: | |
asyncore.loop(timeout=0.0, use_poll=use_poll, count=1, map=_dispatcher_map) | |
for c in cs: | |
c.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment