
这三家我都有使用
先说 使用感受
这几天有想法 测一下 这三家提供的 mysql 读写性能如何, 于是写了下面的脚本。
先说一下这三家 mysql 的配置:
所以这是一个不严谨的测试,因为这些机器都是早就买好的。所以无法做到测试环境一模一样。
每家各测试多次,结果比较稳定,就选取其中的一次结果:
INSERT ONE 3.25666999817 INSERT MANY 0.217604875565 QUERY 3.51868581772 INSERT ONE 5.17626905441 INSERT MANY 1.33850288391 QUERY 2.40842795372 INSERT ONE 5.36786603928 INSERT MANY 0.239859104156 QUERY 5.58612704277 # -*- coding: utf-8 -*- import os import time import random import base64 import MySQLdb MYSQL_HOST = '127.0.0.1' MYSQL_PORT = 3306 MYSQL_USER = 'root' MYSQL_PASSWORD = 'root' MYSQL_DB = 'bench_test' max_num = 10000 conn = MySQLdb.connect( host=MYSQL_HOST, port=MYSQL_PORT, db=MYSQL_DB, user=MYSQL_USER, passwd=MYSQL_PASSWORD ) # 数据库需要提前建立好,但是表不用 cursor = conn.cursor() sql = """create table if not exists test ( id integer not null primary key, age integer not null, name varchar(255) not null )""" cursor.execute(sql) conn.commit() cursor.close() # 生成测试数据 DATA = [] for i in range(max_num): DATA.append((i+1, i+1, base64.b64encode(os.urandom(64)))) def insert_one_test(): cursor = conn.cursor() cursor.execute("delete from test") start = time.time() for value in DATA: cursor.execute('insert into test (id, age, name) values (%s, %s, %s)', value) conn.commit() cursor.close() print "INSERT ONE", time.time() - start def insert_many_test(): cursor = conn.cursor() cursor.execute("delete from test") start = time.time() start_index = 0 batch_amount = 2000 while start_index < max_num: values = DATA[start_index: start_index+batch_amount] cursor.executemany("insert into test (id, age, name) values (%s, %s, %s)", values) start_index += batch_amount conn.commit() cursor.close() print "INSERT MANY", time.time() - start def query_test(): cursor = conn.cursor() start = time.time() for i in range(10000 * 1): _id = random.randint(1, max_num) cursor.execute("select id, age, name from test where id = %s", (_id,)) result = cursor.fetchone() assert result[1] == _id print "QUERY", time.time() - start if __name__ == '__main__': insert_one_test() insert_many_test() query_test() 1 huobazi 2015-11-03 09:15:42 +08:00 谢谢 |
2 zhangv 2015-11-03 16:57:07 +08:00 青云+1 |
3 bingwenshi 2015-11-18 00:33:40 +08:00 只有真正的用户才会认证的做这样的性能测试, 不像很多人在没有深度使用之前,就在网络上主观的乱喷 |