home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2645 < prev    next >
Encoding:
Text File  |  1999-09-02  |  3.6 KB  |  120 lines

  1. # A Test Program for pipeTestService.py
  2. #
  3. # Install and start the Pipe Test service, then run this test
  4. # either from the same machine, or from another using the "-s" param.
  5. #
  6. # Eg: pipeTestServiceClient.py -s server_name Hi There
  7. # Should work.
  8.  
  9. from win32pipe import *
  10. from win32file import *
  11. from win32event import *
  12. import pywintypes
  13. import win32api
  14. import winerror
  15. import sys, os, traceback
  16.  
  17. verbose = 0
  18.  
  19. #def ReadFromPipe(pipeName):
  20.     # Could (Should?) use CallNamedPipe, but this technique allows variable size
  21.     # messages (whereas you must supply a buffer size for CallNamedPipe!
  22. #    hPipe = CreateFile(pipeName, GENERIC_WRITE, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  23. #    more = 1
  24. #    while more:
  25. #        hr = ReadFile(hPipe, 256)
  26. #        if hr==0:
  27. #            more = 0
  28. #        except win32api.error (hr, fn, desc):
  29. #            if hr==winerror.ERROR_MORE_DATA:
  30. #                data = dat
  31. #            
  32.  
  33. def CallPipe(fn, args):
  34.     ret = None
  35.     retryCount = 0
  36.     while retryCount < 8:    # Keep looping until user cancels.
  37.         retryCount = retryCount + 1
  38.         try:
  39.             return apply(fn, args)
  40.         except win32api.error, (rc, fnerr, msg):
  41.             if rc==winerror.ERROR_PIPE_BUSY:
  42.                 win32api.Sleep(5000)
  43.                 continue
  44.             else:
  45.                 raise win32api.error, (rc, fnerr, msg)
  46.  
  47.     raise RuntimeError, "Could not make a connection to the server"
  48.  
  49. def testClient(server,msg):
  50.     if verbose:
  51.         print "Sending", msg
  52.     data = CallPipe(CallNamedPipe, ("\\\\%s\\pipe\\PyPipeTest" % server, msg, 256, NMPWAIT_WAIT_FOREVER))
  53.     if verbose:
  54.         print "Server sent back '%s'" % data
  55.  
  56. def testLargeMessage(server, size = 4096):
  57.     if verbose:
  58.         print "Sending message of size %d" % (size)
  59.     msg = "*" * size
  60.     data = CallPipe(CallNamedPipe, ("\\\\%s\\pipe\\PyPipeTest" % server, msg, 512, NMPWAIT_WAIT_FOREVER))
  61.     if len(data)-size:
  62.         print "Sizes are all wrong - send %d, got back %d" % (size, len(data))
  63.  
  64. def stressThread(server, numMessages, wait):
  65.     try:
  66.         try:
  67.             for i in xrange(numMessages):
  68.                 r = CallPipe(CallNamedPipe, ("\\\\%s\\pipe\\PyPipeTest" % server, "#" * 512, 1024, NMPWAIT_WAIT_FOREVER))
  69.         except:
  70.             traceback.print_exc()
  71.             print "Failed after %d messages" % i
  72.     finally:
  73.         SetEvent(wait)
  74.  
  75. def stressTestClient(server, numThreads, numMessages):
  76.     import thread
  77.     thread_waits = []
  78.     for t_num in xrange(numThreads):
  79.         # Note I could just wait on thread handles (after calling DuplicateHandle)
  80.         # See the service itself for an example of waiting for the clients...
  81.         wait = CreateEvent(None, 0, 0, None)
  82.         thread_waits.append(wait)
  83.         thread.start_new_thread(stressThread, (server,numMessages, wait))
  84.     # Wait for all threads to finish.
  85.     WaitForMultipleObjects(thread_waits, 1, INFINITE)
  86.  
  87. def main():
  88.     import sys, getopt, string
  89.     server = "."
  90.     thread_count = 0
  91.     msg_count = 500
  92.     try:
  93.         opts, args = getopt.getopt(sys.argv[1:], 's:t:m:vl')
  94.         for o,a in opts:
  95.             if o=='-s':
  96.                 server = a
  97.             if o=='-m':
  98.                 msg_count = string.atoi(a)
  99.             if o=='-t':
  100.                 thread_count = string.atoi(a)
  101.             if o=='-v':
  102.                 global verbose
  103.                 verbose = 1
  104.             if o=='-l':
  105.                 testLargeMessage(server)
  106.         msg = string.join(args)
  107.     except getopt.error, msg:
  108.         print msg
  109.         my_name = os.path.split(sys.argv[0])[1]
  110.         print "Usage: %s [-v] [-s server] [-t thread_count=0] [-m msg_count=500] msg ..." % my_name
  111.         print "       -v = verbose"
  112.         print "       Specifying a value for -t will stress test using that many threads."
  113.     testClient(server, msg)
  114.     if thread_count > 0:
  115.         print "Spawning %d threads each sending %d messages..." % (thread_count, msg_count)
  116.         stressTestClient(server, thread_count, msg_count)
  117.  
  118. if __name__=='__main__':
  119.     main()
  120.