<s> from __future__ import print_function <EOL> import aerospike <EOL> import random <EOL> import signal <EOL> import sys <EOL> import string <EOL> import time <EOL> from optparse import OptionParser <EOL> import copy <EOL> from tabulate import tabulate <EOL> """<STR_LIT>""" <EOL> usage = "<STR_LIT>" <EOL> optparser = OptionParser ( usage = usage , add_help_option = False ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:username>" , type = "<STR_LIT:string>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:password>" , type = "<STR_LIT:string>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:host>" , type = "<STR_LIT:string>" , default = "<STR_LIT:127.0.0.1>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:port>" , type = "<STR_LIT:int>" , default = <NUM_LIT> , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = "<STR_LIT:test>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:10> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:30> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = string . ascii_uppercase + string . digits , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = sys . maxint , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:20> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = optparser . parse_args ( ) <EOL> if options . help : <EOL> optparser . print_help ( ) <EOL> print ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> config = { <EOL> '<STR_LIT>' : [ ( options . host , options . port ) ] <EOL> } <EOL> READ_OP = <NUM_LIT:0> <EOL> WRITE_OP = <NUM_LIT:1> <EOL> CHOICE_OP = [ READ_OP , WRITE_OP ] <EOL> def operation ( r , w ) : <EOL> rn = r <EOL> wn = w <EOL> while rn + wn > <NUM_LIT:0> : <EOL> if rn > <NUM_LIT:0> and wn > <NUM_LIT:0> : <EOL> op = random . randint ( <NUM_LIT:0> , <NUM_LIT:100> ) <EOL> if op <= r : <EOL> op = READ_OP <EOL> rn -= <NUM_LIT:1> <EOL> else : <EOL> op = WRITE_OP <EOL> wn -= <NUM_LIT:1> <EOL> if wn == <NUM_LIT:0> and rn == <NUM_LIT:0> : <EOL> rn = r <EOL> wn = w <EOL> yield op <EOL> elif rn > <NUM_LIT:0> : <EOL> op = READ_OP <EOL> rn -= <NUM_LIT:1> <EOL> if wn == <NUM_LIT:0> and rn == <NUM_LIT:0> : <EOL> rn = r <EOL> wn = w <EOL> yield op <EOL> elif wn > <NUM_LIT:0> : <EOL> op = WRITE_OP <EOL> wn -= <NUM_LIT:1> <EOL> if wn == <NUM_LIT:0> and rn == <NUM_LIT:0> : <EOL> rn = r <EOL> wn = w <EOL> yield op <EOL> def genstr ( ) : <EOL> return '<STR_LIT>' . join ( random . choice ( options . str_chars ) for _ in range ( random . randrange ( options . str_min , options . str_max , <NUM_LIT:1> ) ) ) <EOL> def genint ( ) : <EOL> return random . randrange ( options . int_min , options . int_max , <NUM_LIT:1> ) <EOL> exitCode = <NUM_LIT:0> <EOL> k = options . keys <EOL> r = options . reads <EOL> w = options . writes <EOL> total_count = <NUM_LIT:0> <EOL> read_count = <NUM_LIT:0> <EOL> write_count = <NUM_LIT:0> <EOL> start = <NUM_LIT:0> <EOL> intervals = [ ] <EOL> heapy = hpy ( ) <EOL> no_of_buckets , range_increment = [ <EOL> int ( elem . strip ( ) ) for elem in options . latency . split ( '<STR_LIT:U+002C>' ) ] <EOL> __buckets = [ <NUM_LIT:0> , <NUM_LIT:1> ] + [ elem << i for i in [ <NUM_LIT:0> ] <EOL> for elem in [ <NUM_LIT:1> ] * ( no_of_buckets - <NUM_LIT:2> ) for i in [ i + range_increment ] ] <EOL> read_bucket = dict . fromkeys ( __buckets , <NUM_LIT:0> ) <EOL> write_bucket = copy . deepcopy ( read_bucket ) <EOL> current_milliseconds_time = lambda : int ( round ( time . time ( ) * <NUM_LIT:1000> ) ) <EOL> def get_latency_table_headers ( ) : <EOL> headers = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] + [ "<STR_LIT>" . format ( i ) for i in __buckets [ <NUM_LIT:2> : ] ] <EOL> return headers <EOL> def increment_counters ( bucket , time_in_millisecond ) : <EOL> if time_in_millisecond <= <NUM_LIT:0> : <EOL> bucket [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> for key in bucket . keys ( ) : <EOL> if time_in_millisecond >= key > <NUM_LIT:0> : <EOL> bucket [ key ] += <NUM_LIT:1> <EOL> def interprete_summary ( ) : <EOL> global total_count , read_count , write_count , read_bucket , write_bucket <EOL> read_summary = [ '<STR_LIT>' ] + [ '<STR_LIT>' . format ( <EOL> int ( round ( read_bucket [ key ] * <NUM_LIT:100> / read_count ) ) ) for key in read_bucket . keys ( ) ] <EOL> write_summary = [ '<STR_LIT>' ] + [ '<STR_LIT>' . format ( <EOL> int ( round ( write_bucket [ key ] * <NUM_LIT:100> / write_count ) ) ) for key in write_bucket . keys ( ) ] <EOL> return read_summary , write_summary <EOL> def total_summary ( ) : <EOL> stop = time . time ( ) <EOL> elapse = ( stop - start ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" . format ( k ) ) <EOL> print ( "<STR_LIT>" . format ( elapse , total_count ) ) <EOL> print ( "<STR_LIT>" . format ( total_count / elapse ) ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> table = [ info for info in interprete_summary ( ) ] <EOL> print ( tabulate ( table , headers = get_latency_table_headers ( ) ) ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( heapy . heap ( ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> try : <EOL> if options . gen == '<STR_LIT:str>' : <EOL> gen = genstr <EOL> else : <EOL> gen = genint <EOL> print ( "<STR_LIT>" , k ) <EOL> keys = [ gen ( ) for _ in range ( k ) ] <EOL> print ( "<STR_LIT>" ) <EOL> print ( ) <EOL> client = aerospike . client ( config ) . connect ( <EOL> options . username , options . password ) <EOL> try : <EOL> signal . signal ( signal . SIGTERM , total_summary ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( ) <EOL> start = time . time ( ) <EOL> print ( "<STR_LIT>" . format ( <NUM_LIT:0> , heapy . heap ( ) ) ) <EOL> for op in operation ( r , w ) : <EOL> key = ( options . namespace , options . set , <EOL> keys [ random . randint ( <NUM_LIT:0> , k - <NUM_LIT:1> ) ] ) <EOL> if op == READ_OP : <EOL> print ( '<STR_LIT>' , key ) if options . verbose else <NUM_LIT:0> <EOL> now = current_milliseconds_time ( ) <EOL> result = client . exists ( key ) <EOL> elapsed = current_milliseconds_time ( ) - now <EOL> print ( '<STR_LIT>' . format ( <EOL> elapsed ) ) if options . verbose else <NUM_LIT:0> <EOL> increment_counters ( read_bucket , elapsed ) <EOL> total_count += <NUM_LIT:1> <EOL> read_count += <NUM_LIT:1> <EOL> elif op == WRITE_OP : <EOL> print ( '<STR_LIT>' , key ) if options . verbose else <NUM_LIT:0> <EOL> rec = { <EOL> '<STR_LIT:key>' : key [ <NUM_LIT:2> ] <EOL> } <EOL> now = current_milliseconds_time ( ) <EOL> client . put ( key , rec ) <EOL> elapsed = current_milliseconds_time ( ) - now <EOL> print ( '<STR_LIT>' . format ( <EOL> elapsed ) ) if options . verbose else <NUM_LIT:0> <EOL> increment_counters ( write_bucket , elapsed ) <EOL> total_count += <NUM_LIT:1> <EOL> write_count += <NUM_LIT:1> <EOL> if options . heap_interval > <NUM_LIT:0> and ( total_count % options . heap_interval ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" . format ( total_count , heapy . heap ( ) ) ) <EOL> except KeyboardInterrupt : <EOL> total_summary ( ) <EOL> except Exception as eargs : <EOL> print ( "<STR_LIT>" . format ( eargs ) , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except Exception as eargs : <EOL> print ( "<STR_LIT>" . format ( eargs ) , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
