Skip to content

Instantly share code, notes, and snippets.

@kmuthukk
Created April 20, 2023 02:36
Show Gist options
  • Save kmuthukk/13b6928c0e4be87ace3595d926daa7f5 to your computer and use it in GitHub Desktop.
Save kmuthukk/13b6928c0e4be87ace3595d926daa7f5 to your computer and use it in GitHub Desktop.
import os
import json
import time
from cassandra.cluster import Cluster, ExecutionProfile
cluster = Cluster(['127.0.0.1'])
profile = ExecutionProfile()
profile.request_timeout = 60
session = cluster.connect()
session.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;')
session.execute('DROP TABLE IF EXISTS ybdemo.ugent;')
session.execute("CREATE TABLE IF NOT EXISTS ybdemo.ugent (id int PRIMARY KEY, data jsonb) with transactions = {'enabled': 'true'};")
prepared = session.prepare("INSERT INTO ybdemo.ugent(id, data) VALUES (?, ?)")
for i in range (0,23):
size = 2**i
if (size > 4 * 1024 * 1024):
size = 4 * 1024 * 1024
data = dict(text='#'*size)
data = json.dumps(data, sort_keys=True)
num_rows = 10
try:
ts = time.time()
for j in range(0, num_rows):
bound = prepared.bind((i, data))
session.execute(bound)
except Exception as e:
raise
finally:
print("Size of JSON=" + str(len(data)), "Insert Time: " + str((time.time() - ts) * 1000 / num_rows) + " ms")
prepared_select = session.prepare("SELECT * FROM ybdemo.ugent where id = ?")
for i in range (0,23):
num_rows = 10
try:
ts = time.time()
for j in range(0, num_rows):
bound = prepared_select.bind((i,))
results = session.execute(bound)
except Exception as e:
raise
finally:
row_size = len(results[0].data)
print("Size of JSON=" + str(row_size), "Select Time: " + str((time.time() - ts) * 1000 / num_rows) + " ms")
cluster.shutdown()
@kmuthukk
Copy link
Author

kmuthukk commented Apr 20, 2023

Tested on RF=1 devserver, on 2.17.0.0 version of YugabyteDB.

$ python ~/notes/json_ycql_prepared.py
('Size of JSON=13', 'Insert Time: 0.807094573975 ms')
('Size of JSON=14', 'Insert Time: 0.783514976501 ms')
('Size of JSON=16', 'Insert Time: 0.790500640869 ms')
('Size of JSON=20', 'Insert Time: 0.79550743103 ms')
('Size of JSON=28', 'Insert Time: 0.765109062195 ms')
('Size of JSON=44', 'Insert Time: 0.789093971252 ms')
('Size of JSON=76', 'Insert Time: 0.869417190552 ms')
('Size of JSON=140', 'Insert Time: 0.924611091614 ms')
('Size of JSON=268', 'Insert Time: 0.886011123657 ms')
('Size of JSON=524', 'Insert Time: 0.783801078796 ms')
('Size of JSON=1036', 'Insert Time: 0.761008262634 ms')
('Size of JSON=2060', 'Insert Time: 0.854086875916 ms')
('Size of JSON=4108', 'Insert Time: 0.9113073349 ms')
('Size of JSON=8204', 'Insert Time: 0.849103927612 ms')
('Size of JSON=16396', 'Insert Time: 1.02159976959 ms')
('Size of JSON=32780', 'Insert Time: 1.11610889435 ms')
('Size of JSON=65548', 'Insert Time: 1.93269252777 ms')
('Size of JSON=131084', 'Insert Time: 2.08280086517 ms')
('Size of JSON=262156', 'Insert Time: 3.66239547729 ms')
('Size of JSON=524300', 'Insert Time: 5.22360801697 ms')
('Size of JSON=1048588', 'Insert Time: 53.6099910736 ms')
('Size of JSON=2097164', 'Insert Time: 64.7066831589 ms')
('Size of JSON=4194316', 'Insert Time: 84.7468852997 ms')
('Size of JSON=12', 'Select Time: 1.11391544342 ms')
('Size of JSON=13', 'Select Time: 1.05621814728 ms')
('Size of JSON=15', 'Select Time: 1.05907917023 ms')
('Size of JSON=19', 'Select Time: 1.04241371155 ms')
('Size of JSON=27', 'Select Time: 1.05140209198 ms')
('Size of JSON=43', 'Select Time: 1.02601051331 ms')
('Size of JSON=75', 'Select Time: 1.04308128357 ms')
('Size of JSON=139', 'Select Time: 1.0381937027 ms')
('Size of JSON=267', 'Select Time: 1.10881328583 ms')
('Size of JSON=523', 'Select Time: 1.10812187195 ms')
('Size of JSON=1035', 'Select Time: 1.08261108398 ms')
('Size of JSON=2059', 'Select Time: 1.2228012085 ms')
('Size of JSON=4107', 'Select Time: 1.11339092255 ms')
('Size of JSON=8203', 'Select Time: 1.12988948822 ms')
('Size of JSON=16395', 'Select Time: 1.44610404968 ms')
('Size of JSON=32779', 'Select Time: 1.49118900299 ms')
('Size of JSON=65547', 'Select Time: 1.68738365173 ms')
('Size of JSON=131083', 'Select Time: 2.13680267334 ms')
('Size of JSON=262155', 'Select Time: 2.79409885406 ms')
('Size of JSON=524299', 'Select Time: 5.07690906525 ms')
('Size of JSON=1048587', 'Select Time: 8.1072807312 ms')
('Size of JSON=2097163', 'Select Time: 16.6146993637 ms')
('Size of JSON=4194315', 'Select Time: 32.023692131 ms')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment