check-in scripts (passed by Jan Medved) related to
[integration/test.git] / test / tools / odl-mdsal-clustering-tests / clustering-performance-test / restconf_oneput_ngets.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 import requests
10 import time
11
12 class Timer(object):
13     def __init__(self, verbose=False):
14         self.verbose = verbose
15
16     def __enter__(self):
17         self.start = time.time()
18         return self
19
20     def __exit__(self, *args):
21         self.end = time.time()
22         self.secs = self.end - self.start
23         self.msecs = self.secs * 1000  # millisecs
24         if self.verbose:
25             print ("elapsed time: %f ms" % self.msecs)
26             
27 # Parametrized single PUT of list + one element.            
28 JSONPUT = """
29 {
30   "service-functions": {
31     "service-function": [
32       {
33         "ip-mgmt-address": "20.0.0.10",
34         "type": "dpi",
35         "name": "%d"
36       }
37     ]
38   }
39 }"""
40
41 putheaders = {'content-type': 'application/json'}
42 getheaders = {'Accept': 'application/json'}
43 ODLIP   = "127.0.0.1:8080"
44 DELURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/"
45 GETURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/"
46 PUTURL  = "http://" + ODLIP + "/restconf/config/service-function:service-functions/"
47
48 # You probably need to adjust this number based on your OS constraints.
49 # Maximum number of incremental PUT list elements
50 numputreq = 1
51 # Maximum number of GET requests
52 numgetreq = 100000
53 # We will present PUT reports every 10000 PUTs
54 numputstep = 1
55 # We will present GET reports every 10000 PUTs
56 numgetstep = 1000
57
58
59 def getperftest():
60     s = requests.Session()
61     print ("Starting GET Performance. Total of %d requests \n" % numgetreq)
62     for numreq in range(0, numgetreq, numgetstep): 
63         success = 0      
64         with Timer() as t:
65             for i in range(numreq, numreq + numgetstep):
66                 r = s.get(GETURL, stream=False )
67                 if (r.status_code == 200):
68                     success+=1
69         print ("=> %d elapsed requests" % (numreq + numgetstep))
70         print ("=> %d requests/s in the last %d reqs" % ((numgetstep)/t.secs, numgetstep))
71         print ("=> %d successful GET requests in the last %d reqs " % (success, numgetstep))
72         print ("\n")
73
74 # Based on default parameters performs a single PUT. Always overwrite existing elements
75 def putperftest():
76     s = requests.Session()
77     print ("Starting PUT Performance. Total of %d requests\n" % numputreq)
78     for numreq in range(0, numputreq, numputstep): 
79         success = 0      
80         with Timer() as t:
81             for i in range(numreq, numreq + numputstep):
82                 r = s.put(PUTURL, data = (JSONPUT % i), headers=putheaders, stream=False )
83                 if (r.status_code == 200):
84                     success+=1
85         print ("=> %d elapsed requests" % (numreq + numputstep))
86         print ("=> %d requests/s in the last %d reqs" % ((numputstep)/t.secs, numputstep))
87         print ("=> %d successful PUT requests in the last %d reqs " % (success, numputstep))
88         print ("\n")
89
90 # Delete all service functions
91 def delallsf():
92     print ("Deleting all Service Functions")
93     r = requests.delete(DELURL, headers=getheaders)   
94     if (r.status_code == 200) or (r.status_code == 500):
95         print ("Deleted all Service Functions \n")
96         return 0
97     else:
98         print ("Delete Failed \n")
99         exit()
100         return -1
101
102 if __name__ == "__main__":
103     delallsf()
104     putperftest()
105     getperftest()
106
107
108
109
110
111