1+ from cProfile import Profile
12import logging
23import os .path
34import sys
5+ from threading import Thread
46import time
57from optparse import OptionParser
8+
69from greplin import scales
710
811dirname = os .path .dirname (os .path .abspath (__file__ ))
@@ -68,7 +71,7 @@ def teardown(hosts):
6871 session .execute ("DROP KEYSPACE " + KEYSPACE )
6972
7073
71- def benchmark (run_fn ):
74+ def benchmark (thread_class ):
7275 options , args = parse_options ()
7376 for conn_class in options .supported_reactors :
7477 setup (options .hosts )
@@ -87,10 +90,24 @@ def benchmark(run_fn):
8790 """ .format (table = TABLE ))
8891 values = {'key' : 'key' , 'a' : 'a' , 'b' : 'b' }
8992
93+ per_thread = options .num_ops / options .threads
94+ threads = []
95+
9096 log .debug ("Beginning inserts..." )
9197 start = time .time ()
9298 try :
93- run_fn (session , query , values , options .num_ops , options .threads )
99+ for i in range (options .threads ):
100+ thread = thread_class (i , session , query , values , per_thread , options .profile )
101+ thread .daemon = True
102+ threads .append (thread )
103+
104+ for thread in threads :
105+ thread .start ()
106+
107+ for thread in threads :
108+ while thread .is_alive ():
109+ thread .join (timeout = 0.5 )
110+
94111 end = time .time ()
95112 finally :
96113 teardown (options .hosts )
@@ -137,6 +154,9 @@ def parse_options():
137154 help = 'enable and print metrics for operations' )
138155 parser .add_option ('-l' , '--log-level' , default = 'info' ,
139156 help = 'logging level: debug, info, warning, or error' )
157+ parser .add_option ('-p' , '--profile' , action = 'store_true' , dest = 'profile' ,
158+ help = 'Profile the run' )
159+
140160 options , args = parser .parse_args ()
141161
142162 options .hosts = options .hosts .split (',' )
@@ -154,3 +174,24 @@ def parse_options():
154174 options .supported_reactors = supported_reactors
155175
156176 return options , args
177+
178+
179+ class BenchmarkThread (Thread ):
180+
181+ def __init__ (self , thread_num , session , query , values , num_queries , profile ):
182+ Thread .__init__ (self )
183+ self .thread_num = thread_num
184+ self .session = session
185+ self .query = query
186+ self .values = values
187+ self .num_queries = num_queries
188+ self .profiler = Profile () if profile else None
189+
190+ def start_profile (self ):
191+ if self .profiler :
192+ self .profiler .enable ()
193+
194+ def finish_profile (self ):
195+ if self .profiler :
196+ self .profiler .disable ()
197+ self .profiler .dump_stats ('profile-%d' % self .thread_num )
0 commit comments