2013-04-04 15:37:36
来 源
keakon的涂鸦馆
云计算
对大多数应用来说,查询性能是最重要的,这一个数量级的提升也是很有益处的,更何况还少了很多限制。所以如果不需要ORM的话,用Google Cloud SQL取代Datastore是个不错的选择。
编者语:之前的文章中,我们详细介绍了Google Cloud SQL,也刊载了谷歌Google Cloud SQL完全试用指南,今天看看Keakon对于Google Cloud SQL的性能测试

几天前终于申请到Google Cloud SQL的使用资格了,不过一直忙着更新Reader Sharer,直到今晚才有空测试它。

首先创建了一个small类型的instance,因为我用不了多少空间。等了1~2分钟后才提示创建成功,显示已用20MB空间,但是SQL Prompt访问不了。
于是去下载Command Line Tool,结果因为网络问题,没法交换OAuth2 tokens(不清楚是不是GFW的原因)。
但是刷新了一下网页,发现SQL Prompt可以访问了,于是执行SQL来创建数据库和表:
CREATE DATABASE test;
USE test;
CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, value DOUBLE, PRIMARY KEY(id));

然后在绑定的一个HR datastore应用上测试:
from random import random
from time import time
from google.appengine.api import rdbms

rand = [(random(),) for i in xrange(500)]

t = time()
conn = rdbms.connect(instance='keakon:test', database='test')
print time() - t

cursor = conn.cursor()

t = time()
cursor.executemany('INSERT INTO test (value) VALUES (%s)', rand)
print time() - t

t = time()
cursor.execute('SELECT count(*) FROM test')
print time() - t

t = time()
cursor.execute('SELECT id FROM test')
print time() - t

t = time()
cursor.execute('SELECT * FROM test')
print time() - t

t = time()
conn.rollback()
print time() - t

t = time()
cursor.close()
conn.close()
print time() - t
结果:
0.00699996948242
3.8259999752
0.00500011444092
0.235999822617
0.205000162125
0.0639998912811
0.00300002098083
可见连接和关闭数据库还是很快的,但是插入和查询性能并不算好。

接着试试提交:
t = time()
cursor.executemany('INSERT INTO test (value) VALUES (%s)', rand)
conn.commit()
print time() - t

t = time()
cursor.execute('DELETE FROM test')
conn.commit()
print time() - t
结果:
3.3180000782
0.132999897003
可能是测试的波动,插入变快了。比较让我奇怪的是删除,居然比查询还快。

再试了下清空表,即改成cursor.execute('TRUNCATE test')。结果:
2.96399998665
0.128000020981
这次插入更快了,不解…

值得一提的是,我在删除的时候写错了SQL语句,结果报错了。然后再次删除就一直被锁,但是查询和插入都正常,估计是那个事务一直没结束。
最后我试了下TRUNCATE,花了22秒终于成功了。之后再测试时,即使出错也没遇到被锁的情况了,非常奇怪…


然后对比测试HR datastore,使用同一个应用:
from random import random
from time import time
from google.appengine.ext import db

class Test(db.Model):
    value = db.FloatProperty()

entites = [Test(value=random()) for i in xrange(500)]

t = time()
db.put(entites)
print time() - t

t = time()
Test.all().count(None)
print time() - t

t = time()
keys = Test.all(keys_only=True).fetch(500)
print time() - t

t = time()
Test.all().fetch(500)
print time() - t

t = time()
db.delete(keys)
print time() - t
结果:
3.31100010872
0.0799999237061
0.208999872208
2.34000015259
0.861999988556
其中count和查询慢了一个数量级,删除慢约6倍,插入和查询key则差不多。

接着又在一个MS datastore的应用上测试了下,结果是:
1.04965305328
0.109894037247
0.0680358409882
0.660396099091
0.251960992813
插入快2倍,查询慢一个数量级,其他差不多。

对大多数应用来说,查询性能是最重要的,这一个数量级的提升也是很有益处的,更何况还少了很多限制。所以如果不需要ORM的话,用Google Cloud SQL取代Datastore是个不错的选择。至此本次测试暂时结束,大家也看到了Google Cloud SQL性能表现还是很不错的。

本文来自keakon的涂鸦馆

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。