diff options
-rwxr-xr-x | profile | 76 | ||||
-rwxr-xr-x | speed-test | 34 |
2 files changed, 27 insertions, 83 deletions
diff --git a/profile b/profile deleted file mode 100755 index 1560d80..0000000 --- a/profile +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/python -# Copyright 2010 Lars Wirzenius -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - - -import os -import cProfile -import pstats -import random -import shutil -import sys -import tempfile -import time -import uuid - -import btree - - -def measure(keys, func): - start = time.clock() - for key in keys: - func(key) - end = time.clock() - return end - start - - -def generate_keys(key_size): - while True: - u = uuid.uuid4() - mainkey = u.bytes[:9] - subkey = u.bytes[-9:] - for subtype in range(4): - yield '%s%c%s' % (mainkey, subtype, subkey) - - -def main(): - n = int(sys.argv[1]) - - key_size = 19 - value_size = 128 - node_size = 64*1024 - - location = tempfile.mkdtemp() - - codec = btree.NodeCodec(key_size) - if True: - ns = btree.NodeStoreMemory(node_size, codec) - else: - ns = btree.NodeStoreDisk(location, node_size, codec) - forest = btree.Forest(ns) - tree = forest.new_tree() - - keys = [key for i, key in zip(xrange(n), generate_keys(key_size))] - - random.shuffle(keys) - value = 'x' * value_size - measure(keys, lambda key: tree.insert(key, value)) - - if location: - shutil.rmtree(location) - -if __name__ == '__main__': - cProfile.run('main()', 'insert.prof') - @@ -26,6 +26,7 @@ # the lookups. +import cProfile import os import random import shutil @@ -43,11 +44,21 @@ def measure(keys, func): return end - start +def profile(keys, func, basename): + def helper(): + for key in keys: + func(key) + globaldict = globals().copy() + localdict = locals().copy() + cProfile.runctx('helper()', globaldict, localdict, '%s.prof' % basename) + + def main(): location = sys.argv[1] n = int(sys.argv[2]) + do_profile = True if sys.argv[3] == 'yes' else False - key_size = 8 + key_size = 19 value_size = 128 node_size = 64*1024 @@ -73,16 +84,25 @@ def main(): # Measure inserts. random.shuffle(keys) value = 'x' * value_size - insert_time = measure(keys, lambda key: tree.insert(key, value)) - looptime - + if do_profile: + profile(keys, lambda key: tree.insert(key, value), 'insert') + else: + insert_time = measure(keys, lambda key: tree.insert(key, value)) + # Measure lookups. random.shuffle(keys) - lookup_time = measure(keys, lambda key: tree.lookup(key)) - looptime + if do_profile: + profile(keys, tree.lookup, 'lookup') + else: + lookup_time = measure(keys, lambda key: tree.lookup(key)) - looptime # Report - print 'num_operations: %d' % n - print 'insert: %.3f s (%.1f/s)' % (insert_time, n/insert_time) - print 'lookup-time: %.3f s (%.1f/s)' % (lookup_time, n/lookup_time) + if do_profile: + print 'See insert.prof, lookup.prof for profiling data (try viewprof)' + else: + print 'num_operations: %d' % n + print 'insert: %.3f s (%.1f/s)' % (insert_time, n/insert_time) + print 'lookup-time: %.3f s (%.1f/s)' % (lookup_time, n/lookup_time) # Clean up if location: |