def to_external_object_time_func(loops, obj):
    begin = perf_counter()
    for _ in range(loops):
        for _ in range(INNER_LOOPS):
            toExternalObject(obj)
    end = perf_counter()
    return end - begin
Example #2
0
def main(runner=None):

    xmlconfig.file('configure.zcml', nti.externalization.tests.benchmarks)

    obj = HasListOfDerived()
    obj.the_objects = [
        DerivedWithOneTextField(text=u"This is some text " + str(i))
        for i in range(10)
    ]

    if '--profile' in sys.argv:
        profile(100, obj)
        return

    if '--vmprofile' in sys.argv:
        vmprofile(100 if not PYPY else 1, obj)
        return

    mt = getattr(obj, 'mimeType')
    assert mt == 'application/vnd.nextthought.benchmarks.haslistofderived', mt

    runner = runner or perf.Runner()
    runner.bench_time_func(__name__ + ": toExternalObject",
                           to_external_object_time_func,
                           obj,
                           inner_loops=INNER_LOOPS)

    ext = toExternalObject(obj)
    assert StandardExternalFields.MIMETYPE in ext

    runner.bench_time_func(__name__ + ": fromExternalObject",
                           update_from_external_object_time_func,
                           ext,
                           inner_loops=INNER_LOOPS)
def profile(loops=1000, obj=None):
    from cProfile import Profile
    import pstats

    if obj is None:
        obj = DerivedWithOneTextField()
        obj.text = u'This is some text'

    ext = toExternalObject(obj)

    for func, arg in (
        (to_external_object_time_func, obj),
        (find_factory_time_func, ext),
        (update_from_external_object_time_func, ext),
    ):

        prof = Profile()
        prof.enable()
        func(loops, arg)
        prof.disable()
        stats = pstats.Stats(prof)
        stats.strip_dirs()
        stats.sort_stats('cumulative')
        print("Profile of", func)
        stats.print_stats(20)
def main(runner=None):

    xmlconfig.file('configure.zcml', nti.externalization.tests.benchmarks)

    if '--profile' in sys.argv:
        profile()
        return

    obj = DerivedWithOneTextField()
    obj.text = u"This is some text"

    # pylint:disable=line-too-long
    assert getattr(
        obj, 'mimeType'
    ) == 'application/vnd.nextthought.benchmarks.derivedwithonetextfield'

    runner = runner or pyperf.Runner()
    runner.bench_time_func(__name__ + ": toExternalObject",
                           to_external_object_time_func,
                           obj,
                           inner_loops=INNER_LOOPS)

    ext = toExternalObject(obj)
    assert StandardExternalFields.MIMETYPE in ext

    runner.bench_time_func(__name__ + ': find factory',
                           find_factory_time_func,
                           ext,
                           inner_loops=INNER_LOOPS)

    runner.bench_time_func(__name__ + ": fromExternalObject",
                           update_from_external_object_time_func,
                           ext,
                           inner_loops=INNER_LOOPS)
Example #5
0
    def test_user_profile(self):

        from nti.externalization.tests.benchmarks.objects import Address
        from nti.externalization.tests.benchmarks.objects import UserProfile

        home_address = Address(
            full_name=u'Steve Jobs',
            street_address_1=u'1313 Mockingbird Lane',
            city=u'Salem',
            state=u'MA',
            postal_code=u'6666',
            country=u'USA',
        )

        work_address = Address(
            full_name=u'Apple',
            street_address_1=u'1 Infinite Loop',
            city=u'Cupertino',
            state=u'CA',
            postal_code=u'55555',
            country=u'USA',
        )

        user_profile = UserProfile(
            addresses={u'home': home_address, u'work': work_address},
            phones={u'home': u'405-555-1212', u'work': u'405-555-2323'},
            contact_emails={u'home': u'*****@*****.**', u'work': u'*****@*****.**'},
            avatarURL='http://apple.com/steve.png',
            backgroundURL='https://apple.com/bg.jpeg',
            alias=u'Steve',
            realname=u'Steve Jobs',
        )

        mt = getattr(UserProfile, 'mimeType')
        assert_that(mt, is_('application/vnd.nextthought.benchmarks.userprofile'))

        ext = toExternalObject(user_profile)
        assert_that(ext, has_key(StandardExternalFields.MIMETYPE))
        assert_that(ext['addresses'], has_key('home'))
        assert_that(ext['addresses']['home'], has_key("city"))


        addr = update_from_external_object(Address(), toExternalObject(home_address))
        assert_that(addr, is_(home_address))
        prof2 = update_from_external_object(UserProfile(), toExternalObject(user_profile))
        assert_that(prof2, is_(user_profile))
