Ubuntu下安装Tokyo Cabinet和Tokyo Tyrant, 并测试pytyrant

tokyo cabinet : 下载 wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.36.tar.gz

依靠 包:

zlib: sudo apt-get install zlib1g-dev

bzip2: sudo apt-get install libbz2-dev

安装:

tar zxvf tokyocabinet-1.4.36.tar.gz

cd tokyocabinet-1.4.36

./configure

make

sudo make install

tokyo tyrant : 下载 wget http://1978th.net/tokyotyrant/tokyotyrant-1.1.37.tar.gz

安装:

tar zxvf tokyotyrant-1.1.37.tar.gz

cd tokyotyrant-1.1.37

./configure

make

sudo make install

运行tokyo tyrant

ttserver -dmn -pid /home/mk2/ttserver/tt.pid -log /home/mk2/ttserver/tt.log -le -ulog /home/mk2/ttserver/ -ulim 128m /home/mk2/ttserver/data.tcb#bnum=100000

这里需要改为:

完成后可在usr/local/bin里启动 ttservctl start启动默认安装的服务,本机端口1978。
可以马上用ps -ef | grep ttserver 语句看一下启动服务的情况。

# tcb: b+ tree格式, 在内存中缓存最热门的10w条查询

运行pytyrant.py , 代码在: http://code.google.com/p/pytyrant/source/browse/trunk/pytyrant.py

没有任 何异常输出? 那就成功了!!!

与 memcache对比测试

benchmark.py:

!/usr/bin/envpython

# -*-coding:utf-8-*-
""" benchmarktestforpytyrant
"""
import time
import threading

import memcache
import pytyrant


class worker(threading.thread):

def __init__ (self,index,bench,count = 1000 ):
self._count
= count
self._bench
= bench
self._index
= index
super(worker,self).
__init__ ()

def run(self):
self._bench(self._count,self.show)

def show(self,s):
print ' thread%d ' % self._index,s


def _benchmark_threads(bench,count,threads):
print ' - ' * 80
print ' %s:%dthreads,%dtimes ' % (bench. __name__ ,threads,count)
ts
= []
for i in range(threads):
t
= worker(i,bench,count / threads)
ts.append(t)
t.start()
for t in ts:
t.join()

def show(s):
print s

# onethreadset
def set_benchmark(count = 100000 ,display = show):
mem
= memcache.client([ ' 127.0.0.1:11211 ' ])
tt
= pytyrant.pytyrant.open( ' 127.0.0.1 ' , 1978 )

start
= time.time()
for i in xrange(count):
mem.set(
' key_%d ' % i, ' value_%d ' % i)
if display:
display(
' memcache%dset:%s ' % (count,time.time() - start))

start
= time.time()
for i in xrange(count):
tt[
' key_%d ' % i] = ' value_%d ' % i
if display:
display(
' tokyotyrant%dset:%s ' % (count,time.time() - start))

def set_benchmark_threads(count = 100000 ,threads = 20 ):
_benchmark_threads(set_benchmark,count,threads)

# onethreadget
def get_benchmark(count = 100000 ,display = show):
mem
= memcache.client([ ' 127.0.0.1:11211 ' ])
tt
= pytyrant.pytyrant.open( ' 127.0.0.1 ' , 1978 )

start
= time.time()
for i in xrange(count):
assert mem.get( ' key_%d ' % i) == ' value_%d ' % i
display(
' memcache%dget:%s ' % (count,time.time() - start))

start
= time.time()
for i in xrange(count):
assert tt[ ' key_%d ' % i] == ' value_%d ' % i
display(
' tokyotyrant%dget:%s ' % (count,time.time() - start))

def get_benchmark_threads(count = 100000 ,threads = 20 ):
_benchmark_threads(get_benchmark,count,threads)

# onethreadgetallmiss
def get_miss_benchmark(count = 100000 ,display = show):
mem
= memcache.client([ ' 127.0.0.1:11211 ' ])
tt
= pytyrant.pytyrant.open( ' 127.0.0.1 ' , 1978 )

start
= time.time()
for i in xrange(count):
assert mem.get( ' key_%d_miss ' % i) is none
display(
' memcache%dgetmiss:%s ' % (count,time.time() - start))

