TestThreads.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiddleKit » Tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiddleKit » Tests » TestThreads.py
from Test import Test
import threading


class TestThreads(Test):

  def __init__(self):
    Test.__init__(self)
    self._numObjects = 64
    self._numThreads = [1, 2, 4, 8, 16, 32]
    self._numReads   = 32

  def timedMain(self):
    import time
    start = time.time()
    self.main()
    end = time.time()
    duration = end - start
    print
    print 'secs: %0.2f' % duration
    print 'mins: %0.2f' % (duration/60.0)

  def readArgs(self, args):
    self._modelNames = ['MKBasic']

  def testEmpty(self):
    self.createDatabase()
    self.createStore()
    self.createObjects()
    self.testConcurrentReads()

  def createStore(self):
    from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
    self._store = MySQLObjectStore()
    self._store.readModelFileNamed(self._modelName)

  def createObjects(self):
    from Thing import Thing
    for i in range(self._numObjects):
      t = Thing()
      t.setB(1)
      t.setI(2)
      t.setL(3)
      t.setF(4.0)
      t.setS('five')
      self._store.addObject(t)
    self._store.saveChanges()
    things = self._store.fetchObjectsOfClass('Thing')
    assert len(things) == self._numObjects, \
      '%i, %i' % (len(things), self._numObjects)

  def testConcurrentReads(self):
    for numThreads in self._numThreads:
      print '>> numThreads:', numThreads
      self.testReaderThreads(numThreads)

  def testReaderThreads(self, numThreads):

    class Reader(threading.Thread):

      def __init__(self, store, numReads):
        threading.Thread.__init__(self)
        self._store = store
        self._numReads = numReads

      def run(self):
        store = self._store
        for i in range(self._numReads):
          #print '%x:%03i' % (id(self), i),
          objects = store.fetchObjectsOfClass('Thing')

    threads = []
    for i in range(numThreads):
      thread = Reader(self._store, self._numReads)
      threads.append(thread)

    for thread in threads:
      thread.start()

    for thread in threads:
      thread.join()

  def testSamples(self):
    """Test samples.

    We do all our necessary testing in testEmpty() so we override
    this method to pass.
    
    """
    pass


if __name__ == '__main__':
  import sys
  sys.setcheckinterval(100)
  TestThreads().timedMain()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.