Skip to content

Instantly share code, notes, and snippets.

@kmuthukk
Last active June 3, 2020 22:30
Show Gist options
  • Select an option

  • Save kmuthukk/3265b5a629ac22b05136f1d97f61ab94 to your computer and use it in GitHub Desktop.

Select an option

Save kmuthukk/3265b5a629ac22b05136f1d97f61ab94 to your computer and use it in GitHub Desktop.
Sample program to measure overhead of authenticated connections. Test case for https://github.com/YugaByte/yugabyte-db/issues/4596
# pip install yb-cassandra-driver
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import time
import random
from multiprocessing.dummy import Pool as ThreadPool
num_sessions=10
def create_table():
auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra')
cluster = Cluster(['127.0.0.1'], auth_provider=auth_provider)
session = cluster.connect()
start_time = time.time()
session.execute("""CREATE KEYSPACE IF NOT EXISTS k""");
session.execute("""USE k""");
session.execute("""DROP TABLE IF EXISTS users""");
now_time = time.time()
print("Dropped (if exists): users table")
print("Time: %s ms" % ((now_time - start_time) * 1000))
print("====================")
start_time = time.time()
session.execute("""
CREATE TABLE IF NOT EXISTS users(
id text,
name text,
age int,
PRIMARY KEY(id)
) WITH TRANSACTIONS = {'enabled' : true}
""")
now_time = time.time()
print("Created users table")
print("Time: %s ms" % ((now_time - start_time) * 1000))
print("====================")
def session_slave(thread_num):
thread_id = str(thread_num)
start_time = time.time()
auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra')
cluster = Cluster(['127.0.0.1'], auth_provider=auth_provider)
session = cluster.connect()
now_time = time.time()
print("Thread-" + thread_id + ": Connect Time: %s ms" % ((now_time - start_time) * 1000))
def start_sessions():
pool = ThreadPool(num_sessions)
t1 = time.time()
results = pool.map(session_slave, range(num_sessions))
t2 = time.time()
print("====================")
print("Total Time: %s ms" % ((t2 - t1) * 1000))
print("====================")
# Main
create_table()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
start_sessions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment