<s> """<STR_LIT>""" <EOL> import time <EOL> import benchmarks <EOL> BYTES = <NUM_LIT:1000> <EOL> SIZE = <NUM_LIT:1> <EOL> CONCURRENCY = <NUM_LIT:50> <EOL> TRIES = <NUM_LIT:5> <EOL> def reader ( sock ) : <EOL> expect = BYTES <EOL> while expect > <NUM_LIT:0> : <EOL> d = sock . recv ( min ( expect , SIZE ) ) <EOL> expect -= len ( d ) <EOL> def writer ( addr , socket_impl ) : <EOL> sock = socket_impl ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( addr ) <EOL> sent = <NUM_LIT:0> <EOL> while sent < BYTES : <EOL> d = '<STR_LIT>' * ( max ( min ( SIZE / <NUM_LIT:2> , BYTES - sent ) , <NUM_LIT:1> ) ) <EOL> sock . sendall ( d ) <EOL> sent += len ( d ) <EOL> def green_accepter ( server_sock , pool ) : <EOL> for i in xrange ( CONCURRENCY ) : <EOL> sock , addr = server_sock . accept ( ) <EOL> pool . spawn_n ( reader , sock ) <EOL> def heavy_accepter ( server_sock , pool ) : <EOL> for i in xrange ( CONCURRENCY ) : <EOL> sock , addr = server_sock . accept ( ) <EOL> t = threading . Thread ( None , reader , "<STR_LIT>" , ( sock , ) ) <EOL> t . start ( ) <EOL> pool . append ( t ) <EOL> import eventlet . green . socket <EOL> import eventlet <EOL> from eventlet import debug <EOL> debug . hub_exceptions ( True ) <EOL> def launch_green_threads ( ) : <EOL> pool = eventlet . GreenPool ( CONCURRENCY * <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> server_sock = eventlet . green . socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> server_sock . bind ( ( '<STR_LIT:localhost>' , <NUM_LIT:0> ) ) <EOL> server_sock . listen ( <NUM_LIT:50> ) <EOL> addr = ( '<STR_LIT:localhost>' , server_sock . getsockname ( ) [ <NUM_LIT:1> ] ) <EOL> pool . spawn_n ( green_accepter , server_sock , pool ) <EOL> for i in xrange ( CONCURRENCY ) : <EOL> pool . spawn_n ( writer , addr , eventlet . green . socket . socket ) <EOL> pool . waitall ( ) <EOL> import threading <EOL> import socket <EOL> def launch_heavy_threads ( ) : <EOL> threads = [ ] <EOL> server_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> server_sock . bind ( ( '<STR_LIT:localhost>' , <NUM_LIT:0> ) ) <EOL> server_sock . listen ( <NUM_LIT:50> ) <EOL> addr = ( '<STR_LIT:localhost>' , server_sock . getsockname ( ) [ <NUM_LIT:1> ] ) <EOL> accepter_thread = threading . Thread ( None , heavy_accepter , "<STR_LIT>" , ( server_sock , threads ) ) <EOL> accepter_thread . start ( ) <EOL> threads . append ( accepter_thread ) <EOL> for i in xrange ( CONCURRENCY ) : <EOL> client_thread = threading . Thread ( None , writer , "<STR_LIT>" , ( addr , socket . socket ) ) <EOL> client_thread . start ( ) <EOL> threads . append ( client_thread ) <EOL> for t in threads : <EOL> t . join ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import optparse <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:int>' , dest = '<STR_LIT>' , <EOL> default = BYTES ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:int>' , dest = '<STR_LIT:size>' , <EOL> default = SIZE ) <EOL> parser . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , type = '<STR_LIT:int>' , dest = '<STR_LIT>' , <EOL> default = CONCURRENCY ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:int>' , dest = '<STR_LIT>' , <EOL> default = TRIES ) <EOL> opts , args = parser . parse_args ( ) <EOL> BYTES = opts . bytes <EOL> SIZE = opts . size <EOL> CONCURRENCY = opts . concurrency <EOL> TRIES = opts . tries <EOL> funcs = [ launch_green_threads ] <EOL> if opts . threading : <EOL> funcs = [ launch_green_threads , launch_heavy_threads ] <EOL> results = benchmarks . measure_best ( TRIES , <NUM_LIT:3> , <EOL> lambda : None , lambda : None , <EOL> * funcs ) <EOL> print "<STR_LIT>" , results [ launch_green_threads ] <EOL> if opts . threading : <EOL> print "<STR_LIT>" , results [ launch_heavy_threads ] <EOL> print "<STR_LIT:%>" , ( results [ launch_green_threads ] - results [ launch_heavy_threads ] ) / results [ launch_heavy_threads ] * <NUM_LIT:100> </s>
