prepare:
create keyspace ycsb WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 2 };USE ycsb; CREATE TABLE users ( firstname text, lastname text, age int, email text, city text, PRIMARY KEY (lastname));
用bash开5个python进程来测试写入性能,注意:当集群数目增加,应当增加bash脚本的并发进程数!
test_cass.py
from cassandra.cluster import Clusterfrom cassandra.query import BatchStatementfrom time import timecluster = Cluster(["10.178.204.225"])#session = cluster.connect('my_keyspace')session = cluster.connect('ycsb')#session.execute("TRUNCATE my_keyspace.users;")session.execute("TRUNCATE ycsb.users;")start = time()data_cnt = 1000000bulk_cnt = 100for i in range(data_cnt/bulk_cnt): insert_user = session.prepare("insert into users (lastname, age, city, email, firstname) values (?, ?, 'Austin', 'bob@example.com', 'Bob')") batch = BatchStatement() for j in range(bulk_cnt): batch.add(insert_user, ("Jones"+str(i*bulk_cnt+j), j)) session.execute(batch)end = time()interval = end-startprint "time:", interval, "through ops/per second:", data_cnt/intervalresult = session.execute("select * from users where lastname='Jones0'")for x in result: print x.age, x.firstname, x.lastname#result = session.execute("select count(*) from users")#for x in result:# print xresult = session.execute("select * from users where lastname='Jones%s'" % (data_cnt-1))for x in result: print x.age, x.firstname, x.lastname
bash run.sh
for((i=0;i<5;i++)) do python test_cass.py &donewaitecho "done"