1111
1212from cassandra .cluster import Cluster
1313from cassandra .io .asyncorereactor import AsyncoreConnection
14+ from cassandra .policies import HostDistance
1415from cassandra .query import SimpleStatement
1516
1617log = logging .getLogger ()
17- log .setLevel ('INFO' )
1818handler = logging .StreamHandler ()
1919handler .setFormatter (logging .Formatter ("%(asctime)s [%(levelname)s] %(name)s: %(message)s" ))
2020log .addHandler (handler )
3131KEYSPACE = "testkeyspace"
3232TABLE = "testtable"
3333
34- def setup ():
34+ def setup (hosts ):
3535
36- cluster = Cluster (['127.0.0.1' ])
36+ cluster = Cluster (hosts )
37+ cluster .set_core_connections_per_host (HostDistance .LOCAL , 1 )
3738 session = cluster .connect ()
3839
3940 rows = session .execute ("SELECT keyspace_name FROM system.schema_keyspaces" )
@@ -60,16 +61,17 @@ def setup():
6061 )
6162 """ % TABLE )
6263
63- def teardown ():
64- cluster = Cluster (['127.0.0.1' ])
64+ def teardown (hosts ):
65+ cluster = Cluster (hosts )
66+ cluster .set_core_connections_per_host (HostDistance .LOCAL , 1 )
6567 session = cluster .connect ()
6668 session .execute ("DROP KEYSPACE " + KEYSPACE )
6769
6870
6971def benchmark (run_fn ):
7072 options , args = parse_options ()
7173 for conn_class in options .supported_reactors :
72- setup ()
74+ setup (options . hosts )
7375 log .info ("==== %s ====" % (conn_class .__name__ ,))
7476
7577 cluster = Cluster (options .hosts , metrics_enabled = options .enable_metrics )
@@ -88,10 +90,10 @@ def benchmark(run_fn):
8890 log .debug ("Beginning inserts..." )
8991 start = time .time ()
9092 try :
91- run_fn (session , query , values , options .num_ops )
93+ run_fn (session , query , values , options .num_ops , options . threads )
9294 end = time .time ()
9395 finally :
94- teardown ()
96+ teardown (options . hosts )
9597
9698 total = end - start
9799 log .info ("Total time: %0.2fs" % total )
@@ -133,10 +135,14 @@ def parse_options():
133135 help = 'only benchmark with libev connections' )
134136 parser .add_option ('-m' , '--metrics' , action = 'store_true' , dest = 'enable_metrics' ,
135137 help = 'enable and print metrics for operations' )
138+ parser .add_option ('-l' , '--log-level' , default = 'info' ,
139+ help = 'logging level: debug, info, warning, or error' )
136140 options , args = parser .parse_args ()
137141
138142 options .hosts = options .hosts .split (',' )
139143
144+ log .setLevel (options .log_level .upper ())
145+
140146 if options .libev_only :
141147 if not have_libev :
142148 log .error ("libev is not available" )
0 commit comments