最近在做一个项目,数据库使用的是 Redis。在设计数据结构时,不知道哪种实现是最优的,于是做了下测试。 测试环境如下: OS X10.8.3 Redis 2.6.12 Python 2.7.4 redis-py 2.7.2 hiredis 0.1.1 ujson 1.30 MessagePack 0.3.0 注意: 因为是拿 Python 测试的
最近在做一个项目,数据库使用的是 Redis。在设计数据结构时,不知道哪种实现是最优的,于是做了下测试。OS X10.8.3注意:
Redis 2.6.12
Python 2.7.4
redis-py 2.7.2
hiredis 0.1.1
ujson 1.30
MessagePack 0.3.0
# -*- coding: utf-8 -*- import cPickle import json import time import zlib import msgpack import redis import ujson class Timer: def __enter__(self): self.start = time.time() return self def __exit__(self, *args): self.end = time.time() self.interval = self.end - self.start def test(function): def wrapper(*args, **kwargs): args_list = [] if args: args_list.append(','.join((str(arg) for arg in args))) if kwargs: args_list.append(','.join('%s=%s' % (key, value) for key, value in kwargs.iteritems())) print 'call %s(%s):' % (function.func_name, ', '.join(args_list)) redis_client.flushall() print 'memory:', redis_client.info()['used_memory_human'] with Timer() as timer: result = function(*args, **kwargs) print 'time:', timer.interval print 'memory:', redis_client.info()['used_memory_human'] print return result return wrapper redis_client = redis.Redis() pipe = redis_client.pipeline(transaction=False) articles = [{ 'id': i, 'title': u'团结全世界正义力量痛击日本', 'content': u'近期日本社会有四种感觉极度高涨,即二战期间日本军国主义扩张战争的惨败在日本右翼势力内心留下的耻辱感;被美国长期占领和控制的压抑感;经济长期停滞不前的焦虑感;对中国快速崛起引发的失落感。为此,日本为了找到一个发泄口,对中国采取了一系列挑衅行为,我们不能听之任之。现在全国13亿人要万众一心,团结起来,拿出决心、意志和能力,果断实施对等反击。在这场反击日本右翼势力的反攻倒算中,中国不是孤立的,我们要团结全世界一切反法西斯战争的正义力量,痛击日本对国际正义的挑战。', 'source_text': u'环球时报', 'source_url': 'http://opinion.huanqiu.com/column/mjzl/2012-09/3174337.html', 'time': '2012-09-13 09:23', 'is_public': True } for i in xrange(10000)] @test def test_hash(): for article in articles: pipe.hmset('article:%d' % article['id'], article) pipe.execute() @test def test_json_hash(): for article in articles: pipe.hset('article', article['id'], json.dumps(article)) pipe.execute() @test def test_ujson_hash(): for article in articles: pipe.hset('article', article['id'], ujson.dumps(article)) pipe.execute() @test def test_ujson_string(): for article in articles: pipe.set('article:%d' % article['id'], ujson.dumps(article)) pipe.execute() @test def test_zlib_ujson_string(): for article in articles: pipe.set('article:%d' % article['id'], zlib.compress(ujson.dumps(article, ensure_ascii=False))) pipe.execute() @test def test_msgpack(): for article in articles: pipe.hset('article', article['id'], msgpack.packb(article)) pipe.execute() @test def test_pickle_string(): for article in articles: pipe.set('article:%d' % article['id'], cPickle.dumps(article)) pipe.execute() @test def test_json_without_ensure_ascii(): for article in articles: pipe.hset('article', article['id'], json.dumps(article, ensure_ascii=False)) pipe.execute() @test def test_ujson_without_ensure_ascii(): for article in articles: pipe.hset('article', article['id'], ujson.dumps(article, ensure_ascii=False)) pipe.execute() def test_ujson_shard_id(): @test def test_ujson_shard_id_of_size(size): for article in articles: article_id = article['id'] pipe.hset('article:%d' % (article_id / size), article_id % size, ujson.dumps(article, ensure_ascii=False)) pipe.execute() for size in (2, 4, 8, 10, 16, 32, 64, 100, 128, 256, 500, 512, 513, 1000, 1024, 1025, 2048, 4096, 8092): test_ujson_shard_id_of_size(size) test_ujson_shard_id_of_size(512) for key, value in sorted(globals().copy().iteritems(), key=lambda x:x[0]): if key.startswith('test_'): value()
原文地址:关于 Redis 的几种数据库设计方案的内存占用测试, 感谢原作者分享。