home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2502 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.9 KB  |  131 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from win32pipe import *
  5. from win32file import *
  6. from win32event import *
  7. import pywintypes
  8. import win32api
  9. import winerror
  10. import sys
  11. import os
  12. import traceback
  13. verbose = 0
  14.  
  15. def CallPipe(fn, args):
  16.     ret = None
  17.     retryCount = 0
  18.     while retryCount < 8:
  19.         retryCount = retryCount + 1
  20.         
  21.         try:
  22.             return apply(fn, args)
  23.         continue
  24.         except win32api.error:
  25.             (rc, fnerr, msg) = None
  26.             if rc == winerror.ERROR_PIPE_BUSY:
  27.                 win32api.Sleep(5000)
  28.                 continue
  29.             else:
  30.                 raise win32api.error, (rc, fnerr, msg)
  31.             rc == winerror.ERROR_PIPE_BUSY
  32.         
  33.  
  34.         None<EXCEPTION MATCH>win32api.error
  35.     raise RuntimeError, 'Could not make a connection to the server'
  36.  
  37.  
  38. def testClient(server, msg):
  39.     if verbose:
  40.         print 'Sending', msg
  41.     
  42.     data = CallPipe(CallNamedPipe, ('\\\\%s\\pipe\\PyPipeTest' % server, msg, 256, NMPWAIT_WAIT_FOREVER))
  43.     if verbose:
  44.         print "Server sent back '%s'" % data
  45.     
  46.     print 'Sent and received a message!'
  47.  
  48.  
  49. def testLargeMessage(server, size = 4096):
  50.     if verbose:
  51.         print 'Sending message of size %d' % size
  52.     
  53.     msg = '*' * size
  54.     data = CallPipe(CallNamedPipe, ('\\\\%s\\pipe\\PyPipeTest' % server, msg, 512, NMPWAIT_WAIT_FOREVER))
  55.     if len(data) - size:
  56.         print 'Sizes are all wrong - send %d, got back %d' % (size, len(data))
  57.     
  58.  
  59.  
  60. def stressThread(server, numMessages, wait):
  61.     
  62.     try:
  63.         for i in xrange(numMessages):
  64.             r = CallPipe(CallNamedPipe, ('\\\\%s\\pipe\\PyPipeTest' % server, '#' * 512, 1024, NMPWAIT_WAIT_FOREVER))
  65.     except:
  66.         traceback.print_exc()
  67.         print 'Failed after %d messages' % i
  68.     finally:
  69.         SetEvent(wait)
  70.  
  71.  
  72.  
  73. def stressTestClient(server, numThreads, numMessages):
  74.     import thread
  75.     thread_waits = []
  76.     for t_num in xrange(numThreads):
  77.         wait = CreateEvent(None, 0, 0, None)
  78.         thread_waits.append(wait)
  79.         thread.start_new_thread(stressThread, (server, numMessages, wait))
  80.     
  81.     WaitForMultipleObjects(thread_waits, 1, INFINITE)
  82.  
  83.  
  84. def main():
  85.     global verbose
  86.     import sys
  87.     import getopt
  88.     import string
  89.     server = '.'
  90.     thread_count = 0
  91.     msg_count = 500
  92.     
  93.     try:
  94.         (opts, args) = getopt.getopt(sys.argv[1:], 's:t:m:vl')
  95.         for o, a in opts:
  96.             if o == '-s':
  97.                 server = a
  98.             
  99.             if o == '-m':
  100.                 msg_count = string.atoi(a)
  101.             
  102.             if o == '-t':
  103.                 thread_count = string.atoi(a)
  104.             
  105.             if o == '-v':
  106.                 verbose = 1
  107.             
  108.             if o == '-l':
  109.                 testLargeMessage(server)
  110.                 continue
  111.         
  112.         msg = string.join(args)
  113.     except getopt.error:
  114.         msg = None
  115.         print msg
  116.         my_name = os.path.split(sys.argv[0])[1]
  117.         print 'Usage: %s [-v] [-s server] [-t thread_count=0] [-m msg_count=500] msg ...' % my_name
  118.         print '       -v = verbose'
  119.         print '       Specifying a value for -t will stress test using that many threads.'
  120.         return None
  121.  
  122.     testClient(server, msg)
  123.     if thread_count > 0:
  124.         print 'Spawning %d threads each sending %d messages...' % (thread_count, msg_count)
  125.         stressTestClient(server, thread_count, msg_count)
  126.     
  127.  
  128. if __name__ == '__main__':
  129.     main()
  130.  
  131.