[Zope-CVS] CVS: Products/ZCTextIndex/tests - __init__.py:1.1.2.1 mailtest.py:1.1.2.1

Jim Fulton jim@zope.com
Tue, 30 Apr 2002 13:04:50 -0400


Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv22461/tests

Added Files:
      Tag: TextIndexDS9-branch
	__init__.py mailtest.py 
Log Message:
Set up workspace for new TextIndex work.


=== Added File Products/ZCTextIndex/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""

Revision information:
$Id: __init__.py,v 1.1.2.1 2002/04/30 17:04:50 jim Exp $
"""



=== Added File Products/ZCTextIndex/tests/mailtest.py ===
import ZODB
import ZODB.FileStorage
from Products.PluginIndexes.TextIndex.TextIndex import TextIndex

import mailbox
import time

class Message:

    total_bytes = 0
    
    def __init__(self, msg):
        self.msg = msg

    def text(self):
        buf = self.msg.fp.read()
        Message.total_bytes += len(buf)
        return buf

def main(inp, out):
    global NUM
    idx_time = 0
    pack_time = 0
    
    f = ZODB.FileStorage.FileStorage(out)
    db = ZODB.DB(f)
    cn = db.open()
    rt = cn.root()
    rt["index"] = idx = TextIndex("text")
    get_transaction().commit()

    mbox = mailbox.UnixMailbox(open(inp))
    if VERBOSE:
        print "opened", inp
    if not NUM:
        NUM = sys.maxint
    i = 0
    while i < NUM:
        i += 1
        msg = Message(mbox.next())
        i0 = time.clock()
        idx.index_object(i, msg)
        get_transaction().commit()
        i1 = time.clock()
        idx_time += i1 - i0
        if VERBOSE and i % 50 == 0:
            print i, "messages indexed"
        if i % PACK_INTERVAL == 0:
            p0 = time.clock()
            db.pack(time.time())
            p1 = time.clock()
            print "pack took %s sec" % (p1 - p0)
            pack_time += p1 - p0
    return idx_time, pack_time

if __name__ == "__main__":
    import sys
    import getopt

    NUM = 0
    VERBOSE = 0
    PACK_INTERVAL = 500
    opts, args = getopt.getopt(sys.argv[1:], 'vn:p:')
    for o, v in opts:
        if o == '-n':
            NUM = int(v)
        elif o == '-v':
            VERBOSE += 1
        elif o == '-p':
            PACK_INTERVAL = int(v)
    inp, out = args
    ti, tp = main(inp, out)
    print "Index time", ti
    print "Index bytes", Message.total_bytes