[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexDS9/tests - __init__.py:1.1.2.1 mailtest.py:1.1.2.1

Jeremy Hylton jeremy@zope.com
Tue, 30 Apr 2002 01:20:15 -0400


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexDS9/tests
In directory cvs.zope.org:/tmp/cvs-serv22666/TextIndexDS9/tests

Added Files:
      Tag: TextIndexDS9-branch
	__init__.py mailtest.py 
Log Message:
Create some placeholders and a trivial test-driver.


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexDS9/tests/__init__.py ===


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexDS9/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