Public
Snippet $165 authored by iv

Compare different kind of Folders on `objectValues` method

Edited
comparison.py
from zExceptions import BadRequest
from time import time
import cProfile, pstats, StringIO

# to put in a script called by an external method

def comparison(self):
  pt = self.getPortalObject().portal_types
  portal = self.getPortalObject()
  try:
    # create portal_types
    pt.newContent('BTree Folder Module', 'Base Type',
                        type_class='Folder',
                        type_filter_content_type=1,
                        type_allowed_content_type_list=('Foo', 'Bar'),
                        type_hidden_content_type_list=('Foo', 'Bar'),
                        type_base_category_list=(),
                        type_property_sheet_list=()
                      )

    pt.newContent('ERP5 OFS Folder Module', 'Base Type',
                      type_class='ERP5OFSFolder',
                      type_filter_content_type=1,
                      type_allowed_content_type_list=('Foo', 'Bar'),
                      type_hidden_content_type_list=('Foo', 'Bar'),
                      type_base_category_list=(),
                      type_property_sheet_list=()
                    )
  
    pt.newContent('ERP5 Persistent Mapping Module', 'Base Type',
                            type_class='ERP5PersistentMappingFolder',
                            type_filter_content_type=1,
                            type_allowed_content_type_list=('Foo', 'Bar'),
                            type_hidden_content_type_list=('Foo', 'Bar'),
                            type_base_category_list=(),
                            type_property_sheet_list=()
                           )
  
    pt.newContent('ERP5 Persistent Mapping Index Module', 'Base Type',
                                   type_class='ERP5PersistentMappingIndexFolder',
                                   type_filter_content_type=1,
                                   type_allowed_content_type_list=('Foo', 'Bar'),
                                   type_hidden_content_type_list=('Foo', 'Bar'),
                                   type_base_category_list=(),
                                   type_property_sheet_list=()
                                 )
  except BadRequest:
    pass

  try:
    # create folders
    btree_module = portal.newContent(id='btree_module', portal_type='BTree Folder Module')
    ofs_module = portal.newContent(id='ofs_module', portal_type='ERP5 OFS Folder Module')
    persistent_module = portal.newContent(id='persistent_module', portal_type='ERP5 Persistent Mapping Module')
    index_module = portal.newContent(id='index_module', portal_type='ERP5 Persistent Mapping Index Module')
  except BadRequest:
    btree_module = portal.btree_module
    ofs_module = portal.ofs_module
    persistent_module = portal.persistent_module
    index_module = portal.index_module

  for module in (btree_module, ofs_module, persistent_module, index_module):
    module_content = module.objectValues()
    if module_content:
      module.manage_delObjects([x.id for x in module_content])

  result = "RESULT\n\n"

  for module in (ofs_module, btree_module, persistent_module, index_module):
    creation_initial_time = time()
    for i in range(400):
      module.newContent(id='foo'+str(i), portal_type='Foo')
    creation_final_time = time()
    
    not_filtered_initial_time = time()
    for _ in range(1000):
      unused = [o for o in module.objectValues()]
    not_filtered_final_time = time()

    for i in range(400):
      module.newContent(id='bar'+str(i), portal_type='Bar')

    pr = cProfile.Profile()
    pr.enable()
    filtered_initial_time = time()
    for _ in range(1000):
      unused = [o for o in module.objectValues(portal_type='Foo')]
    filtered_final_time = time()
    pr.disable()

    s = StringIO.StringIO()
    sortby = 'cumulative'
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats(60)
    result += "\n".join([
        "######################################",
        module.id,
        "newContent %s  Foo objects: %0.2f" % (len(module.objectValues(portal_type='Foo')), creation_final_time - creation_initial_time),
        "objectValues without filter: %0.2f" % (not_filtered_final_time - not_filtered_initial_time),
        "objectValues with filter: %0.2f" % (filtered_final_time - filtered_initial_time),
        "\n\n" + s.getvalue(),
        "######################################\n\n"
    ])
  import transaction
  transaction.commit()
  return result