[POS-commit] Kiwi/tests benchmark_kgetattr.py,NONE,1.1 test_kgetattr.py,1.1,1.2 test_kgetattr_recycle.py,1.3,1.4

nobody at async.com.br nobody at async.com.br
Sun May 11 04:23:13 BRST 2003


Update of /cvs/Kiwi/tests
In directory anthem:/tmp/cvs-serv31621/tests

Modified Files:
	test_kgetattr.py test_kgetattr_recycle.py 
Added Files:
	benchmark_kgetattr.py 
Log Message:
Reorganisation kgetattr/ksetattr:
-) new cache structure, that contains always only valid entries.
-) Model.get_[gs]etter removed. If you need the basic functionality, use
   accessors.get_default_[gs]etter.
-) The functionality of get_default_[gs]etter has been inlined in kgetattr and
   ksetattr to save one function call.

Unittests:
-) updated to reflect that kgetattr doesn't provide a get_getter anymore.
-) added a simple benchmark to compare the performance of kgetattr with
   the old way.



--- NEW FILE: benchmark_kgetattr.py ---
#!/usr/bin/env python
#
# This is benchmark to compare different access pattern and their performance
# between kgetattr and the old inline style.
#
# (c) 2003 Andreas Kostyrka <andreas at mtg.co.at>
#

import sys, os

sys.path.insert(0, os.environ.get("KIWIPATH",".."))

from Kiwi.FrameWork import kgetattr, ksetattr, Model, clear_attr_cache
from Kiwi import FrameWork

import time

class Getattr_Getter(Model):
    def __init__(self):
        self.a = 0

class Get_Value_Getter(Getattr_Getter):
    def get_getter(self, attr_name, cache):
        return getattr(self, "get_%s" % attr_name)
    def get_a(self):
        return self.a

def timefunc(func, *args, **kw):
    if os.environ.has_key("PROFILENAME"):
        return apply(profiled_timefunc, (func, ) + args, kw)
    st = time.time()
    apply(func, args, kw)
    en = time.time()
    return en - st

profilecount=0

def profiled_timefunc(func, *args, **kw):
    global profilecount
    import hotshot
    pr = hotshot.Profile(os.environ.get("PROFILENAME")+".%03d" % profilecount,
                         lineevents=1)
    profilecount = profilecount + 1
    st = time.time()
    apply(pr.runcall, (func, ) + args, kw)
    en = time.time()
    pr.close()

    return en - st

def classic_getattr(model, attr_name):
    func = getattr(model, "get_%s" % attr_name, None)
    if callable(func):
        return func()
    else:
        return getattr(model, attr_name)

def classic_func(model, attr_name, count):
    for i in xrange(count):
        classic_getattr(model, attr_name)

def classic_inline(model, attr_name, count):
    for i in xrange(count):
        func = getattr(model, "get_%s" % attr_name, None)
        if callable(func):
            func()
        else:
            getattr(model, attr_name)

def kgetattr_multiple(model, attr_name, count):
    for i in xrange(count):
        kgetattr(model, attr_name)

def kgetattr_miss(model, attr_name, count):
    for i in xrange(count):
        kgetattr(model, attr_name)
        FrameWork._kgetattr_cache = {}

count = int(sys.argv[1])

for model, modname in [(Getattr_Getter(),  'getattr'),
                    (Get_Value_Getter(),'get_%s '),]:
    for name, func in [('CLASSIC INLINE', classic_inline),
                       ('CLASSIC FUNC  ', classic_func),
                       ('KGETATTR MULTI', kgetattr_multiple),
                       ('KGETATTR_MISS ', kgetattr_miss)]:
        print "%-10s/%-20s: %8.6f" % (modname, name,
                                      timefunc(func, model, 'a', count))

          

Index: test_kgetattr.py
===================================================================
RCS file: /cvs/Kiwi/tests/test_kgetattr.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- test_kgetattr.py	18 Apr 2003 07:10:21 -0000	1.1
+++ test_kgetattr.py	11 May 2003 07:23:06 -0000	1.2
@@ -7,7 +7,13 @@
 
 sys.path.insert(0, "..")
 
-from Kiwi.FrameWork import kgetattr, ksetattr, Model
+from Kiwi.FrameWork.accessors import get_default_getter, get_default_setter
+
+from Kiwi.FrameWork import kgetattr, ksetattr
+
+class Model:
+    get_getter = get_default_getter
+    get_setter = get_default_setter
 
 class A(Model):
     def __init__(self):

Index: test_kgetattr_recycle.py
===================================================================
RCS file: /cvs/Kiwi/tests/test_kgetattr_recycle.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- test_kgetattr_recycle.py	2 May 2003 18:40:35 -0000	1.3
+++ test_kgetattr_recycle.py	11 May 2003 07:23:06 -0000	1.4
@@ -12,7 +12,12 @@
 
 sys.path.insert(0, os.environ.get("KIWIPATH",".."))
 
-from Kiwi.FrameWork import kgetattr, ksetattr, Model, clear_attr_cache
+from Kiwi.FrameWork.accessors import get_default_getter, get_default_setter
+from Kiwi.FrameWork import kgetattr, ksetattr, clear_attr_cache
+
+class Model:
+    get_getter = get_default_getter
+    get_setter = get_default_setter
 
 class Store(Model):
     def __init__(self):



More information about the POS-commit mailing list