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

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

 

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

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

与memcache对比测试

benchmark.py:

!/usr/bin/env python

#  -*- coding: utf-8 -*-
""" benchmark test for pytyrant
"""
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: %d threads, %d times '   %  (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
        
#  one thread set
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 %d set: %s '   %  (count, time.time()  -  start))
    
    start 
=  time.time()
    
for  i  in  xrange(count):
        tt[
' key_%d '   %  i]  =   ' value_%d '   %  i
    
if  display:
        display(
' tokyo tyrant %d set: %s '   %  (count, time.time()  -  start))
    
def  set_benchmark_threads(count = 100000 , threads = 20 ):
    _benchmark_threads(set_benchmark, count, threads)

#  one thread get
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 %d get: %s '   %  (count, time.time()  -  start))
    
    start 
=  time.time()
    
for  i  in  xrange(count):
        
assert  tt[ ' key_%d '   %  i]  ==   ' value_%d '   %  i
    display(
' tokyo tyrant %d get: %s '   %  (count, time.time()  -  start))
    
def  get_benchmark_threads(count = 100000 , threads = 20 ):
    _benchmark_threads(get_benchmark, count, threads)

#  one thread get all miss
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 %d get miss: %s '   %  (count, time.time()  -  start))
    
    start 
=  time.time()
    
for  i  in  xrange(count):
        
assert  tt.get( ' key_%d_miss '   %  i, none)  is  none
    display(
' tokyo tyrant %d get miss: %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   ' one threads '
    
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有明显的性能提高。

你可能感兴趣的:(Ubuntu,thread,TokyoCabinet,Python,.net)