|
| 1 | +# Copyright 2009 10gen, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""MongoDB benchmarking suite.""" |
| 16 | + |
| 17 | +import time |
| 18 | +import sys |
| 19 | +sys.path[0:0] = [""] |
| 20 | + |
| 21 | +import datetime |
| 22 | +import cProfile |
| 23 | + |
| 24 | +from pymongo import connection |
| 25 | +from pymongo import ASCENDING |
| 26 | + |
| 27 | +trials = 2 |
| 28 | +per_trial = 5000 |
| 29 | +batch_size = 100 |
| 30 | +small = {} |
| 31 | +medium = {"integer": 5, |
| 32 | + "number": 5.05, |
| 33 | + "boolean": False, |
| 34 | + "array": ["test", "benchmark"] |
| 35 | + } |
| 36 | +# this is similar to the benchmark data posted to the user list |
| 37 | +large = {"base_url": "http://www.example.com/test-me", |
| 38 | + "total_word_count": 6743, |
| 39 | + "access_time": datetime.datetime.now(), |
| 40 | + "meta_tags": {"description": "i am a long description string", |
| 41 | + "author": "Holly Man", |
| 42 | + "dynamically_created_meta_tag": "who know\n what" |
| 43 | + }, |
| 44 | + "page_structure": {"counted_tags": 3450, |
| 45 | + "no_of_js_attached": 10, |
| 46 | + "no_of_images": 6 |
| 47 | + }, |
| 48 | + "harvested_words": ["10gen","web","open","source","application","paas", |
| 49 | + "platform-as-a-service","technology","helps", |
| 50 | + "developers","focus","building","mongodb","mongo"] * 20 |
| 51 | + } |
| 52 | + |
| 53 | +def setup_insert(db, collection, object): |
| 54 | + db.drop_collection(collection) |
| 55 | + |
| 56 | +def insert(db, collection, object): |
| 57 | + for i in range(per_trial): |
| 58 | + to_insert = object.copy() |
| 59 | + to_insert["x"] = i |
| 60 | + db[collection].insert(to_insert) |
| 61 | + |
| 62 | +def insert_batch(db, collection, object): |
| 63 | + for i in range(per_trial / batch_size): |
| 64 | + db[collection].insert([object] * batch_size) |
| 65 | + |
| 66 | +def find_one(db, collection, x): |
| 67 | + for _ in range(per_trial): |
| 68 | + db[collection].find_one({"x": x}) |
| 69 | + |
| 70 | +def find(db, collection, x): |
| 71 | + for _ in range(per_trial): |
| 72 | + for _ in db[collection].find({"x": x}): |
| 73 | + pass |
| 74 | + |
| 75 | +def timed(name, function, args=[], setup=None): |
| 76 | + times = [] |
| 77 | + for _ in range(trials): |
| 78 | + if setup: |
| 79 | + setup(*args) |
| 80 | + start = time.time() |
| 81 | + function(*args) |
| 82 | + times.append(time.time() - start) |
| 83 | + best_time = min(times) |
| 84 | + print "%s%f" % (name + (60 - len(name)) * ".", best_time) |
| 85 | + return best_time |
| 86 | + |
| 87 | +def main(): |
| 88 | + connection._TIMEOUT=60 # jack up the timeout |
| 89 | + c = connection.Connection() |
| 90 | + c.drop_database("benchmark") |
| 91 | + db = c.benchmark |
| 92 | + |
| 93 | + timed("insert (small, no index)", insert, [db, 'small_none', small], setup_insert) |
| 94 | + timed("insert (medium, no index)", insert, [db, 'medium_none', medium], setup_insert) |
| 95 | + timed("insert (large, no index)", insert, [db, 'large_none', large], setup_insert) |
| 96 | + |
| 97 | + db.small_index.create_index("x", ASCENDING) |
| 98 | + timed("insert (small, indexed)", insert, [db, 'small_index', small]) |
| 99 | + db.medium_index.create_index("x", ASCENDING) |
| 100 | + timed("insert (medium, indexed)", insert, [db, 'medium_index', medium]) |
| 101 | + db.large_index.create_index("x", ASCENDING) |
| 102 | + timed("insert (large, indexed)", insert, [db, 'large_index', large]) |
| 103 | + |
| 104 | + timed("batch insert (small, no index)", insert_batch, [db, 'small_bulk', small], setup_insert) |
| 105 | + timed("batch insert (medium, no index)", insert_batch, [db, 'medium_bulk', medium], setup_insert) |
| 106 | + timed("batch insert (large, no index)", insert_batch, [db, 'large_bulk', large], setup_insert) |
| 107 | + |
| 108 | + timed("find_one (small, no index)", find_one, [db, 'small_none', per_trial / 2]) |
| 109 | + timed("find_one (medium, no index)", find_one, [db, 'medium_none', per_trial / 2]) |
| 110 | + timed("find_one (large, no index)", find_one, [db, 'large_none', per_trial / 2]) |
| 111 | + |
| 112 | + timed("find_one (small, indexed)", find_one, [db, 'small_index', per_trial / 2]) |
| 113 | + timed("find_one (medium, indexed)", find_one, [db, 'medium_index', per_trial / 2]) |
| 114 | + timed("find_one (large, indexed)", find_one, [db, 'large_index', per_trial / 2]) |
| 115 | + |
| 116 | + timed("find (small, no index)", find, [db, 'small_none', per_trial / 2]) |
| 117 | + timed("find (medium, no index)", find, [db, 'medium_none', per_trial / 2]) |
| 118 | + timed("find (large, no index)", find, [db, 'large_none', per_trial / 2]) |
| 119 | + |
| 120 | + timed("find (small, indexed)", find, [db, 'small_index', per_trial / 2]) |
| 121 | + timed("find (medium, indexed)", find, [db, 'medium_index', per_trial / 2]) |
| 122 | + timed("find (large, indexed)", find, [db, 'large_index', per_trial / 2]) |
| 123 | + |
| 124 | +# timed("find range (small, no index)", find, |
| 125 | +# [db, 'small_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}]) |
| 126 | +# timed("find range (medium, no index)", find, |
| 127 | +# [db, 'medium_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}]) |
| 128 | +# timed("find range (large, no index)", find, |
| 129 | +# [db, 'large_none', {"$gt": per_trial / 4, "$lt": 3 * per_trial / 4}]) |
| 130 | + |
| 131 | + timed("find range (small, indexed)", find, |
| 132 | + [db, 'small_index', {"$gt": per_trial / 2, "$lt": per_trial / 4 + batch_size}]) |
| 133 | + timed("find range (medium, indexed)", find, |
| 134 | + [db, 'medium_index', {"$gt": per_trial / 2, "$lt": per_trial / 4 + batch_size}]) |
| 135 | + timed("find range (large, indexed)", find, |
| 136 | + [db, 'large_index', {"$gt": per_trial / 2, "$lt": per_trial / 4 + batch_size}]) |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | +# cProfile.run("main()") |
| 140 | + main() |
0 commit comments