start
= time.time()
for i in xrange(count):
assert tt.get( ' key_%d_miss ' % i,none) is none
display(
' tokyotyrant%dgetmiss:%s ' % (count,time.time() - start))

def get_miss_benchmark_threads(count = 100000 ,threads = 20 ):
_benchmark_threads(get_miss_benchmark,count,threads)


if __name__ == ' __main__ ' :
print ' onethreads '
print ' set_benchmark '
set_benchmark()
print ' - ' * 80
print ' get_benchmark '
get_benchmark()
print ' - ' * 80
print ' get_miss_benchmark '
get_miss_benchmark()

set_benchmark_threads()
get_benchmark_threads()
get_miss_benchmark_threads()

测试输 出:

one threads
set_benchmark
memcache 100000 set: 10.5929949284
tokyo tyrant 100000 set: 9.69395589828
--------------------------------------------------------------------------------
get_benchmark
memcache 100000 get: 10.9661550522
tokyo tyrant 100000 get: 11.5382130146
--------------------------------------------------------------------------------
get_miss_benchmark
memcache 100000 get miss: 9.16992592812
tokyo tyrant 100000 get miss: 10.9790480137
--------------------------------------------------------------------------------
set_benchmark: 20 threads, 100000 times
thread 5 memcache 5000 set: 9.16596198082
thread 1 memcache 5000 set: 9.2044479847
thread 2 memcache 5000 set: 9.5196750164
thread 10 memcache 5000 set: 9.78295493126
thread 9 memcache 5000 set: 10.1644408703
thread 8 memcache 5000 set: 10.2827599049
thread 3 memcache 5000 set: 10.3494279385
thread 18 memcache 5000 set: 10.5312678814
thread 14 memcache 5000 set: 10.5295097828
thread 13 memcache 5000 set: 10.5583910942
thread 6 memcache 5000 set: 10.64412117
thread 11 memcache 5000 set: 10.7909929752
thread 7 memcache 5000 set: 10.8441131115
thread 12 memcache 5000 set: 10.9090180397
thread 16 memcache 5000 set: 10.9221849442
thread 4 memcache 5000 set: 10.9808840752
thread 17 memcache 5000 set: 11.0821311474
thread 0 memcache 5000 set: 11.135324955
thread 15 memcache 5000 set: 11.2227208614
thread 19 memcache 5000 set: 11.4754559994
thread 2 tokyo tyrant 5000 set: 7.76640605927
thread 5 tokyo tyrant 5000 set: 8.22156119347
thread 1 tokyo tyrant 5000 set: 8.40494203568
thread 10 tokyo tyrant 5000 set: 7.92209196091
thread 9 tokyo tyrant 5000 set: 7.55454802513
thread 18 tokyo tyrant 5000 set: 7.27255797386
thread 8 tokyo tyrant 5000 set: 7.63895893097
thread 11 tokyo tyrant 5000 set: 7.13767504692
thread 13 tokyo tyrant 5000 set: 7.42961502075
thread 14 tokyo tyrant 5000 set: 7.43208909035
thread 6 tokyo tyrant 5000 set: 7.3564889431
thread 15 tokyo tyrant 5000 set: 6.79607892036
thread 17 tokyo tyrant 5000 set: 6.93887209892
thread 3 tokyo tyrant 5000 set: 7.68552422523
thread 12 tokyo tyrant 5000 set: 7.12319302559
thread 16 tokyo tyrant 5000 set: 7.10764598846
thread 0 tokyo tyrant 5000 set: 6.90239214897
thread 7 tokyo tyrant 5000 set: 7.22372317314
thread 4 tokyo tyrant 5000 set: 7.10077404976
thread 19 tokyo tyrant 5000 set: 6.64217996597
--------------------------------------------------------------------------------
get_benchmark: 20 threads, 100000 times
thread 6 memcache 5000 get: 8.76900911331
thread 18 memcache 5000 get: 8.84009003639
thread 17 memcache 5000 get: 8.86155486107
thread 5 memcache 5000 get: 8.91267108917
thread 13 memcache 5000 get: 8.92148303986
thread 0 memcache 5000 get: 8.98046302795
thread 19 memcache 5000 get: 8.98061203957
thread 16 memcache 5000 get: 8.99304008484
thread 11 memcache 5000 get: 9.07233214378
thread 1 memcache 5000 get: 9.09262895584
thread 14 memcache 5000 get: 9.11016702652
thread 4 memcache 5000 get: 9.11597895622
thread 3 memcache 5000 get: 9.1481218338
thread 12 memcache 5000 get: 9.20062994957
thread 10 memcache 5000 get: 9.2384750843
thread 2 memcache 5000 get: 9.27785277367
thread 8 memcache 5000 get: 9.27573204041
thread 9 memcache 5000 get: 9.32341504097
thread 7 memcache 5000 get: 9.40595293045
thread 15 memcache 5000 get: 9.44804811478
thread 6 tokyo tyrant 5000 get: 6.73215508461
thread 0 tokyo tyrant 5000 get: 6.55519604683
thread 17 tokyo tyrant 5000 get: 6.68555307388
thread 5 tokyo tyrant 5000 get: 6.64170980453
thread 11 tokyo tyrant 5000 get: 6.53203821182
thread 12 tokyo tyrant 5000 get: 6.41466784477
thread 4 tokyo tyrant 5000 get: 6.51224589348
thread 2 tokyo tyrant 5000 get: 6.35708498955
thread 14 tokyo tyrant 5000 get: 6.54695796967
thread 19 tokyo tyrant 5000 get: 6.67935991287
thread 16 tokyo tyrant 5000 get: 6.67978215218
thread 18 tokyo tyrant 5000 get: 6.83982586861
thread 13 tokyo tyrant 5000 get: 6.78410601616
thread 3 tokyo tyrant 5000 get: 6.55975389481
thread 9 tokyo tyrant 5000 get: 6.38529014587
thread 8 tokyo tyrant 5000 get: 6.4428050518
thread 7 tokyo tyrant 5000 get: 6.31911993027
thread 10 tokyo tyrant 5000 get: 6.496737957
thread 15 tokyo tyrant 5000 get: 6.28649902344
thread 1 tokyo tyrant 5000 get: 6.65115785599
--------------------------------------------------------------------------------
get_miss_benchmark: 20 threads, 100000 times
thread 10 memcache 5000 get miss: 7.05730509758
thread 9 memcache 5000 get miss: 7.05415606499
thread 3 memcache 5000 get miss: 7.1769759655
thread 16 memcache 5000 get miss: 7.19843101501
thread 13 memcache 5000 get miss: 7.26215600967
thread 7 memcache 5000 get miss: 7.26590704918
thread 19 memcache 5000 get miss: 7.27665185928
thread 12 memcache 5000 get miss: 7.35561084747
thread 17 memcache 5000 get miss: 7.46251511574
thread 18 memcache 5000 get miss: 7.48763084412
thread 14 memcache 5000 get miss: 7.50556397438
thread 5 memcache 5000 get miss: 7.57047796249
thread 4 memcache 5000 get miss: 7.56979203224
thread 1 memcache 5000 get miss: 7.61345601082
thread 11 memcache 5000 get miss: 7.60594892502
thread 6 memcache 5000 get miss: 7.63839101791
thread 2 memcache 5000 get miss: 7.65122389793
thread 0 memcache 5000 get miss: 7.70662999153
thread 8 memcache 5000 get miss: 7.72461605072
thread 15 memcache 5000 get miss: 7.73385190964
thread 9 tokyo tyrant 5000 get miss: 6.74626517296
thread 3 tokyo tyrant 5000 get miss: 6.66942286491
thread 10 tokyo tyrant 5000 get miss: 6.8257830143
thread 16 tokyo tyrant 5000 get miss: 6.69402313232
thread 13 tokyo tyrant 5000 get miss: 6.63183093071
thread 12 tokyo tyrant 5000 get miss: 6.5411028862
thread 7 tokyo tyrant 5000 get miss: 6.66564011574
thread 19 tokyo tyrant 5000 get miss: 6.70229697227
thread 17 tokyo tyrant 5000 get miss: 6.54302000999
thread 4 tokyo tyrant 5000 get miss: 6.44842004776
thread 1 tokyo tyrant 5000 get miss: 6.44668984413
thread 14 tokyo tyrant 5000 get miss: 6.57335805893
thread 18 tokyo tyrant 5000 get miss: 6.58749604225
thread 5 tokyo tyrant 5000 get miss: 6.51425504684
thread 15 tokyo tyrant 5000 get miss: 6.35351395607
thread 11 tokyo tyrant 5000 get miss: 6.48207402229
thread 8 tokyo tyrant 5000 get miss: 6.37408590317
thread 0 tokyo tyrant 5000 get miss: 6.39541912079
thread 6 tokyo tyrant 5000 get miss: 6.46143507957
thread 2 tokyo tyrant 5000 get miss: 6.46356105804

