LevelDB – Installation and Performance Tests in Python

py-leveldb installation


# http://code.google.com/p/py-leveldb/

# install subversion
sudo apt-get install -y subversion

# get source code from SVN
mkdir -p ~/build/py-leveldb-read
cd ~/build/py-leveldb-read
svn checkout http://py-leveldb.googlecode.com/svn/trunk/ py-leveldb-read-only
cd py-leveldb-read-only

# build the leveldb library
./compile_leveldb.sh

# build the Python extensions
python setup.py build

# install it
sudo python setup.py install

python -c 'import leveldb; print "works"'

#cd leveldb-read-only
#make
#cd ..
#sudo python setup.py install

Level DB Installation

mkdir -p ~/build/leveldb/
cd ~/build/leveldb/
sudo apt-get install -y git libsnappy-dev
git clone https://code.google.com/p/leveldb/
cd leveldb
#cd ~/build/leveldb/leveldb
make
# You don't neccessarily need snappy as LevelDB will work without it but you would need to recompile if you don't install it before compiling.
# Installation: Excecute the following shell snippet as root from your LevelDB folder:
#!/bin/sh
sudo cp --preserve=links libleveldb.* /usr/local/lib
sudo cp -r include/leveldb /usr/local/include/
sudo ldconfig
########## cpy-leveldb###########
# doesn't work
mkdir -p ~/build/leveldb
cd  ~/build/leveldb
git clone https://github.com/forhappy/cpy-leveldb
cd cpy-leveldb
sudo python setup.py install

Testing


mkdir -p ~/build/leveldb/test
cd ~/build/leveldb/test

tee ./leveldb_test.py <<"_EOF_"
import leveldb
db = leveldb.LevelDB("./testlevel.db")

print "put", db.Put('hello', 'world')
print "get", db.Get('hello')
print "delete", db.Delete('hello')

# multiple put/delete applied atomically, and committed to disk
batch = leveldb.WriteBatch()
batch.Put('hello', 'world')
batch.Put('hello again', 'world')
batch.Delete('hello')
print "batch write", db.Write(batch, sync = True)

_EOF_

python leveldb_test.py
put None
get world
delete None
batch write None

Performance Tests

cat genre.txt

mkdir -p ~/build/leveldb/test
cd ~/build/leveldb/test


tee ./leveldb_perftest.py <<"_EOF_"
import leveldb

def main():
    import sys
    #print "argurment count",len(sys.argv)
    if len(sys.argv) < 4:
        print """enter operation and how many.
          example:
          time python leveldb_perftest.py   
          # write 1 million values to testdb
          time python leveldb_perftest.py test.db write 1000000

          # read 1 million values to testdb
          time python leveldb_perftest.py test.db read 1000000
        """
        return
    filename,ops,max = sys.argv[1],sys.argv[2],int(sys.argv[3])
    db = leveldb.LevelDB(filename)
    if 'write' in ops:
       mystring='a'*80
       for i in range(0,max):
           db.Put('key'+str(i), str(i)+mystring)
    elif 'read' in ops:
       for i in range(0,max):
           db.Get('key'+str(i))
    else:
        print 'unkown operation:%s use "read" or "write"'%ops
    print 'done'

if __name__ == "__main__":
    main()

_EOF_


# 1 million write
time python leveldb_perftest.py testdb write 1000000
#done
#
#real	0m5.343s
#user	0m5.352s
#sys	0m2.010s

# 1 million read
time python leveldb_perftest.py testdb read 1000000
#done
#
#real	0m2.369s
#user	0m2.508s
#sys	0m0.060s

# 100 million write
time python leveldb_perftest.py bigtestdb write 100000000
#done
#
#real	9m33.823s
#user	11m10.719s
#sys	3m51.960s

# 100 million read
time python leveldb_perftest.py bigtestdb read 100000000
#done
#
#real	3m51.423s
#user	3m53.806s
#sys	0m1.902s

References