~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to bin/notranstest/clientside/slave.py

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import SOAPpy
 
2
import random
 
3
import getopt
 
4
import sys
 
5
import test_functions
 
6
import time
 
7
import threading
 
8
import urllib2
 
9
tf = test_functions
 
10
ID_KEY_LENGTH = 15
 
11
ip = "0.0.0.0"
 
12
TEST = False
 
13
TARGETS = [] 
 
14
DEBUG = False
 
15
NUM_REQ = 0
 
16
TOTAL_NUM_ACCOUNTS = 100000
 
17
def padString(string):
 
18
  zero_padded_id = ("0" * (ID_KEY_LENGTH - len(string))) + string
 
19
  return zero_padded_id
 
20
 
 
21
class webRequest(threading.Thread):
 
22
  def setup(self, target, sender, receiver, amount, retries):
 
23
    self.target = target
 
24
    self.sender = sender
 
25
    self.receiver = receiver 
 
26
    self.amount = amount 
 
27
    self.retries = retries
 
28
    self.success = False
 
29
  def run(self):
 
30
    global TEST
 
31
    global DEBUG
 
32
    if TEST:
 
33
      ret = tf.GetIndex(self.target[0], self.target[1], self.sender, self.receiver, self.amount, self.retries)
 
34
    else:
 
35
      try:
 
36
        ret = tf.CreateTransfer(self.target[0], self.target[1], self.sender, self.receiver, self.amount, self.retries)
 
37
      except urllib2.URLError, e:
 
38
        ret = "Exception tossed. Timedout"
 
39
    lines = ret.split('\n')
 
40
    self.success = False 
 
41
    for line in lines: 
 
42
      if line.startswith("Success:"):  
 
43
        if line.endswith("True"): 
 
44
          self.success = True  
 
45
      if TEST:
 
46
        if line.startswith("<"):
 
47
          self.success = True
 
48
    if DEBUG and self.success == False:
 
49
      print lines
 
50
    return
 
51
 
 
52
def setup(targetlist, numrequest, seed):
 
53
  global DEBUG
 
54
  global TARGETS 
 
55
  global NUM_REQ 
 
56
  TARGETS = targetlist 
 
57
  NUM_REQ = numrequest  
 
58
  random.seed(seed)
 
59
  if DEBUG:
 
60
    print "Target list:",targetlist
 
61
    print "Num of request:", numrequest
 
62
    print "Seed:",seed
 
63
 
 
64
  return
 
65
 
 
66
def run():
 
67
  global DEBUG
 
68
  global TARGETS 
 
69
  global NUM_REQ
 
70
  global TOTAL_NUM_ACCOUNTS
 
71
  if DEBUG:
 
72
    print "Done"
 
73
  allRequest = []
 
74
  for ii in range(0, NUM_REQ):
 
75
    rand1 = random.randint(0,TOTAL_NUM_ACCOUNTS - 1) 
 
76
    rand2 = random.randint(0,TOTAL_NUM_ACCOUNTS - 1) 
 
77
    sender = padString(str(rand1))
 
78
    receiver = padString(str(rand2)) 
 
79
    amount = "1"
 
80
    retries = 3
 
81
    # Targets is a list of ip, port
 
82
    rand3 = random.randint(0, len(TARGETS)/2 - 1)
 
83
    if DEBUG: print "target selected:",rand3
 
84
    target = (TARGETS[rand3 * 2],TARGETS[rand3 * 2 +1])
 
85
    print "Selected target:",target
 
86
    wr = webRequest()
 
87
    wr.setup(target, sender, receiver, amount, retries)
 
88
    allRequest.append(wr)
 
89
  startTime = time.time()
 
90
  for ii in allRequest:
 
91
    ii.start()
 
92
  endTime = time.time()
 
93
  if DEBUG:
 
94
    print "Time taken to create threads:",(endTime-startTime)
 
95
  successes = 0
 
96
  for ii in allRequest:
 
97
    ii.join()
 
98
    if ii.success:
 
99
      successes += 1
 
100
  endTime = time.time()
 
101
  if DEBUG:
 
102
    print "Time to complete all request:",(endTime - startTime) 
 
103
  return [successes, NUM_REQ] 
 
104
 
 
105
def usage():
 
106
  print "-b for bind address"
 
107
 
 
108
def main(argv):
 
109
  global DEBUG
 
110
  global TEST
 
111
  try:
 
112
    opts, args = getopt.getopt(argv, "b:dt",
 
113
                          ["bind=",
 
114
                           "debug",
 
115
                           "test"])
 
116
  except getopt.GetoptError:
 
117
    usage()
 
118
    sys.exit(1)
 
119
  bindport = 0
 
120
  for opt, arg in opts:
 
121
    if opt in ("-b", "--bind"):
 
122
      bindport = int(arg) 
 
123
    if opt in ("-d"):
 
124
      print "Debugging turned on"
 
125
      DEBUG = True
 
126
    if opt in ("-t"):
 
127
      print "Testing mode"
 
128
      TEST = True
 
129
  if bindport == 0:
 
130
    print "Bind port must be set to a valid value"
 
131
    usage()
 
132
    sys.exit(1)
 
133
 
 
134
  server = SOAPpy.SOAPServer((ip, bindport))
 
135
  server.registerFunction(setup)
 
136
  server.registerFunction(run)
 
137
 
 
138
  while 1:
 
139
    # Run Server
 
140
    server.serve_forever()
 
141
 
 
142
if __name__ == "__main__":
 
143
  main(sys.argv[1:])