由上面 输出可以看到,tokyo tyrant并不比memcache性能差太多(可能是memcache.py有性能问题)。

在多线 (进)程环境下,memcache表现并不好,而tokyo tyrant有明显的性能提高。

文章2:

http://blog.163.com/chenxiaodong_2009/blog/static/1290823302010220105436629/

Tokyo Cabinet 是日本人 Mikio Hirabayashi(平林幹雄)のページ 开发的一款DBM数据库(注:大名鼎鼎的DBM数据库qdbm就是他开发的),该数据库读写非常快。对比memcache最大的特性是添加了数据持久的 功能。在数据重要性不是很高的情况下可以用它代替mysql的数据存储.这里提到的TokyoTyrant是前面Tokyo Cabinet的网络接口。
我是超级菜鸟型的,什么命令都得骨骼,跌跌撞裝到这里,这里主要想说明的是安装过程中的一些体会,希望能对和我一样基础的人有些帮助。具体的安装过程网上 很多,但比较严谨的可见 阿福的技术BLOG,还可以关注他的此专题系列文章。
一.安装
首先安装Tokyo Cabinet,安装方法de>
curl -O http://tokyocabinet.sourceforge.net/tokyocabinet-1.4.16.tar.gz
./configure
make
make install
de>
安装tokyotyrant de>
curl -O http://tokyocabinet.sourceforge.net/tyrantpkg/tokyotyrant-1.1.23.tar.gz
./configure
make
make install
完成后可在usr/local/bin里启动 ttservctl start启动默认安装的服务,本机端口1978。
可以马上用ps -ef | grep ttserver 语句看一下启动服务的情况。
二.测试
1. curl测试
服务启动之后,我们就要在前台做一些测试,快捷的可以像福哥那样用curl语句测试:
curl -X PUT http://127.0.0.1:1978 key1 -d "smile2life" 得到created的响应表明创建key-value成功。
de>de>curl -X GET http://127.0.0.1:1978 key1 我们可以得到键值smile2life。
2.持久存储测试
可以建立起动态网页语句进行测试,这里以php为例,前提是装了php的memched扩展。因为tt与memcached基本接口一致,所以可以完全用 访问memcached的接口来访问TC,根据ip和端口连接到对应的ttserver或者memcached,所以不需要我们操心了。这里就测试一点: 重启ttserver后以前设置的键值还可以访问,说明键值已经存到文件了。
<?php
$tt = new memcache();
$tt->connect('127.0.0.1',1978);
$tt->set('test','PHP connet to Tokyo cabinet at port 1978');
echo $tt->get('test');
$tt->close();
?>
这时运行http://127.0.0.1/testTT.php,得到de>“PHP connet to Tokyo cabinet at port 1978“,说明设置key-value成功。
然后我们重新启动ttserver。sudo /usr/local/sbin/ttserver restart
Stopping the server of Tokyo Tyrant
Sending the terminal signal to the process: 25680
Done
Starting the server of Tokyo Tyrant
Executing: ttserver -port 1978 -dmn -pid /var/ttserver/pid
Done
注意看下这里执行语句de>de>Executing: ttserver -port 1978 -dmn -pid /var/ttserver/pid,说明重新启动了刚才我们开的服务,因为端口都是1978。
再次执行testTT.php,这次我们先注释掉de>de>$tt->set('test','PHP connet to Tokyo cabinet at port 1978');结果还是能得到de>de>“PHP connet to Tokyo cabinet at port 1978“,说明key-value保存到了数据库文件。

你可能感兴趣的:(Tokyo Cabinet)