Example #6
0
 def describe_mismatch(self, item, mismatch_description):
     ext_obj = toExternalObject(item)
     if ext_obj is None:
         mismatch_description.append_text('externalized to none')
     else:
         text = 'was '
         if INonExternalizableReplacement.providedBy(ext_obj):  # pylint:disable=no-value-for-parameter
             text += 'replaced by '
         mismatch_description.append_text(text).append_description_of(
             ext_obj)
Example #7
0
    def _matches(self, item):
        ext_obj = toExternalObject(item)
        # pylint:disable=no-value-for-parameter
        result = ext_obj is not None and \
                 not INonExternalizableReplacement.providedBy(ext_obj)
        if result and self.matcher is not None:
            result = self.matcher.matches(ext_obj)

        # For convenience, if the truthy value of ext_obj matches the truthy value of result,
        # return the ext_obj
        return ext_obj if result == bool(ext_obj) else result
def main(runner=None):

    xmlconfig.file('configure.zcml', nti.externalization.tests.benchmarks)

    home_address = Address(
        full_name=u'Steve Jobs',
        street_address_1=u'1313 Mockingbird Lane',
        city=u'Salem',
        state=u'MA',
        postal_code=u'6666',
        country=u'USA',
    )

    work_address = Address(
        full_name=u'Apple',
        street_address_1=u'1 Infinite Loop',
        city=u'Cupertino',
        state=u'CA',
        postal_code=u'55555',
        country=u'USA',
    )

    user_profile = UserProfile(
        addresses={u'home': home_address, u'work': work_address},
        phones={u'home': u'405-555-1212', u'work': u'405-555-2323'},
        contact_emails={u'home': u'*****@*****.**', u'work': u'*****@*****.**'},
        avatarURL='http://apple.com/steve.png',
        backgroundURL='https://apple.com/bg.jpeg',
        alias=u'Steve',
        realname=u'Steve Jobs',
    )


    if '--profile' in sys.argv:
        profile(100, user_profile)
        return

    ext = toExternalObject(user_profile)

    runner = runner or perf.Runner()
    runner.bench_time_func(__name__ + ": toExternalObject",
                           to_external_object_time_func,
                           user_profile,
                           inner_loops=INNER_LOOPS)


    runner.bench_time_func(__name__ + ": fromExternalObject",
                           update_from_external_object_time_func,
                           ext,
                           inner_loops=INNER_LOOPS)
Example #9
0
def main(runner=None):

    xmlconfig.file('configure.zcml', nti.externalization.tests.benchmarks)

    obj = SimplestPossibleObject()

    def func():
        toExternalObject(obj)

    runner = runner or perf.Runner()
    runner.bench_func(__name__ + ": toExternalObject", func)

    ext = toExternalObject(obj)
    ext.pop(StandardExternalFields.MIMETYPE, None)

    def from_():
        obj = default_externalized_object_factory_finder(ext)()
        update_from_external_object(obj, ext)

    runner.bench_func(__name__ + ": fromExternalObject", from_)

    def no_args():
        obj = NoArgs()
        update_from_external_object(obj, ext)

    def context_arg():
        obj = ContextArg()
        update_from_external_object(obj, ext)

    def ds_arg():
        obj = DSArg()
        update_from_external_object(obj, ext)

    runner.bench_func(__name__ + ": fromExternalObject (no args)", no_args)

    runner.bench_func(__name__ + ": fromExternalObject (context arg)",
                      context_arg)

    warnings.simplefilter('ignore')
    runner.bench_func(__name__ + ": fromExternalObject (ds arg)", ds_arg)
def vmprofile(loops=1000, obj=None):
    import vmprof  # pylint:disable=import-error

    if obj is None:
        obj = DerivedWithOneTextField()
        obj.text = u'This is some text'

    ext = toExternalObject(obj)

    for func, arg in (
        (to_external_object_time_func, obj),
        (find_factory_time_func, ext),
            # This one often segfaults if we use Cython somewhere in the
            # guts of libunwind.
        (update_from_external_object_time_func, ext),
    ):
        with open('benchmark_results/' + func.__name__ + '.vmprof',
                  'w+b') as f:
            print("Begin", func)
            vmprof.enable(f.fileno())
            func(loops, arg)
            vmprof.disable()
            print("End", func)
 def toExternalList(self):
     result = [toExternalObject(x) for x in self if x is not None]
     return result
 def toExternalDictionary(self, *args, **kwargs):
     result = super(PersistentExternalizableDictionary,
                    self).toExternalDictionary(self, *args, **kwargs)
     for key, value in iteritems(self):
         result[key] = toExternalObject(value, *args, **kwargs)
     return result
def _weakRef_toExternalObject(self):
    val = self()
    return toExternalObject(val) if val is not None else None
Example #14
0
 def func():
     toExternalObject(obj)