Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 02f14e90e5
Fetching contributors…

Cannot retrieve contributors at this time

file 123 lines (101 sloc) 3.513 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
import socket

from concurrence import _event as event
from concurrence.io import Buffer, get_errno

from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN, ESHUTDOWN, EINTR, EISCONN, ENOENT, EAGAIN

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

s.connect(('127.0.0.1', 11211))
fd = s.fileno()
#we are connected, turn to non-blockig mode
s.setblocking(0)
#print s.fileno()

STATE_WRITE_SET_REQUEST = 0
STATE_READ_SET_RESPONSE = 1
STATE_WAIT_SET_RESPONSE = 2

state = STATE_WRITE_SET_REQUEST

N = 100000
#N = 1000
n = 0

buff = Buffer(1024)
 
def callback(flags):
    #print 'cb', flags
    if flags & event.EV_TIMEOUT:
        raise Exception("Timeout")
    global state
    global n
    while True:
        if state == STATE_WRITE_SET_REQUEST:
            buff.clear()
            key = 'piet%d' % n
            value = 'klaas%d' % n
            buff.write_bytes("%s %s %d 0 %d\r\n%s\r\n" % ('set', key, 0, len(value), value))
            buff.flip()
            written, remaining = buff.send(fd)
            if written < 0:
                raise Exception("TODO")
            if remaining > 0:
                raise Exception("TODO")
            state = STATE_READ_SET_RESPONSE
            buff.clear()
            read_event.add(1.0)
            return
        elif state == STATE_READ_SET_RESPONSE:
            read, remaining = buff.recv(fd)
            if read < 0:
                raise Exception("TODO")
            elif buff.position == 8:
                buff.flip()
                #result = buff.read_line()
                #if result == 'STORED':
                if n < N:
                    n += 1
                    #print n
                    state = STATE_WRITE_SET_REQUEST
                else:
                    raise Exception("DONE")
                #else:
                # raise Exception("TODO")
            else:
                raise Exception("TODO")
        else:
            raise Exception("UNKNOWN STATE")
        
              
read_event = event.event(fd, event.EV_READ, callback)
write_event = event.event(fd, event.EV_WRITE, callback)

def main():
    import time

    start = time.time()

    try:

        callback(0) #kick off!

        while True:
            event.loop()

            while event.has_next():
                e, flags, fd = event.next()
                e.data(flags)

    except:
        end = time.time()
        print '#set/sec', N / (end - start)
        raise


from cProfile import Profile
prof = Profile()
try:
    prof = prof.runctx("main()", globals(), locals())
except Exception:
    pass

import pstats
stats = pstats.Stats(prof)
stats.strip_dirs()
stats.sort_stats('time')
stats.print_stats(20)

#with gil, using deque
#===============================================================================
# henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py
# #set/sec 27512.9132082
# henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py
# #set/sec 32166.8986723
# henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py
# #set/sec 32139.9231288
# henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py
# #set/sec 32263.7280136
# henk@henk-worktop:~/workspace/concurrence{speedup}$ stackless sandbox/test_basic.py
# #set/sec 25581.622577
#===============================================================================
Something went wrong with that request. Please try again.