Additional comments & spellcheck for the README file
[integration/test.git] / test / tools / odl-mdsal-clustering-tests / clustering-performance-test / restconf_incr_put.py
1 __author__ = "Reinaldo Penno"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __version__ = "0.1"
5 __email__ = "repenno@cisco.com"
6 __status__ = "alpha"
7
8
9 #####
10 # Incrementally PUT(s) more and more list elements up to numputreq
11 # while computing the number of req/s and successful requests
12 #
13 # Then measure the number of GET request/sec up to numgetreq
14 # while computing the number of successful requests
15
16 # For the default values it is estimated you will consume about
17 # 1.5GB of heap memory in ODL
18 ####
19
20
21 import requests
22 import time
23
24 class Timer(object):
25     def __init__(self, verbose=False):
26         self.verbose = verbose
27
28     def __enter__(self):
29         self.start = time.time()
30         return self
31
32     def __exit__(self, *args):
33         self.end = time.time()
34         self.secs = self.end - self.start
35         self.msecs = self.secs * 1000  # millisecs
36         if self.verbose:
37             print ("elapsed time: %f ms" % self.msecs)
38
39 # Parametrized PUT of incremental List elements            
40 JSONPUT = """
41 {
42   "service-function": [
43     {
44       "ip-mgmt-address": "20.0.0.11",
45       "type": "service-function:napt44",
46       "name": "%d"
47     }
48   ]
49 }"""
50
51 putheaders = {'content-type': 'application/json'}
52 getheaders = {'Accept': 'application/json'}
53 # ODL IP:port
54 ODLIP   = "127.0.0.1:8080"
55 # We fist delete all existing service functions
56 DELURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/"
57 GETURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/service-function/%d/"
58 # Incremental PUT. This URL is for a list element
59 PUTURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/service-function/%d/"
60
61 # You probably need to adjust this number based on your OS constraints.
62 # Maximum number of incremental PUT list elements
63 numputreq = 1000000
64 # Maximum number of GET requests
65 numgetreq = 10000
66 # We will present PUT reports every 10000 PUTs
67 numputstep = 1000
68 # We will present GET reports every 10000 PUTs
69 numgetstep = 1000
70
71 # Incrementally PUT list elements up to numputreq
72 def putperftest():
73     s = requests.Session()
74     print ("Starting PUT Performance. Total of %d requests\n" % numputreq)
75     for numreq in range(0, numputreq, numputstep): 
76         success = 0      
77         with Timer() as t:
78             for i in range(numreq, numreq + numputstep):
79                 r = s.put((PUTURL % i),data = (JSONPUT % i), headers=putheaders, stream=False )
80                 if (r.status_code == 200):
81                     success+=1
82         print ("=> %d elapsed requests" % (numreq + numputstep))
83         print ("=> %d requests/s in the last %d reqs" % ((numputstep)/t.secs, numputstep))
84         print ("=> %d successful PUT requests in the last %d reqs " % (success, numputstep))
85         print ("\n")
86
87 # Delete all service functions
88 def delallsf():
89     print ("Deleting all Service Functions")
90     r = requests.delete(DELURL, headers=getheaders)   
91     if (r.status_code == 200) or (r.status_code == 500):
92         print ("Deleted all Service Functions \n")
93         return 0
94     else:
95         print ("Delete Failed \n")
96         exit()
97         return -1
98
99 # Retrieve list elements 
100 def getperftest():
101     s = requests.Session()
102     print ("Starting GET Performance. Total of %d requests \n" % numgetreq)
103     for numreq in range(0, numgetreq, numgetstep): 
104         success = 0      
105         with Timer() as t:
106             for i in range(numreq, numreq + numgetstep):
107                 r = s.get((GETURL % i), stream=False )
108                 if (r.status_code == 200):
109                     success+=1
110         print ("=> %d elapsed requests" % (numreq + numgetstep))
111         print ("=> %d requests/s in the last %d reqs" % ((numgetstep)/t.secs, numgetstep))
112         print ("=> %d successful GET requests in the last %d reqs " % (success, numgetstep))
113         print ("\n")
114
115
116 if __name__ == "__main__":
117     delallsf()
118     putperftest()
119     getperftest()
120
121
122
123
124
125
126