Commits

Gregory Petukhov committed e63426a

Improve test

Comments (0)

Files changed (2)

 
 syntax:regexp
 ^var/
+^grab$
 import zlib
 from bson import Binary
 import sys
+#from grab import Grab
 
 # Number of read/write operations
-NUMBER = 2000
+NUMBER = 1000
 
 # Size of data of one record in database
 #DATA_SIZE = 1000
 
 CONFIG = [
     {'key': 'mysql', 'enable': 1, 'name': 'MySQL'},
-    {'key': 'mongo', 'enable': 1, 'name': 'MongoDB'},
-    {'key': 'tc', 'enable': 1, 'name': 'Tokyo Cabinet'},
+    {'key': 'mongo', 'enable': 0, 'name': 'MongoDB'},
+    {'key': 'tc', 'enable': 0, 'name': 'Tokyo Cabinet'},
     {'key': 'postgres', 'enable': 0, 'name': 'Postgres'},
 ]
 
+ONLY_READ = 0
+
 
 def random_data(number, only_hash=False):#, data_size=DATA_SIZE):
-    _hash = sha1(str(number)).hexdigest()
+    _raw_hash = sha1(str(number)).digest()
+    _hex_hash = sha1(str(number)).hexdigest()
     if only_hash:
         zdata = None
     else:
         }
         data = marshal.dumps(page)
         zdata = zlib.compress(data)
-    return _hash, zdata
+    return _raw_hash, _hex_hash, zdata
 
 
 def parse_page(data):
     dump = zlib.decompress(data)
-    return marshal.loads(dump)
+    data =  marshal.loads(dump)
+    #g = Grab(data['body'])
+    #g.tree
+    return data
 
 
 def build_hash_list():
     print 'Building hash list'
     hash_list =[]
     for x in xrange(NUMBER):
-        _hash, data = random_data(x, only_hash=True)
-        hash_list.append(_hash)
+        _raw_hash, _hex_hash, data = random_data(x, only_hash=True)
+        hash_list.append(_hex_hash)
     shuffle(hash_list)
     return hash_list
 
     cur.execute('use dbtest')
             #id int auto_increment primary key,
             #unique (hash)
+            #crc int unsigned not null,
+            #index (crc),
     cur.execute('''
         create table cache (
-            id varchar(40) primary key,
-            data blob not null
+            id binary(20) not null,
+            data mediumblob not null,
+            primary key (id)
         ) engine = myisam
     ''')
 
     cur = conn.cursor()
     cur.execute('use dbtest')
     for x in xrange(NUMBER):
-        _hash, data = random_data(x)
+        _raw_hash, _hex_hash, data = random_data(x)
         cur.execute('''
-            insert into cache (id, data) values(%s, %s)
-        ''', (_hash, data))
+            insert into cache (id, data) values(x%s, %s)
+        ''', (_hex_hash, data))
         yield 1
 
 
     cur = conn.cursor()
     cur.execute('use dbtest')
     #cur.execute('load index into cache `cache`')
-    for _hash in hash_list:
+    for _hex_hash in hash_list:
         cur.execute('''
             select id, data from cache
-            where id = %s
-        ''', (_hash,))
-        _hash, data = cur.fetchone()
+            where id = x%s
+        ''', (_hex_hash,))
+        _raw_hash, data = cur.fetchone()
         page = parse_page(data)
-        assert len(_hash) == 40
+        assert len(_raw_hash) == 20
         assert len(page['body']) == len(DATA)
         assert page['body'].startswith(DATA[:100])
         yield 1
             setup_func = globals()['%s_setup_database' % config['key']]
             write_func = globals()['%s_write' % config['key']]
             read_func = globals()['%s_read' % config['key']]
-            setup_func()
 
-            print '%s: writing' % config['name']
-            total = timeit(write_func)
-            msg = '%s write: %0.2f sec.' % (config['name'], total)
-            messages.append(msg)
-            print msg
+            if not ONLY_READ:
+                setup_func()
+
+            if not ONLY_READ:
+                print '%s: writing' % config['name']
+                total = timeit(write_func)
+                msg = '%s write: %0.2f sec.' % (config['name'], total)
+                messages.append(msg)
+                print msg
 
             print '%s: reading' % config['name']
             total = timeit(read_func, hash_list)