#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
Global tests for EditingContext
This tests the editing context and the whole underlying framework
CVS information
$Id: stressTests_EditingContext.py 932 2004-07-20 06:21:57Z sbigaret $
"""
__version__='$Revision: 932 $'[11:-2]
import unittest
if __name__ == "__main__":
import utils, sys
utils.fixpath()
from Modeling import ModelSet
from Modeling.EditingContext import EditingContext
from Modeling.FetchSpecification import FetchSpecification
from Modeling.Qualifier import qualifierWithQualifierFormat
#from Modeling import Adaptor
#from Modeling import Database
#from PostgresqlAdaptorLayer import PostgresqlAdaptorChannel
from testPackages.AuthorBooks.Writer import Writer
from testPackages.AuthorBooks.Book import Book
def parseConfigFile_and_updateModel(model):
"""
Parse 'test.cfg' and change the model's connection dictionary according to
the options the configuration file sets.
This should only be triggered after the model has been loaded, e.g. after
one of the modules in packages 'AuthorBooks' has been loaded.
"""
from ConfigParser import ConfigParser
import os
defaults={'database': 'AUTHOR_BOOKS',
'host': 'localhost',
'user': 'postgres',
'password': '',
}
cp=ConfigParser()
cp.read('test.cfg')
for key in defaults.keys():
if cp.has_option('Postgresql', key):
defaults[key]=cp.get('Postgresql', key)
model.setConnectionDictionary(defaults)
#print ModelSet.defaultModelSet().modelNamed('AuthorBooks').connectionDictionary()
parseConfigFile_and_updateModel(ModelSet.defaultModelSet().modelNamed('AuthorBooks'))
class StressTests(unittest.TestCase):
"Stress tests for EditingContext"
def test_01_stressTests(self):
"[EditingContext] stress test: 1.000 inserts then saveChanges"
ec=EditingContext()
import time
t0=time.time()
for i in range(1000):
b=Book()
b.setTitle('Hop')
ec.insertObject(b)
t1=time.time()
ec.saveChanges()
t2=time.time()
print '\ntotal time:\t%s'%(t2-t0)
print 'inserts:\t%s'%(t1-t0)
print 'saveChanges:\t%s'%(t2-t1), ' ',
# 1000 / PII 900 Mhz 256 Mo Ram
#19.7838679552
#4.03471696377
#15.7491509914
def test_02_stressTests(self):
"[EditingContext] stress test: 1.000.000 willChange on inserted object"
ec=EditingContext()
import time
t0=time.time()
b=Book()
ec.insertObject(b)
for i in range(1000000):
b.setTitle('Hop')
t1=time.time()
print t1-t0, ' ',
def test_03_stressTests(self):
"[EditingContext] stress test: 1.000.000 willChange on fetched object"
ec=EditingContext()
qualifier=qualifierWithQualifierFormat('lastName=="Dard"')
fetchSpec=FetchSpecification(entityName='Writer', qualifier=qualifier)
objects=ec.objectsWithFetchSpecification(fetchSpec)
self.failUnless(len(objects)==1)
dard=objects[0]
import time
t0=time.time()
b=Book()
ec.insertObject(b)
for i in range(1000000):
b.setTitle('Hop')
t1=time.time()
print t1-t0, ' ',
def usage(prgName, exitStatus=None):
_usage="""%s [-vVp]
Runs the tests for the Modeling package
Options
--------
-v Minimally verbose
-V Really verbose
-h Prints this message
-p enables profiling
""" % prgName
if exitStatus is not None:
print _usage
sys.exit(exitStatus)
else:
return _usage
verbose=0
def main(args):
me=args[0]
import getopt
options, args = getopt.getopt(sys.argv[1:], 'hvVp')
#except: usage(me, 1)
global verbose
profile=0
for k, v in options:
if k=='-h': usage(me, 1)
if k=='-v': verbose=1; continue
if k=='-V': verbose="Y"; continue
if k=='-p': profile=1; continue
if args: usage(me, 1) #raise 'Unexpected arguments', args
if profile:
import profile
profile.run('utils.run_suite(test_suite(), verbosity=verbose)',
'profile.out')
return
else:
return utils.run_suite(test_suite(), verbosity=verbose)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(StressTests, "test_"))
return suite
if __name__ == "__main__":
errs = main(sys.argv)
#errs = utils.run_suite(test_suite())
sys.exit(errs and 1 or 0)
|