Skip to content

Instantly share code, notes, and snippets.

@beltran
Created August 8, 2017 20:24
Show Gist options
  • Save beltran/2835c8de804123104fcf91a559d83cc7 to your computer and use it in GitHub Desktop.
Save beltran/2835c8de804123104fcf91a559d83cc7 to your computer and use it in GitHub Desktop.
import cProfile, pstats, StringIO
from itertools import cycle
from cassandra.concurrent import execute_concurrent
from cassandra.cluster import Cluster
concurrency = 50
TABLE = "testtable"
KEYSPACE = "testkeyspace"
query = "INSERT INTO {0}(k, v) VALUES (%s, %s)".format(TABLE)
warmup_num_ops = int(1e4)
num_ops = int(1e5)
statements = cycle((query,))
warmup_parameters = [(i, i + 1) for i in range(warmup_num_ops)]
warmup_query_and_params = list(zip(statements, warmup_parameters))
cluster = Cluster()
session = cluster.connect()
drop_keyspace ='''DROP KEYSPACE IF EXISTS %s''' % KEYSPACE
session.execute(drop_keyspace)
create_keyspace = '''
CREATE KEYSPACE %s
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}''' % KEYSPACE
session.execute(create_keyspace)
session.set_keyspace(KEYSPACE)
create_table = '''
CREATE TABLE %s.%s (
k int PRIMARY KEY,
v int )''' % (KEYSPACE, TABLE)
session.execute(create_table)
# warmup
print("Starting warming")
execute_concurrent(session, warmup_query_and_params,
concurrency=concurrency, results_generator=False)
parameters = [(i, i + 1) for i in range(num_ops)]
query_and_params = list(zip(statements, parameters))
print("Starting profiling")
pr = cProfile.Profile()
pr.enable()
for _ in range(100):
execute_concurrent(session, query_and_params, concurrency=concurrency, results_generator=False)
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
f = open("stats_execute_concurrent.txt", "w")
ps = pstats.Stats(pr, stream=f).sort_stats(sortby)
ps.print_stats()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment