def test_all(times=300): seed = random.random() sys.stderr.write("Seed: %s\n" % seed) tests = [] gen.global_prng = random.Random(seed) push(tests, ['remove', [{}]]) for i in (x for x in range(times * 10)): push(tests, ['insert', [gen.random_document(True)]]) push(tests, ['find', [{}]]) for i in range(0, times): push(tests, ['find', [gen.random_query(), {'batch_size': 2}]]) return tests
def preload_database(ns): gen.generator_options.numeric_fieldnames = ns['no_numeric_fieldnames'] gen.generator_options.test_nulls = ns['no_nulls'] instance = random.random() client = pymongo.MongoClient(ns['host'], ns['port']) if (ns['collection'] == ''): collection = client['test']['performance' + str(instance)[2:]] else: collection = client['test'][ns['collection']] collection.remove() i = 0 while i < ns['number']: docs = [] for j in range(0, min(100, ns['number'] - i)): i += 1 if ns['big_documents']: doc = d.copy() else: doc = gen.random_document(False) doc["boo"] = i doc["_id"] = str(i) docs.append(doc) collection.insert(docs, safe=False) print "Inserted " + str(i) # collection.insert(docs) # print [i for i in collection.find()] print "Inserted " + str(ns['number']) + " documents" print "Database: test" print "Collection: " + ('performance' + str(instance)[2:] if ns['collection'] == '' else ns['collection'])
def one_iteration(collection1, collection2, ns, seed): update_tests_enabled = ns['no_updates'] sorting_tests_enabled = gen.generator_options.allow_sorts indexes_enabled = ns['no_indexes'] projections_enabled = ns['no_projections'] verbose = ns['verbose'] num_doc = ns['num_doc'] fname = "unknown" def _run_operation_(op1, op2): okay = True exceptionOne = None exceptionTwo = None func1, args1, kwargs1 = op1 func2, args2, kwargs2 = op2 try: func1(*args1, **kwargs1) except pymongo.errors.OperationFailure as e: if verbose: print "Failed func1 with " + str(e) exceptionOne = e except MongoModelException as e: if verbose: print "Failed func1 with " + str(e) exceptionOne = e try: func2(*args2, **kwargs2) except pymongo.errors.OperationFailure as e: if verbose: print "Failed func2 with " + str(e) exceptionTwo = e except MongoModelException as e: if verbose: print "Failed func2 with " + str(e) exceptionTwo = e if ((exceptionOne is None and exceptionTwo is None) or (exceptionOne is not None and exceptionTwo is not None and exceptionOne.code == exceptionTwo.code)): pass else: print 'Unmatched result: ' print type(exceptionOne), ': ', str(exceptionOne) print type(exceptionTwo), ': ', str(exceptionTwo) okay = False ignored_exception_check(exceptionOne) ignored_exception_check(exceptionTwo) return okay try: okay = True if verbose: util.traceLevel = 'debug' fname = util.save_cmd_line(util.command_line_str(ns, seed)) collection1.drop() collection2.drop() indexes = [] num_of_indexes = 5 indexes_first = gen.global_prng.choice([True, False]) if indexes_enabled: for i in range(0, num_of_indexes): index_obj = gen.random_index_spec() indexes.append(index_obj) # 0.5% likelyhood to allow using unique index in this iteration, assuming a uniform distribution useUnique = (gen.global_prng.randint(1, 200) == 1) # only allow one out of $num_of_indexes to be unique. allowed_ii = gen.global_prng.randint(1, num_of_indexes) if indexes_first: ii = 1 for i in indexes: if ii == allowed_ii: uniqueIndex = useUnique else: uniqueIndex = False okay = _run_operation_((collection1.ensure_index, (i, ), { "unique": uniqueIndex }), (collection2.ensure_index, (i, ), { "unique": uniqueIndex })) if not okay: return (okay, fname, None) ii += 1 docs = [] for i in range(0, num_doc): doc = gen.random_document(True) docs.append(doc) okay = _run_operation_((collection1.insert, (docs, ), {}), (collection2.insert, (docs, ), {})) if not okay: print "Failed when doing inserts" return (okay, fname, None) if not indexes_first: ii = 1 for i in indexes: if ii == allowed_ii: uniqueIndex = useUnique else: uniqueIndex = False okay = _run_operation_((collection1.ensure_index, (i, ), { "unique": uniqueIndex }), (collection2.ensure_index, (i, ), { "unique": uniqueIndex })) if not okay: print "Failed when adding index after insert" return (okay, fname, None) ii += 1 okay = check_query(dict(), collection1, collection2) if not okay: return (okay, fname, None) if update_tests_enabled: okay, skip_current_iteration = test_update(collection1, collection2, verbose) if skip_current_iteration: if verbose: print "Skipping current iteration due to the failure from update." return (True, fname, None) if not okay: return (okay, fname, None) for ii in range(1, 30): query = gen.random_query() if not sorting_tests_enabled: sort = None limit = 0 skip = 0 else: sort = gen.random_query_sort() limit = gen.global_prng.randint(0, 600) skip = gen.global_prng.randint(0, 10) # Always generate a projection, whether or not we use it. This allows us to run the same test in # either case. temp_projection = gen.random_projection() if not projections_enabled: projection = None else: projection = temp_projection okay = check_query(query, collection1, collection2, projection, sort=sort, limit=limit, skip=skip) if not okay: return (okay, fname, None) if not okay: return (okay, fname, None) except IgnoredException as e: print "Ignoring EXCEPTION: ", e.message return True, fname, None except Exception as e: import traceback traceback.print_exc() return (False, fname, e) return (okay, fname, None)
def one_iteration(collection1, collection2, ns, seed): update_tests_enabled = ns['no_updates'] sorting_tests_enabled = gen.generator_options.allow_sorts indexes_enabled = ns['no_indexes'] projections_enabled = ns['no_projections'] verbose = ns['verbose'] num_doc = ns['num_doc'] fname = "unknown" def _run_operation_(op1, op2): okay = True exceptionOne = None exceptionTwo = None func1, args1, kwargs1 = op1 func2, args2, kwargs2 = op2 try: func1(*args1, **kwargs1) except pymongo.errors.OperationFailure as e: exceptionOne = e except MongoModelException as e: exceptionOne = e try: func1(*args2, **kwargs2) except pymongo.errors.OperationFailure as e: exceptionTwo = e except MongoModelException as e: exceptionTwo = e if ((exceptionOne is None and exceptionTwo is None) or (exceptionOne is not None and exceptionTwo is not None and exceptionOne.code == exceptionTwo.code)): pass else: print 'Unmatched result: ' print type(exceptionOne), ': ', str(exceptionOne) print type(exceptionTwo), ': ', str(exceptionTwo) okay = False ignored_exception_check(exceptionOne) ignored_exception_check(exceptionTwo) return okay try: okay = True if verbose: util.traceLevel = 'debug' fname = util.save_cmd_line(util.command_line_str(ns, seed)) if indexes_enabled: transactional_shim.drop_indexes(collection1) transactional_shim.drop_indexes(collection2) transactional_shim.remove(collection1) transactional_shim.remove(collection2) indexes = [] indexes_first = gen.global_prng.choice([True, False]) if indexes_enabled: for i in range(0, 5): index_obj = gen.random_index_spec() indexes.append(index_obj) if indexes_first: for i in indexes: # When we do enable make sure we don't enable for 50% cases. That way unique indexes will cloud everything else. Make it something like 5-10%. # uniqueIndex = gen.global_prng.choice([True, False]) uniqueIndex = False okay = _run_operation_( (transactional_shim.ensure_index, (collection1, i), { "unique": uniqueIndex }), (transactional_shim.ensure_index, (collection2, i), { "unique": uniqueIndex })) if not okay: return (okay, fname, None) docs = [] for i in range(0, num_doc): doc = gen.random_document(True) docs.append(doc) okay = _run_operation_( (transactional_shim.insert, (collection1, docs), {}), (transactional_shim.insert, (collection2, docs), {})) if not okay: print "Failed when doing inserts" return (okay, fname, None) if not indexes_first: for i in indexes: # When we do enable make sure we don't enable for 50% cases. That way unique indexes will cloud everything else. Make it something like 5-10%. # uniqueIndex = gen.global_prng.choice([True, False]) uniqueIndex = False okay = _run_operation_( (transactional_shim.ensure_index, (collection1, i), { "unique": uniqueIndex }), (transactional_shim.ensure_index, (collection2, i), { "unique": uniqueIndex })) if not okay: print "Failed when adding index after insert" return (okay, fname, None) okay = check_query(dict(), collection1, collection2) if not okay: return (okay, fname, None) if update_tests_enabled: if not test_update([collection1, collection2], verbose): okay = False return (okay, fname, None) for ii in range(1, 30): query = gen.random_query() if not sorting_tests_enabled: sort = None limit = 0 skip = 0 else: sort = gen.random_query_sort() limit = gen.global_prng.randint(0, 600) skip = gen.global_prng.randint(0, 10) # Always generate a projection, whether or not we use it. This allows us to run the same test in # either case. temp_projection = gen.random_projection() if not projections_enabled: projection = None else: projection = temp_projection okay = check_query(query, collection1, collection2, projection, sort=sort, limit=limit, skip=skip) if not okay: return (okay, fname, None) if not okay: return (okay, fname, None) except IgnoredException as e: print "Ignoring EXCEPTION: ", e.message return True, fname, None except Exception as e: import traceback traceback.print_exc() return (False, fname, e) return (okay, fname, None)
def one_iteration(collection1, collection2, ns, seed): update_tests_enabled = ns['no_updates'] sorting_tests_enabled = gen.generator_options.allow_sorts indexes_enabled = ns['no_indexes'] projections_enabled = ns['no_projections'] verbose = ns['verbose'] num_doc = ns['num_doc'] fname = "unknown" try: okay = True if verbose: util.traceLevel = 'debug' fname = util.save_cmd_line(util.command_line_str(ns, seed)) if indexes_enabled: transactional_shim.drop_indexes(collection1) transactional_shim.drop_indexes(collection2) transactional_shim.remove(collection1) transactional_shim.remove(collection2) indexes = [] indexes_first = gen.global_prng.choice([True, False]) if indexes_enabled: for i in range(0, 5): index_obj = gen.random_index_spec() indexes.append(index_obj) if indexes_first: for i in indexes: transactional_shim.ensure_index(collection1, i) transactional_shim.ensure_index(collection2, i) docs = [] for i in range(0, num_doc): doc = gen.random_document(True) docs.append(doc) exception = [] exception_msg = [] for c in [collection1, collection2]: try: transactional_shim.insert(c, docs) except pymongo.errors.OperationFailure as e: exception.append(e) exception_msg.append(' '.join([ 'Caught PyMongo error with collection {} . Offending insert({})' .format(str(c), str(docs)) ])) except MongoModelException as e: exception.append(e) exception_msg.append(' '.join([ 'Caught MongoModel error with collection {} . Offending insert({})' .format(str(c), str(docs)) ])) if not indexes_first: for i in indexes: transactional_shim.ensure_index(collection1, i) transactional_shim.ensure_index(collection2, i) if len(exception_msg) == 1: print '\033[91m', exception[0], '\033[0m' print '\033[91m', exception_msg[0], '\033[0m' okay = False if exception_msg: return (okay, fname, None) okay = check_query(dict(), collection1, collection2) if not okay: return (okay, fname, None) if update_tests_enabled: if not test_update([collection1, collection2], verbose): okay = False return (okay, fname, None) for ii in range(1, 30): query = gen.random_query() if not sorting_tests_enabled: sort = None limit = 0 skip = 0 else: sort = gen.random_query_sort() limit = gen.global_prng.randint(0, 600) skip = gen.global_prng.randint(0, 10) # Always generate a projection, whether or not we use it. This allows us to run the same test in # either case. temp_projection = gen.random_projection() if not projections_enabled: projection = None else: projection = temp_projection okay = check_query(query, collection1, collection2, projection, sort=sort, limit=limit, skip=skip) if not okay: return (okay, fname, None) if not okay: return (okay, fname, None) except Exception as e: # import traceback # traceback.print_exc() return (False, fname, e) return (okay, fname, None)