|
import argparse |
|
import asyncio |
|
import time |
|
from acsylla import create_cluster, create_statement, Session |
|
import scyllapy |
|
|
|
|
|
async def build_insert_statement(session: Session, i: int): |
|
statement = create_statement( |
|
"INSERT INTO test (id, val) VALUES (?, ?)", parameters=2 |
|
) |
|
statement.bind_list([i, i]) |
|
await session.execute(statement) |
|
|
|
|
|
async def acsylla_run_selects(session: Session, num: int): |
|
await asyncio.gather( |
|
*[session.execute(create_statement("SELECT * FROM test;")) for _ in range(num)] |
|
) |
|
|
|
|
|
async def acsylla_run_inserts(session: Session, num: int): |
|
await asyncio.gather(*[build_insert_statement(session, i) for i in range(num)]) |
|
|
|
|
|
async def scyllapy_run_selects(scylla: scyllapy.Scylla, num: int): |
|
await asyncio.gather(*[scylla.execute("SELECT * FROM test;") for _ in range(num)]) |
|
|
|
|
|
async def scyllapy_run_inserts(scylla: scyllapy.Scylla, num: int): |
|
await asyncio.gather( |
|
*[ |
|
scylla.execute("INSERT INTO test(id, val) VALUES (?, ?)", (i, i)) |
|
for i in range(num) |
|
] |
|
) |
|
|
|
|
|
def parse_args() -> argparse.Namespace: |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("--uvloop", help="Use uvloop", action="store_true") |
|
parser.add_argument("scylla_urls", help="Scylla Host IPs", nargs="+") |
|
parser.add_argument( |
|
"--num-inserts", |
|
help="Number of inserts", |
|
type=int, |
|
default=1000, |
|
) |
|
parser.add_argument( |
|
"--num-elements", |
|
help="Number of elements", |
|
type=int, |
|
default=100, |
|
) |
|
parser.add_argument( |
|
"--num-selects", |
|
help="Number of selects", |
|
type=int, |
|
default=5000, |
|
) |
|
return parser.parse_args() |
|
|
|
|
|
async def main(args: argparse.Namespace) -> None: |
|
cluster = create_cluster( |
|
args.scylla_urls, |
|
retry_policy="fallthrough", |
|
username="cassandra", |
|
password="cassandra", |
|
) |
|
|
|
scyllapy_session = scyllapy.Scylla( |
|
args.scylla_urls, |
|
username="cassandra", |
|
password="cassandra", |
|
) |
|
await scyllapy_session.startup() |
|
await scyllapy_session.execute( |
|
"CREATE KEYSPACE IF NOT EXISTS benchmarks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" |
|
) |
|
await scyllapy_session.use_keyspace("benchmarks") |
|
acsylla_session = await cluster.create_session("benchmarks") |
|
await scyllapy_session.execute( |
|
"CREATE TABLE IF NOT EXISTS test (id INT PRIMARY KEY, val INT)" |
|
) |
|
print(f" {args.num_inserts} inserts ".center(40, "#")) |
|
await scyllapy_session.execute("TRUNCATE test") |
|
start = time.monotonic() |
|
await scyllapy_run_inserts(scyllapy_session, args.num_inserts) |
|
print("ScyllaPy", time.monotonic() - start) |
|
|
|
await scyllapy_session.execute("TRUNCATE test") |
|
start = time.monotonic() |
|
await acsylla_run_inserts(acsylla_session, args.num_inserts) |
|
print("ACSYLLA ", time.monotonic() - start) |
|
|
|
print(f" {args.num_selects} selects ".center(40, "#")) |
|
await scyllapy_session.execute("TRUNCATE test") |
|
await scyllapy_run_inserts(scyllapy_session, args.num_elements) |
|
start = time.monotonic() |
|
await scyllapy_run_selects(scyllapy_session, args.num_selects) |
|
print("ScyllaPy", time.monotonic() - start) |
|
|
|
await scyllapy_session.execute("TRUNCATE test") |
|
await acsylla_run_inserts(acsylla_session, args.num_elements) |
|
start = time.monotonic() |
|
await acsylla_run_selects(acsylla_session, args.num_selects) |
|
print("ACSYLLA ", time.monotonic() - start) |
|
|
|
|
|
if __name__ == "__main__": |
|
args = parse_args() |
|
if args.uvloop: |
|
print("Installing uvloop") |
|
import uvloop |
|
|
|
uvloop.install() |
|
asyncio.run(main(args)) |