【发布时间】:2011-06-15 20:00:21
【问题描述】:
对于具有大量插入(1M/天)的项目,我必须选择 Cassandra 或 MongoDB(或其他 nosql 数据库,我接受建议)。 所以我创建了一个小测试来测量写入性能。这是要插入 Cassandra 的代码:
import time
import os
import random
import string
import pycassa
def get_random_string(string_length):
return ''.join(random.choice(string.letters) for i in xrange(string_length))
def connect():
"""Connect to a test database"""
connection = pycassa.connect('test_keyspace', ['localhost:9160'])
db = pycassa.ColumnFamily(connection,'foo')
return db
def random_insert(db):
"""Insert a record into the database. The record has the following format
ID timestamp
4 random strings
3 random integers"""
record = {}
record['id'] = str(time.time())
record['str1'] = get_random_string(64)
record['str2'] = get_random_string(64)
record['str3'] = get_random_string(64)
record['str4'] = get_random_string(64)
record['num1'] = str(random.randint(0, 100))
record['num2'] = str(random.randint(0, 1000))
record['num3'] = str(random.randint(0, 10000))
db.insert(str(time.time()), record)
if __name__ == "__main__":
db = connect()
start_time = time.time()
for i in range(1000000):
random_insert(db)
end_time = time.time()
print "Insert time: %lf " %(end_time - start_time)
在 Mongo 中插入的代码与更改连接功能相同:
def connect():
"""Connect to a test database"""
connection = pymongo.Connection('localhost', 27017)
db = connection.test_insert
return db.foo2
结果是在 Cassandra 中插入 ~1046 秒,在 Mongo 中完成 ~437 秒。 假设 Cassandra 比 Mongo 插入数据要快得多。那么,我做错了什么?
【问题讨论】:
-
你有没有用更多的线程再次测试,如果是,你能分享结果吗?
标签: python mongodb cassandra nosql