pipeTestServiceClient.py 4.0 KB

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