tests/pythonÂ_tests make Python files Pythonic
This commit is contained in:
parent
1771869167
commit
50ec536c1b
|
@ -49,6 +49,7 @@ class Result(object):
|
|||
class PythonTest(object):
|
||||
def __init__(self):
|
||||
self.result = Result()
|
||||
self.args = None
|
||||
|
||||
def run_test(self):
|
||||
pass
|
||||
|
@ -94,7 +95,7 @@ class PythonTest(object):
|
|||
|
||||
try:
|
||||
self.run_test()
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(traceback.format_exc())
|
||||
|
||||
self.result.save(self.args.output_directory)
|
||||
|
|
|
@ -30,7 +30,6 @@ sys.path.append(args.build_dir + '/bindings/python')
|
|||
import fdb
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
fdb.api_version(630)
|
||||
|
||||
if not os.path.exists(args.client_log_dir):
|
||||
|
@ -55,6 +54,6 @@ for i in range(100):
|
|||
key = b"test_%d" % random.randrange(0, 100000000)
|
||||
val = b"value_%d" % random.randrange(0, 10000000)
|
||||
db = dbs[i % len(dbs)]
|
||||
print ("Writing: ", key, val, db)
|
||||
print("Writing: ", key, val, db)
|
||||
db[key] = val
|
||||
assert (val == db[key])
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
import os
|
||||
import sys
|
||||
import time
|
||||
import string
|
||||
import random
|
||||
import traceback
|
||||
|
||||
|
@ -36,7 +35,7 @@ fdb.api_version(400)
|
|||
|
||||
|
||||
# A class that mimics some of the operations of the FoundationDB key-value store
|
||||
class KeyValueStore():
|
||||
class KeyValueStore:
|
||||
|
||||
# Uses a simple dictionary to store key-value pairs
|
||||
# Any operations that depend on the order of keys first sort the data
|
||||
|
@ -44,42 +43,42 @@ class KeyValueStore():
|
|||
|
||||
def get(self, key):
|
||||
if key is fdb.KeySelector:
|
||||
key = get_key(key)
|
||||
key = self.get_key(key)
|
||||
|
||||
if key in self.store:
|
||||
return self.store[key]
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_key(self, keySelector):
|
||||
sortedKeys = list(sorted(self.store.keys()))
|
||||
for index, key in enumerate(sortedKeys):
|
||||
if key >= keySelector.key:
|
||||
index += keySelector.offset
|
||||
if (key == keySelector.key and not keySelector.or_equal) or key != keySelector.key:
|
||||
def get_key(self, key_selector):
|
||||
sorted_keys = list(sorted(self.store.keys()))
|
||||
for index, key in enumerate(sorted_keys):
|
||||
if key >= key_selector.key:
|
||||
index += key_selector.offset
|
||||
if (key == key_selector.key and not key_selector.or_equal) or key != key_selector.key:
|
||||
index -= 1
|
||||
|
||||
if index < 0 or index >= len(sortedKeys):
|
||||
if index < 0 or index >= len(sorted_keys):
|
||||
return ''
|
||||
else:
|
||||
return sortedKeys[index]
|
||||
return sorted_keys[index]
|
||||
|
||||
index = len(sortedKeys) + keySelector.offset - 1
|
||||
if index < 0 or index >= len(sortedKeys):
|
||||
index = len(sorted_keys) + key_selector.offset - 1
|
||||
if index < 0 or index >= len(sorted_keys):
|
||||
return ''
|
||||
else:
|
||||
return sortedKeys[index]
|
||||
return sorted_keys[index]
|
||||
|
||||
def get_range(self, begin, end, limit=None):
|
||||
values = []
|
||||
count = 0
|
||||
|
||||
if begin is fdb.KeySelector:
|
||||
begin = get_key(begin)
|
||||
begin = self.get_key(begin)
|
||||
if end is fdb.KeySelector:
|
||||
end = get_key(end)
|
||||
end = self.get_key(end)
|
||||
|
||||
for key, value in sorted(self.store.iteritems()):
|
||||
for key, value in sorted(self.store.items()):
|
||||
if limit is not None and count >= limit:
|
||||
break
|
||||
|
||||
|
@ -95,7 +94,7 @@ class KeyValueStore():
|
|||
def get_range_startswith(self, prefix, limit=None):
|
||||
values = []
|
||||
count = 0
|
||||
for key, value in sorted(self.store.iteritems()):
|
||||
for key, value in sorted(self.store.items()):
|
||||
if limit is not None and count >= limit:
|
||||
break
|
||||
|
||||
|
@ -113,7 +112,7 @@ class KeyValueStore():
|
|||
|
||||
def clear(self, key):
|
||||
if key is fdb.KeySelector:
|
||||
key = get_key(key)
|
||||
key = self.get_key(key)
|
||||
|
||||
if key in self.store:
|
||||
del self.store[key]
|
||||
|
@ -137,55 +136,55 @@ class PythonCorrectness(PythonTest):
|
|||
db = fdb.open(None, 'DB')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('fdb.open failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('fdb.open failed'))
|
||||
return
|
||||
|
||||
try:
|
||||
print('Testing functions...')
|
||||
self.testFunctions(db)
|
||||
self.test_functions(db)
|
||||
|
||||
print('Testing correctness...')
|
||||
del db[:]
|
||||
self.testCorrectness(db)
|
||||
self.test_correctness(db)
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Failed to complete all tests'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Failed to complete all tests'))
|
||||
|
||||
# Generates a random set of keys and values
|
||||
def generateData(self, numKeys, minKeyLength, maxKeyLength, minValueLength, maxValueLength, prefix='', allowDuplicates=True):
|
||||
def generate_data(self, num_keys, min_key_length, max_key_length, min_value_length, max_value_length, prefix='', allow_duplicates=True):
|
||||
data = list()
|
||||
keys = set()
|
||||
while len(data) < numKeys:
|
||||
while len(data) < num_keys:
|
||||
# key = prefix + ''.join(random.choice(string.ascii_lowercase)
|
||||
# for i in range(0, random.randint(minKeyLength - len(prefix), maxKeyLength - len(prefix))))
|
||||
key = prefix + ''.join(chr(random.randint(0, 254))
|
||||
for i in range(0, random.randint(minKeyLength - len(prefix), maxKeyLength - len(prefix))))
|
||||
if not allowDuplicates:
|
||||
for _ in range(0, random.randint(min_key_length - len(prefix), max_key_length - len(prefix))))
|
||||
if not allow_duplicates:
|
||||
if key in keys:
|
||||
continue
|
||||
else:
|
||||
keys.add(key)
|
||||
|
||||
value = ''.join('x' for i in range(0, random.randint(maxKeyLength, maxValueLength)))
|
||||
value = ''.join('x' for _ in range(0, random.randint(max_key_length, max_value_length)))
|
||||
data.append([key, value])
|
||||
|
||||
return data
|
||||
|
||||
# Function to test the callback feature of Future objects
|
||||
def testCallback(self, future):
|
||||
def test_callback(self, future):
|
||||
try:
|
||||
future.wait()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.callbackError = getError('Callback future get failed')
|
||||
except Exception:
|
||||
self.callbackError = self.get_error('Callback future get failed')
|
||||
|
||||
self.callback = True
|
||||
|
||||
# Tests that all of the functions in the python API can be called without failing
|
||||
def testFunctions(self, db):
|
||||
def test_functions(self, db):
|
||||
self.callback = False
|
||||
self.callbackError = ''
|
||||
|
||||
|
@ -193,8 +192,8 @@ class PythonCorrectness(PythonTest):
|
|||
tr = db.create_transaction()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('db.create_transaction failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('db.create_transaction failed'))
|
||||
return
|
||||
|
||||
try:
|
||||
|
@ -206,23 +205,23 @@ class PythonCorrectness(PythonTest):
|
|||
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Set/Get value failed (block until ready)'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Set/Get value failed (block until ready)'))
|
||||
|
||||
try:
|
||||
value = tr['testkey']
|
||||
value.wait()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get value failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get value failed'))
|
||||
|
||||
try:
|
||||
tr['testkey'] = 'newtestvalue'
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Replace value failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Replace value failed'))
|
||||
|
||||
try:
|
||||
value = tr['fakekey']
|
||||
|
@ -231,30 +230,30 @@ class PythonCorrectness(PythonTest):
|
|||
value.wait()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get non-existent key failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get non-existent key failed'))
|
||||
|
||||
try:
|
||||
tr.commit().wait()
|
||||
tr.get_committed_version()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Commit failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Commit failed'))
|
||||
|
||||
try:
|
||||
tr.reset()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Reset failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Reset failed'))
|
||||
|
||||
try:
|
||||
version = tr.get_read_version()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('tr.get_read_version failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('tr.get_read_version failed'))
|
||||
|
||||
try:
|
||||
value = tr['testkey']
|
||||
|
@ -262,38 +261,38 @@ class PythonCorrectness(PythonTest):
|
|||
tr.reset()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get and reset failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get and reset failed'))
|
||||
|
||||
try:
|
||||
tr.set_read_version(version.wait())
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Set read version failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Set read version failed'))
|
||||
|
||||
try:
|
||||
value = tr['testkey']
|
||||
callbackTime = time.time()
|
||||
value.on_ready(self.testCallback)
|
||||
callback_time = time.time()
|
||||
value.on_ready(self.test_callback)
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get future and set callback failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get future and set callback failed'))
|
||||
|
||||
try:
|
||||
del tr['testkey']
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Delete key failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Delete key failed'))
|
||||
|
||||
try:
|
||||
del tr['fakekey']
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Delete non-existent key failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Delete non-existent key failed'))
|
||||
|
||||
try:
|
||||
tr.set('testkey', 'testvalue')
|
||||
|
@ -301,15 +300,15 @@ class PythonCorrectness(PythonTest):
|
|||
value.wait()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Future.get failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Future.get failed'))
|
||||
|
||||
try:
|
||||
tr.clear('testkey')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Clear key failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Clear key failed'))
|
||||
|
||||
try:
|
||||
tr['testkey1'] = 'testvalue1'
|
||||
|
@ -326,8 +325,8 @@ class PythonCorrectness(PythonTest):
|
|||
v += ''
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get range failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get range failed'))
|
||||
|
||||
try:
|
||||
tr['otherkey1'] = 'othervalue1'
|
||||
|
@ -337,22 +336,22 @@ class PythonCorrectness(PythonTest):
|
|||
v += ''
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get range starts with failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get range starts with failed'))
|
||||
|
||||
try:
|
||||
tr.clear_range_startswith('otherkey')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Clear range starts with failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Clear range starts with failed'))
|
||||
|
||||
try:
|
||||
tr.clear_range('testkey1', 'testkey3')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Clear range failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Clear range failed'))
|
||||
|
||||
try:
|
||||
tr['testkey1'] = 'testvalue1'
|
||||
|
@ -363,8 +362,8 @@ class PythonCorrectness(PythonTest):
|
|||
end = fdb.KeySelector('testkey2', 0, 1)
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Create key selector failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Create key selector failed'))
|
||||
|
||||
try:
|
||||
for k, v in tr.get_range(begin, end):
|
||||
|
@ -377,8 +376,8 @@ class PythonCorrectness(PythonTest):
|
|||
v += ''
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Get range (key selectors) failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Get range (key selectors) failed'))
|
||||
|
||||
try:
|
||||
tr.clear_range(begin, end)
|
||||
|
@ -394,8 +393,8 @@ class PythonCorrectness(PythonTest):
|
|||
tr['testkey3'] = 'testvalue3'
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Clear range (key selectors) failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Clear range (key selectors) failed'))
|
||||
|
||||
try:
|
||||
begin = fdb.KeySelector.last_less_than('testkey2')
|
||||
|
@ -411,33 +410,33 @@ class PythonCorrectness(PythonTest):
|
|||
v += ''
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Builtin key selectors failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Builtin key selectors failed'))
|
||||
|
||||
try:
|
||||
del tr['testkey1':'testkey3']
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Delete key range failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Delete key range failed'))
|
||||
|
||||
try:
|
||||
tr.commit().wait()
|
||||
tr.get_committed_version()
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Commit failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Commit failed'))
|
||||
|
||||
try:
|
||||
key = fdb.tuple.pack(('k1', 'k2', 'k3'))
|
||||
kTuple = fdb.tuple.unpack(key)
|
||||
if kTuple[0] != 'k1' and kTuple[1] != 'k2' and kTuple[2] != 'k3':
|
||||
k_tuple = fdb.tuple.unpack(key)
|
||||
if k_tuple[0] != 'k1' and k_tuple[1] != 'k2' and k_tuple[2] != 'k3':
|
||||
self.result.add_error('Tuple <-> key conversion yielded incorrect results')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Tuple <-> key conversion failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Tuple <-> key conversion failed'))
|
||||
|
||||
try:
|
||||
tr[fdb.tuple.pack(('k1', 'k2'))] = 'v'
|
||||
|
@ -449,8 +448,8 @@ class PythonCorrectness(PythonTest):
|
|||
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Tuple get range failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Tuple get range failed'))
|
||||
|
||||
try:
|
||||
tr['testint'] = '10'
|
||||
|
@ -459,97 +458,97 @@ class PythonCorrectness(PythonTest):
|
|||
self.result.add_error('Value retrieval yielded incorrect results')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(getError('Future value retrieval failed'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Future value retrieval failed'))
|
||||
|
||||
if not self.callback:
|
||||
time.sleep(5)
|
||||
if not self.callback:
|
||||
self.result.add_error('Warning: Future callback not called after %f seconds' % (time.time() - callbackTime))
|
||||
self.result.add_error('Warning: Future callback not called after %f seconds' % (time.time() - callback_time))
|
||||
if len(self.callbackError) > 0:
|
||||
self.result.add_error(self.callbackError)
|
||||
|
||||
# Compares a FoundationDB database with an in-memory key-value store
|
||||
def compareDatabaseToMemory(self, db, store):
|
||||
dbResult = self.correctnessGetRangeTransactional(db, '\x00', '\xff')
|
||||
storeResult = store.get_range('\x00', '\xff')
|
||||
def compare_database_to_memory(self, db, store):
|
||||
db_result = self.correctness_get_range_transactional(db, '\x00', '\xff')
|
||||
store_result = store.get_range('\x00', '\xff')
|
||||
|
||||
return self.compareResults(dbResult, storeResult)
|
||||
return self.compare_results(db_result, store_result)
|
||||
|
||||
# Compares result sets coming from a FoundationDB database and an in-memory key-value store
|
||||
def compareResults(self, dbResults, storeResults):
|
||||
if len(dbResults) != len(storeResults):
|
||||
def compare_results(self, db_results, store_results):
|
||||
if len(db_results) != len(store_results):
|
||||
# print 'mismatched lengths: ' + str(len(dbResults)) + ' - ' + str(len(storeResults))
|
||||
return False
|
||||
|
||||
for i in range(0, len(dbResults)):
|
||||
for i in range(0, len(db_results)):
|
||||
# if i >= len(storeResults):
|
||||
# print 'mismatched key: ' + dbResults[i].key
|
||||
# return False
|
||||
if dbResults[i].key != storeResults[i][0] or dbResults[i].value != storeResults[i][1]:
|
||||
if db_results[i].key != store_results[i][0] or db_results[i].value != store_results[i][1]:
|
||||
# print 'mismatched key: ' + dbResults[i].key + ' - ' + storeResults[i][0]
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# Performs the same operations on a FoundationDB database and an in-memory key-value store and compares the results
|
||||
def testCorrectness(self, db):
|
||||
numKeys = 5000
|
||||
ratioShortKeys = 0.5
|
||||
minShortKeyLength = 1
|
||||
maxShortKeyLength = 3
|
||||
minLongKeyLength = 1
|
||||
maxLongKeyLength = 128
|
||||
minValueLength = 1
|
||||
maxValueLength = 10000
|
||||
maxTransactionBytes = 5000000
|
||||
def test_correctness(self, db):
|
||||
num_keys = 5000
|
||||
ratio_short_keys = 0.5
|
||||
min_short_key_length = 1
|
||||
max_short_key_length = 3
|
||||
min_long_key_length = 1
|
||||
max_long_key_length = 128
|
||||
min_value_length = 1
|
||||
max_value_length = 10000
|
||||
max_transaction_bytes = 5000000
|
||||
|
||||
numReads = 100
|
||||
numRangeReads = 100
|
||||
numPrefixReads = 100
|
||||
numGetKeys = 100
|
||||
num_reads = 100
|
||||
num_range_reads = 100
|
||||
num_prefix_reads = 100
|
||||
num_get_keys = 100
|
||||
|
||||
numClears = 100
|
||||
numRangeClears = 10
|
||||
numPrefixClears = 10
|
||||
# num_clears = 100
|
||||
num_range_clears = 10
|
||||
num_prefix_clears = 10
|
||||
|
||||
maxKeysPerTransaction = max(1, maxTransactionBytes / (maxValueLength + maxLongKeyLength))
|
||||
max_keys_per_transaction = max(1, int(max_transaction_bytes / (max_value_length + max_long_key_length)))
|
||||
|
||||
try:
|
||||
store = KeyValueStore()
|
||||
|
||||
# Generate some random data
|
||||
data = self.generateData(numKeys * ratioShortKeys, minShortKeyLength, maxShortKeyLength, minValueLength, maxValueLength)
|
||||
data.extend(self.generateData(numKeys * (1 - ratioShortKeys), minLongKeyLength, maxLongKeyLength, minValueLength, maxValueLength))
|
||||
data = self.generate_data(num_keys * ratio_short_keys, min_short_key_length, max_short_key_length, min_value_length, max_value_length)
|
||||
data.extend(self.generate_data(num_keys * (1 - ratio_short_keys), min_long_key_length, max_long_key_length, min_value_length, max_value_length))
|
||||
|
||||
# Insert the data
|
||||
self.correctnessSet(db, store, data, maxKeysPerTransaction)
|
||||
if not self.compareDatabaseToMemory(db, store):
|
||||
self.correctness_set(db, store, data, max_keys_per_transaction)
|
||||
if not self.compare_database_to_memory(db, store):
|
||||
self.result.add_error('transaction.set resulted in incorrect database')
|
||||
|
||||
# Compare the results of single key reads
|
||||
if not self.correctnessGet(db, store, data, numReads, maxKeysPerTransaction):
|
||||
if not self.correctness_get(db, store, data, num_reads, max_keys_per_transaction):
|
||||
self.result.add_error('transaction.get returned incorrect result')
|
||||
|
||||
# Compare the results of range reads
|
||||
for i in range(0, numRangeReads):
|
||||
if not self.correctnessGetRange(db, store, data):
|
||||
for i in range(0, num_range_reads):
|
||||
if not self.correctness_get_range(db, store, data):
|
||||
self.result.add_error('transaction.get_range returned incorrect results')
|
||||
break
|
||||
|
||||
# Compare the results of prefix reads
|
||||
for i in range(0, numPrefixReads):
|
||||
if not self.correctnessGetPrefix(db, store, data):
|
||||
for i in range(0, num_prefix_reads):
|
||||
if not self.correctness_get_prefix(db, store, data):
|
||||
self.result.add_error('transaction.get_range_startswith returned incorrect results')
|
||||
break
|
||||
|
||||
# Compare the results of get key
|
||||
if not self.correctnessGetKey(db, store, data, numGetKeys, maxKeysPerTransaction):
|
||||
if not self.correctness_get_key(db, store, data, num_get_keys, max_keys_per_transaction):
|
||||
self.result.add_error('transaction.get_key returned incorrect results')
|
||||
|
||||
# Compare the results of clear
|
||||
clearedKeys = self.correctnessClear(db, store, data, numClears, maxKeysPerTransaction)
|
||||
if not self.compareDatabaseToMemory(db, store):
|
||||
# clearedKeys = self.correctnessClear(db, store, data, num_clears, max_keys_per_transaction)
|
||||
if not self.compare_database_to_memory(db, store):
|
||||
self.result.add_error('transaction.clear resulted in incorrect database')
|
||||
|
||||
# for key in clearedKeys:
|
||||
|
@ -558,21 +557,21 @@ class PythonCorrectness(PythonTest):
|
|||
# print 'successful compare'
|
||||
|
||||
# Fill the database back up with data
|
||||
self.correctnessSet(db, store, data, maxKeysPerTransaction)
|
||||
if not self.compareDatabaseToMemory(db, store):
|
||||
self.correctness_set(db, store, data, max_keys_per_transaction)
|
||||
if not self.compare_database_to_memory(db, store):
|
||||
self.result.add_error('transaction.set resulted in incorrect database')
|
||||
|
||||
# Compare the results of clear_range
|
||||
for i in range(0, numRangeClears):
|
||||
self.correctnessClearRange(db, store, data)
|
||||
for i in range(0, num_range_clears):
|
||||
self.correctness_clear_range(db, store, data)
|
||||
|
||||
success = self.compareDatabaseToMemory(db, store)
|
||||
success = self.compare_database_to_memory(db, store)
|
||||
if not success:
|
||||
self.result.add_error('transaction.clear_range resulted in incorrect database')
|
||||
|
||||
# Fill the database back up with data
|
||||
self.correctnessSet(db, store, data, maxKeysPerTransaction)
|
||||
if not self.compareDatabaseToMemory(db, store):
|
||||
self.correctness_set(db, store, data, max_keys_per_transaction)
|
||||
if not self.compare_database_to_memory(db, store):
|
||||
self.result.add_error('transaction.set resulted in incorrect database')
|
||||
break
|
||||
|
||||
|
@ -580,54 +579,54 @@ class PythonCorrectness(PythonTest):
|
|||
break
|
||||
|
||||
# Compare the results of clear_range_startswith
|
||||
self.correctnessClearPrefix(db, store, data, numPrefixClears)
|
||||
if not self.compareDatabaseToMemory(db, store):
|
||||
self.correctness_clear_prefix(db, store, data, num_prefix_clears)
|
||||
if not self.compare_database_to_memory(db, store):
|
||||
self.result.add_error('transaction.clear_range_startswith resulted in incorrect database')
|
||||
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
self.result.add_error(self.getError('Database error in correctness test'))
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Database error in correctness test'))
|
||||
|
||||
# Stores data in the database and a memory key-value store
|
||||
def correctnessSet(self, db, store, data, maxKeysPerTransaction):
|
||||
def correctness_set(self, db, store, data, max_keys_per_transaction):
|
||||
for [key, value] in data:
|
||||
store.set(key, value)
|
||||
|
||||
keysCommitted = 0
|
||||
while keysCommitted < len(data):
|
||||
self.correctnessSetTransactional(db, data[keysCommitted: keysCommitted + maxKeysPerTransaction])
|
||||
keysCommitted += maxKeysPerTransaction
|
||||
keys_committed = 0
|
||||
while keys_committed < len(data):
|
||||
self.correctness_set_transactional(db, data[keys_committed: keys_committed + max_keys_per_transaction])
|
||||
keys_committed += max_keys_per_transaction
|
||||
|
||||
# Stores data in the database
|
||||
@fdb.transactional
|
||||
def correctnessSetTransactional(self, tr, data):
|
||||
def correctness_set_transactional(self, tr, data):
|
||||
for [key, value] in data:
|
||||
tr.set(key, value)
|
||||
|
||||
# Compares the results of the get operation from the database and a memory key-value store
|
||||
def correctnessGet(self, db, store, data, numReads, maxKeysPerTransaction):
|
||||
def correctness_get(self, db, store, data, num_reads, max_keys_per_transaction):
|
||||
keys = []
|
||||
for i in range(0, numReads):
|
||||
for i in range(0, num_reads):
|
||||
index = random.randint(0, len(data) - 1)
|
||||
keys.append(data[index][0])
|
||||
|
||||
keysRetrieved = 0
|
||||
while keysRetrieved < len(keys):
|
||||
subKeys = keys[keysRetrieved: keysRetrieved + maxKeysPerTransaction]
|
||||
keys_retrieved = 0
|
||||
while keys_retrieved < len(keys):
|
||||
sub_keys = keys[keys_retrieved: keys_retrieved + max_keys_per_transaction]
|
||||
|
||||
values = self.correctnessGetTransactional(db, subKeys)
|
||||
for i in range(0, numReads):
|
||||
if values[i] != store.get(subKeys[i]):
|
||||
print('mismatch: %s', subKeys[i])
|
||||
values = self.correctness_get_transactional(db, sub_keys)
|
||||
for i in range(0, num_reads):
|
||||
if values[i] != store.get(sub_keys[i]):
|
||||
print('mismatch: %s', sub_keys[i])
|
||||
return False
|
||||
keysRetrieved += maxKeysPerTransaction
|
||||
keys_retrieved += max_keys_per_transaction
|
||||
|
||||
return True
|
||||
|
||||
# Gets the values for the specified list of keys from the database
|
||||
@fdb.transactional
|
||||
def correctnessGetTransactional(self, tr, keys):
|
||||
def correctness_get_transactional(self, tr, keys):
|
||||
futures = []
|
||||
for key in keys:
|
||||
futures.append(tr.get(key))
|
||||
|
@ -639,43 +638,43 @@ class PythonCorrectness(PythonTest):
|
|||
return values
|
||||
|
||||
# Compares the results of the get_range operation from the database and a memory key-value store
|
||||
def correctnessGetRange(self, db, store, data):
|
||||
def correctness_get_range(self, db, store, data):
|
||||
index = random.randint(0, len(data) - 1)
|
||||
index2 = random.randint(0, len(data) - 1)
|
||||
|
||||
key1 = min(data[index][0], data[index2][0])
|
||||
key2 = max(data[index][0], data[index2][0])
|
||||
|
||||
dbResults = self.correctnessGetRangeTransactional(db, key1, key2, data)
|
||||
storeResults = store.get_range(key1, key2)
|
||||
db_results = self.correctness_get_range_transactional(db, key1, key2, data)
|
||||
store_results = store.get_range(key1, key2)
|
||||
|
||||
return self.compareResults(dbResults, storeResults)
|
||||
return self.compare_results(db_results, store_results)
|
||||
|
||||
# Gets the entries in the range [key1,key2) from the database
|
||||
@fdb.transactional
|
||||
def correctnessGetRangeTransactional(self, tr, key1, key2, data=None):
|
||||
def correctness_get_range_transactional(self, tr, key1, key2, data=None):
|
||||
if data is not None:
|
||||
return list(tr.get_range(key1, key2, len(data)))
|
||||
else:
|
||||
return list(tr.get_range(key1, key2))
|
||||
|
||||
# Compares the results of the get_range_startswith operation from the database and a memory key-value store
|
||||
def correctnessGetPrefix(self, db, store, data):
|
||||
prefix = ''.join(chr(random.randint(0, 254)) for i in range(0, random.randint(1, 3)))
|
||||
dbResults = self.correctnessGetPrefixTransactional(db, prefix)
|
||||
storeResults = store.get_range_startswith(prefix)
|
||||
def correctness_get_prefix(self, db, store, data):
|
||||
prefix = ''.join(chr(random.randint(0, 254)) for _ in range(0, random.randint(1, 3)))
|
||||
db_results = self.correctness_get_prefix_transactional(db, prefix)
|
||||
store_results = store.get_range_startswith(prefix)
|
||||
|
||||
return self.compareResults(dbResults, storeResults)
|
||||
return self.compare_results(db_results, store_results)
|
||||
|
||||
# Gets the entries with a given prefix from the database
|
||||
@fdb.transactional
|
||||
def correctnessGetPrefixTransactional(self, tr, prefix):
|
||||
def correctness_get_prefix_transactional(self, tr, prefix):
|
||||
return list(tr.get_range_startswith(prefix))
|
||||
|
||||
# Compares the results of the get_key operation from the database and a memory key-value store
|
||||
def correctnessGetKey(self, db, store, data, numGetKeys, maxKeysPerTransaction):
|
||||
def correctness_get_key(self, db, store, data, num_get_keys, max_keys_per_transaction):
|
||||
selectors = []
|
||||
for i in range(0, numGetKeys):
|
||||
for i in range(0, num_get_keys):
|
||||
index = random.randint(0, len(data) - 1)
|
||||
or_equal = random.randint(0, 1)
|
||||
offset = random.randint(max(-index + (1 - or_equal), -10), min(len(data) - index - 1 + or_equal, 10))
|
||||
|
@ -684,23 +683,23 @@ class PythonCorrectness(PythonTest):
|
|||
selector = fdb.KeySelector(key, or_equal, offset)
|
||||
selectors.append(selector)
|
||||
|
||||
keysRetrieved = 0
|
||||
while keysRetrieved < len(selectors):
|
||||
subSelectors = selectors[keysRetrieved: keysRetrieved + maxKeysPerTransaction]
|
||||
dbKeys = self.correctnessGetKeyTransactional(db, subSelectors)
|
||||
for i in range(0, numGetKeys):
|
||||
if dbKeys[i] != store.get_key(subSelectors[i]):
|
||||
keys_retrieved = 0
|
||||
while keys_retrieved < len(selectors):
|
||||
sub_selectors = selectors[keys_retrieved: keys_retrieved + max_keys_per_transaction]
|
||||
db_keys = self.correctness_get_key_transactional(db, sub_selectors)
|
||||
for i in range(0, num_get_keys):
|
||||
if db_keys[i] != store.get_key(sub_selectors[i]):
|
||||
return False
|
||||
keysRetrieved += maxKeysPerTransaction
|
||||
keys_retrieved += max_keys_per_transaction
|
||||
|
||||
return True
|
||||
|
||||
# Gets the keys specified by the list of key selectors
|
||||
@fdb.transactional
|
||||
def correctnessGetKeyTransactional(self, tr, keySelectors):
|
||||
def correctness_get_key_transactional(self, tr, key_selectors):
|
||||
futures = []
|
||||
count = 0
|
||||
for selector in keySelectors:
|
||||
for selector in key_selectors:
|
||||
futures.append(tr.get_key(selector))
|
||||
count += 1
|
||||
|
||||
|
@ -713,63 +712,63 @@ class PythonCorrectness(PythonTest):
|
|||
return keys
|
||||
|
||||
# Clears data from a database and a memory key-value store
|
||||
def correctnessClear(self, db, store, data, numClears, maxKeysPerTransaction):
|
||||
clearedKeys = []
|
||||
for i in range(0, numClears):
|
||||
def correctness_clear(self, db, store, data, num_clears, max_keys_per_transaction):
|
||||
cleared_keys = []
|
||||
for i in range(0, num_clears):
|
||||
index = random.randint(0, len(data) - 1)
|
||||
clearedKeys.append(data[index][0])
|
||||
cleared_keys.append(data[index][0])
|
||||
store.clear(data[index][0])
|
||||
|
||||
keysCleared = 0
|
||||
while keysCleared < len(clearedKeys):
|
||||
self.correctnessClearTransactional(db, clearedKeys[keysCleared: keysCleared + maxKeysPerTransaction])
|
||||
keysCleared += maxKeysPerTransaction
|
||||
keys_cleared = 0
|
||||
while keys_cleared < len(cleared_keys):
|
||||
self.correctness_clear_transactional(db, cleared_keys[keys_cleared: keys_cleared + max_keys_per_transaction])
|
||||
keys_cleared += max_keys_per_transaction
|
||||
|
||||
return clearedKeys
|
||||
return cleared_keys
|
||||
|
||||
# Clears a list of keys from the database
|
||||
@fdb.transactional
|
||||
def correctnessClearTransactional(self, tr, clearedKeys):
|
||||
for key in clearedKeys:
|
||||
def correctness_clear_transactional(self, tr, cleared_keys):
|
||||
for key in cleared_keys:
|
||||
tr.clear(key)
|
||||
|
||||
# Clears a range of data from a database and a memory key-value store
|
||||
def correctnessClearRange(self, db, store, data):
|
||||
def correctness_clear_range(self, db, store, data):
|
||||
index = random.randint(0, len(data) - 1)
|
||||
index2 = random.randint(0, len(data) - 1)
|
||||
|
||||
key1 = min(data[index][0], data[index2][0])
|
||||
key2 = max(data[index][0], data[index2][0])
|
||||
|
||||
self.correctnessClearRangeTransactional(db, key1, key2)
|
||||
self.correctness_clear_range_transactional(db, key1, key2)
|
||||
store.clear_range(key1, key2)
|
||||
|
||||
# Clears a range of memory from a database
|
||||
@fdb.transactional
|
||||
def correctnessClearRangeTransactional(self, tr, key1, key2):
|
||||
def correctness_clear_range_transactional(self, tr, key1, key2):
|
||||
tr.clear_range(key1, key2)
|
||||
|
||||
# Clears data with random prefixes from a database and a memory key-value store
|
||||
def correctnessClearPrefix(self, db, store, data, numPrefixClears):
|
||||
def correctness_clear_prefix(self, db, store, data, num_prefix_clears):
|
||||
prefixes = []
|
||||
for i in range(0, numPrefixClears):
|
||||
prefix = ''.join(chr(random.randint(0, 254)) for i in range(0, random.randint(1, 3)))
|
||||
for i in range(0, num_prefix_clears):
|
||||
prefix = ''.join(chr(random.randint(0, 254)) for _ in range(0, random.randint(1, 3)))
|
||||
prefixes.append(prefix)
|
||||
store.clear_range_startswith(prefix)
|
||||
|
||||
self.correctnessClearPrefixTransactional(db, prefixes)
|
||||
self.correctness_clear_prefix_transactional(db, prefixes)
|
||||
|
||||
# Clears keys from a database that have a prefix in the prefixes list
|
||||
@fdb.transactional
|
||||
def correctnessClearPrefixTransactional(self, tr, prefixes):
|
||||
def correctness_clear_prefix_transactional(self, tr, prefixes):
|
||||
for prefix in prefixes:
|
||||
tr.clear_range_startswith(prefix)
|
||||
|
||||
# Adds the stack trace to an error message
|
||||
def getError(self, message):
|
||||
errorMessage = message + "\n" + traceback.format_exc()
|
||||
print('%s', errorMessage)
|
||||
return errorMessage
|
||||
def get_error(self, message):
|
||||
error_message = message + "\n" + traceback.format_exc()
|
||||
print('%s', error_message)
|
||||
return error_message
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -57,7 +57,7 @@ class PythonPerformance(PythonTest):
|
|||
super(PythonPerformance, self).__init__()
|
||||
self.key_count = key_count
|
||||
self.key_size = key_size
|
||||
self.value_str = ''.join(['x' for i in range(value_size)])
|
||||
self.value_str = ''.join(['x' for _ in range(value_size)])
|
||||
|
||||
# Python Performance Tests (checks if functions run and yield correct results, gets performance indicators)
|
||||
def run_test(self):
|
||||
|
@ -65,7 +65,7 @@ class PythonPerformance(PythonTest):
|
|||
db = fdb.open(None, 'DB')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('fdb.open failed'))
|
||||
return
|
||||
|
||||
|
@ -73,7 +73,7 @@ class PythonPerformance(PythonTest):
|
|||
self.test_performance(db)
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Failed to complete all tests'))
|
||||
|
||||
def random_key(self):
|
||||
|
@ -86,12 +86,12 @@ class PythonPerformance(PythonTest):
|
|||
return self.value_str
|
||||
|
||||
def insert_data(self, db):
|
||||
print 'Loading database'
|
||||
print('Loading database')
|
||||
del db[:]
|
||||
num_keys = 100000 / (self.key_size + len(self.value_str))
|
||||
|
||||
trs = [db.create_transaction() for i in range(int(math.ceil(float(self.key_count) / num_keys)))]
|
||||
success = [False for i in range(len(trs))]
|
||||
trs = [db.create_transaction() for _ in range(int(math.ceil(float(self.key_count) / num_keys)))]
|
||||
success = [False for _ in range(len(trs))]
|
||||
|
||||
while not all(success):
|
||||
futures = {}
|
||||
|
@ -142,7 +142,7 @@ class PythonPerformance(PythonTest):
|
|||
results.append(getattr(self, fxn_name)(db))
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Performance test failed: ' + PythonPerformance.tests[test]))
|
||||
break
|
||||
|
||||
|
@ -233,7 +233,7 @@ class PythonPerformance(PythonTest):
|
|||
tr.options.set_retry_limit(5)
|
||||
|
||||
if count > self.key_count / 2:
|
||||
keys = [self.random_key() for i in range(count)]
|
||||
keys = [self.random_key() for _ in range(count)]
|
||||
else:
|
||||
key_set = OrderedDict()
|
||||
while len(key_set) < count:
|
||||
|
@ -295,9 +295,9 @@ class PythonPerformance(PythonTest):
|
|||
|
||||
# Adds the stack trace to an error message
|
||||
def get_error(self, message):
|
||||
errorMessage = message + "\n" + traceback.format_exc()
|
||||
print('%s' % errorMessage)
|
||||
return errorMessage
|
||||
error_message = message + "\n" + traceback.format_exc()
|
||||
print('%s' % error_message)
|
||||
return error_message
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -53,7 +53,7 @@ class RYWBenchmark(PythonTest):
|
|||
db = fdb.open(None, 'DB')
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('fdb.open failed'))
|
||||
return
|
||||
|
||||
|
@ -61,7 +61,7 @@ class RYWBenchmark(PythonTest):
|
|||
self.test_performance(db)
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Failed to complete all tests'))
|
||||
|
||||
def key(self, num):
|
||||
|
@ -69,9 +69,9 @@ class RYWBenchmark(PythonTest):
|
|||
|
||||
# Adds the stack trace to an error message
|
||||
def get_error(self, message):
|
||||
errorMessage = message + "\n" + traceback.format_exc()
|
||||
print(errorMessage)
|
||||
return errorMessage
|
||||
error_message = message + "\n" + traceback.format_exc()
|
||||
print(error_message)
|
||||
return error_message
|
||||
|
||||
def test_performance(self, db):
|
||||
tr = db.create_transaction()
|
||||
|
@ -99,7 +99,7 @@ class RYWBenchmark(PythonTest):
|
|||
results.append(getattr(self, fxn_name)(tr))
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
except:
|
||||
except Exception:
|
||||
self.result.add_error(self.get_error('Performance test failed: ' + RYWBenchmark.tests[test]))
|
||||
break
|
||||
|
||||
|
|
Loading…
Reference in New Issue