Skip to content

Commit 83c019e

Browse files
committed
Add twisted reactor to benchmarking tools
1 parent 57a4799 commit 83c019e

1 file changed

Lines changed: 44 additions & 26 deletions

File tree

benchmarks/base.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
sys.path.append(dirname)
2727
sys.path.append(os.path.join(dirname, '..'))
2828

29+
import cassandra
2930
from cassandra.cluster import Cluster
3031
from cassandra.io.asyncorereactor import AsyncoreConnection
3132
from cassandra.policies import HostDistance
@@ -44,46 +45,55 @@
4445
except ImportError as exc:
4546
pass
4647

47-
KEYSPACE = "testkeyspace"
48+
have_twisted = False
49+
try:
50+
from cassandra.io.twistedreactor import TwistedConnection
51+
have_twisted = True
52+
supported_reactors.append(TwistedConnection)
53+
except ImportError as exc:
54+
log.exception("Error importing twisted")
55+
pass
56+
57+
KEYSPACE = "testkeyspace" + str(int(time.time()))
4858
TABLE = "testtable"
4959

5060

5161
def setup(hosts):
62+
log.info("Using 'cassandra' package from %s", cassandra.__path__)
5263

5364
cluster = Cluster(hosts)
5465
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
55-
session = cluster.connect()
56-
57-
rows = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
58-
if KEYSPACE in [row[0] for row in rows]:
59-
log.debug("dropping existing keyspace...")
60-
session.execute("DROP KEYSPACE " + KEYSPACE)
61-
62-
log.debug("Creating keyspace...")
63-
session.execute("""
64-
CREATE KEYSPACE %s
65-
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' }
66-
""" % KEYSPACE)
67-
68-
log.debug("Setting keyspace...")
69-
session.set_keyspace(KEYSPACE)
70-
71-
log.debug("Creating table...")
72-
session.execute("""
73-
CREATE TABLE %s (
74-
thekey text,
75-
col1 text,
76-
col2 text,
77-
PRIMARY KEY (thekey, col1)
78-
)
79-
""" % TABLE)
66+
try:
67+
session = cluster.connect()
68+
69+
log.debug("Creating keyspace...")
70+
session.execute("""
71+
CREATE KEYSPACE %s
72+
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' }
73+
""" % KEYSPACE)
74+
75+
log.debug("Setting keyspace...")
76+
session.set_keyspace(KEYSPACE)
77+
78+
log.debug("Creating table...")
79+
session.execute("""
80+
CREATE TABLE %s (
81+
thekey text,
82+
col1 text,
83+
col2 text,
84+
PRIMARY KEY (thekey, col1)
85+
)
86+
""" % TABLE)
87+
finally:
88+
cluster.shutdown()
8089

8190

8291
def teardown(hosts):
8392
cluster = Cluster(hosts)
8493
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
8594
session = cluster.connect()
8695
session.execute("DROP KEYSPACE " + KEYSPACE)
96+
cluster.shutdown()
8797

8898

8999
def benchmark(thread_class):
@@ -124,6 +134,7 @@ def benchmark(thread_class):
124134

125135
end = time.time()
126136
finally:
137+
cluster.shutdown()
127138
teardown(options.hosts)
128139

129140
total = end - start
@@ -164,6 +175,8 @@ def parse_options():
164175
help='only benchmark with asyncore connections')
165176
parser.add_option('--libev-only', action='store_true', dest='libev_only',
166177
help='only benchmark with libev connections')
178+
parser.add_option('--twisted-only', action='store_true', dest='twisted_only',
179+
help='only benchmark with Twisted connections')
167180
parser.add_option('-m', '--metrics', action='store_true', dest='enable_metrics',
168181
help='enable and print metrics for operations')
169182
parser.add_option('-l', '--log-level', default='info',
@@ -184,6 +197,11 @@ def parse_options():
184197
log.error("libev is not available")
185198
sys.exit(1)
186199
options.supported_reactors = [LibevConnection]
200+
elif options.twisted_only:
201+
if not have_twisted:
202+
log.error("Twisted is not available")
203+
sys.exit(1)
204+
options.supported_reactors = [TwistedConnection]
187205
else:
188206
options.supported_reactors = supported_reactors
189207
if not have_libev:

0 commit comments

Comments
 (0)