Skip to content

Commit df221cf

Browse files
author
Mike Dirolf
committed
replace old benchmark suite with new one
1 parent 19f0d9a commit df221cf

File tree

2 files changed

+108
-173
lines changed

2 files changed

+108
-173
lines changed

tools/benchmark.py

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,129 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Jim's benchmarking suite
16-
"""
15+
"""MongoDB benchmarking suite."""
1716

18-
import datetime
17+
import time
1918
import sys
2019
sys.path[0:0] = [""]
2120

22-
from pymongo.connection import Connection
21+
import datetime
22+
import cProfile
23+
24+
from pymongo import connection
2325
from pymongo import ASCENDING
2426

25-
N = 30000
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+
}
2652

27-
def timed(function, db):
28-
before = datetime.datetime.now()
29-
function(db)
30-
print "%s%s" % (function.__name__.ljust(15), datetime.datetime.now() - before)
53+
def setup_insert(db, collection, object):
54+
db.drop_collection(collection)
3155

32-
def insert(db):
33-
for i in range(N):
34-
db.test.insert({"i": i})
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)
3561

36-
def find_one(db):
37-
for _ in range(N):
38-
db.test.find_one()
62+
def insert_batch(db, collection, object):
63+
for i in range(per_trial / batch_size):
64+
db[collection].insert([object] * batch_size)
3965

40-
def find(db):
41-
for _ in range(N):
42-
for _ in db.test.find({"i": 3}):
43-
pass
44-
for _ in db.test.find({"i": 234}):
45-
pass
46-
for _ in db.test.find({"i": 9876}):
47-
pass
66+
def find_one(db, collection, x):
67+
for _ in range(per_trial):
68+
db[collection].find_one({"x": x})
4869

49-
def find_range(db):
50-
for _ in range(N):
51-
for _ in db.test.find({"i": {"$gt": 200, "$lt": 200}}):
70+
def find(db, collection, x):
71+
for _ in range(per_trial):
72+
for _ in db[collection].find({"x": x}):
5273
pass
5374

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%d" % (name + (60 - len(name)) * ".", per_trial / best_time)
85+
return best_time
86+
5487
def main():
55-
db = Connection().benchmark
56-
db.drop_collection("test")
57-
db.test.create_index("i", ASCENDING)
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}])
58130

59-
timed(insert, db)
60-
timed(find_one, db)
61-
timed(find, db)
62-
timed(find_range, db)
131+
timed("find range (small, indexed)", find,
132+
[db, 'small_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
133+
timed("find range (medium, indexed)", find,
134+
[db, 'medium_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
135+
timed("find range (large, indexed)", find,
136+
[db, 'large_index', {"$gt": per_trial / 2, "$lt": per_trial / 2 + batch_size}])
63137

64138
if __name__ == "__main__":
139+
# cProfile.run("main()")
65140
main()

tools/benchmark1.py

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)