1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | #############################################
|
---|
4 | #Author: Damiano Barboni <barboni@meeo.it>
|
---|
5 | #Version:
|
---|
6 | #Description: Perform parallel read-only requests to RASDAMAN or PETASCOPE
|
---|
7 | #Usage: ./test_parallel_request.py APP N
|
---|
8 | # APP can be "r" for RASDAMAN or "p" for PETASCOPE
|
---|
9 | # N is the number of requests generated
|
---|
10 | #Changelog: None
|
---|
11 | #############################################
|
---|
12 |
|
---|
13 | import httplib
|
---|
14 | import sys
|
---|
15 | import threading
|
---|
16 | import time
|
---|
17 | import commands
|
---|
18 |
|
---|
19 | #global variables definition
|
---|
20 | WCS_host = "earthserverclimate"
|
---|
21 | WCS_port = 8080
|
---|
22 | WCS_url = "/petascope/wcs2?"
|
---|
23 | WCS_service = "WCS"
|
---|
24 | WCS_version = "2.0.0"
|
---|
25 |
|
---|
26 | CoverageId = "NIR"
|
---|
27 | CollectionName = "NIR"
|
---|
28 |
|
---|
29 | Output_File_Path = "/home/rasdaman/test"
|
---|
30 |
|
---|
31 | class GetCoverage():
|
---|
32 | #########################################
|
---|
33 | ############## INIT CLASS ###############
|
---|
34 | #########################################
|
---|
35 | def __init__(self, **kw):
|
---|
36 | #initlog
|
---|
37 | self.Request = "GetCoverage"
|
---|
38 | self.service = WCS_service
|
---|
39 | self.url = WCS_url
|
---|
40 | self.subset = ""
|
---|
41 |
|
---|
42 | #set coverage id
|
---|
43 | self.CoverageId = kw['CoverageId']
|
---|
44 |
|
---|
45 | #set wcs version
|
---|
46 | self.version = WCS_version
|
---|
47 |
|
---|
48 | #########################################
|
---|
49 | ############# SET SUBSET METHOD #########
|
---|
50 | #########################################
|
---|
51 | def setSubset(self, axis, minVal, maxVal):
|
---|
52 | #if the axis is different than x or y log a warning message becuse the axis was not tested
|
---|
53 | if axis.lower()!="x" and axis.lower()!="y" and axis.lower()!="t":
|
---|
54 | self.LOG.warning("%s the axis %s is different than x-y-t and was not tested" %(self.Request, axis))
|
---|
55 | #append new subset to self.subset string
|
---|
56 | if axis.lower()=="t":
|
---|
57 | self.subset += "subset%s=%s(%s,%s)&" %(axis.upper(), axis.lower(), minVal, maxVal)
|
---|
58 | else:#axis.lower()=="x" OR axis.lower()=="y"
|
---|
59 | self.subset += "subset%s=%s(%d,%d)&" %(axis.upper(), axis.lower(), minVal, maxVal)
|
---|
60 |
|
---|
61 | return self.subset
|
---|
62 |
|
---|
63 | #########################################
|
---|
64 | ############ Get data METHOD ############
|
---|
65 | #########################################
|
---|
66 |
|
---|
67 | def getData(self, **kw):
|
---|
68 | #get host coverage id
|
---|
69 | host = kw['host']
|
---|
70 | port = kw['port']
|
---|
71 |
|
---|
72 | #create string
|
---|
73 | request_string = WCS_url + "service=WCS" + "&Request=" + self.Request + "&version=" + self.version + "&CoverageId=" + self.CoverageId + "&" + self.subset
|
---|
74 |
|
---|
75 | conn = WCSRequestGet(host, port)
|
---|
76 | xml = conn.execute(request_string)
|
---|
77 |
|
---|
78 | return xml
|
---|
79 |
|
---|
80 | #implement http reguest to WCS server
|
---|
81 | class WCSRequestGet():
|
---|
82 | def __init__(self, host, port = WCS_port):
|
---|
83 | self.host = host
|
---|
84 | self.port = port
|
---|
85 |
|
---|
86 | def execute(self, wcs_request_string):
|
---|
87 | #init connection
|
---|
88 | conn = httplib.HTTPConnection(self.host+":"+str(self.port))
|
---|
89 | conn.request("GET", wcs_request_string)
|
---|
90 | r = conn.getresponse()
|
---|
91 |
|
---|
92 | #check if response status is 200
|
---|
93 | r_status = r.status
|
---|
94 | if 200 <= r_status <= 299:
|
---|
95 | xml = r.read()
|
---|
96 | else:
|
---|
97 | xml = "ERROR " %(r_status)
|
---|
98 | print ("Unable to get data from WCS server. Bad response status N %d returned by WCS server" %(r_status))
|
---|
99 |
|
---|
100 | conn.close()
|
---|
101 | return xml
|
---|
102 |
|
---|
103 | class requestThread(threading.Thread):
|
---|
104 | def __init__(self,application, i):
|
---|
105 | threading.Thread.__init__(self)
|
---|
106 | self.application = application
|
---|
107 | self.th_number = i
|
---|
108 |
|
---|
109 | #run method
|
---|
110 | def run(self):
|
---|
111 | start = time.time()
|
---|
112 | print "Thread %d STARTED" %(self.th_number)
|
---|
113 | if self.application == "r":
|
---|
114 | #request to rasdaman
|
---|
115 | rasdaman_request = 'rasql -q "select csv(n[0:200,0:200]) from NIR as n" --out string'
|
---|
116 | data_Out = commands.getstatusoutput(rasdaman_request)
|
---|
117 |
|
---|
118 | filename = "%s/rasdaman_output_%d.txt" %(Output_File_Path,self.th_number)
|
---|
119 |
|
---|
120 | elif application == "p":
|
---|
121 | #request to petascope
|
---|
122 | obj = GetCoverage(CoverageId=CoverageId)
|
---|
123 | #set subset
|
---|
124 | obj.setSubset("x", 0,200)
|
---|
125 | obj.setSubset("y", 0,200)
|
---|
126 |
|
---|
127 | #get xml response
|
---|
128 | data_Out = obj.getData(host=WCS_host, port=WCS_port)
|
---|
129 |
|
---|
130 | #save file
|
---|
131 | filename = "%s/petascope_output_%d.xml" %(Output_File_Path,self.th_number)
|
---|
132 |
|
---|
133 | #write output to file
|
---|
134 | out_file = open(filename,"w")
|
---|
135 | out_file.write(data_Out)
|
---|
136 | out_file.close()
|
---|
137 |
|
---|
138 | print "Thread %d ENDED in %f seconds" %(self.th_number,time.time()-start)
|
---|
139 | sys.exit(0)
|
---|
140 |
|
---|
141 | if __name__ == '__main__':
|
---|
142 | try:
|
---|
143 | application = sys.argv[1]
|
---|
144 | req_number = int(sys.argv[2],10)
|
---|
145 | if (application != "r" or application != "p") and req_number < 1:
|
---|
146 | raise
|
---|
147 | except:
|
---|
148 | print "ERROR bad arguments. USAGE:\n\tpython %s APP N" %(sys.argv[0])
|
---|
149 | print '\t\tAPP can be "r" for RASDAMAN or "p" for PETASCOPE'
|
---|
150 | print '\t\tN is the number of requests generated'
|
---|
151 | sys.exit(65)
|
---|
152 |
|
---|
153 | #thread generation
|
---|
154 | thread_list = []
|
---|
155 | for i in range(0, req_number):
|
---|
156 | thread_list.append(requestThread(application, i))
|
---|
157 | #thread execution
|
---|
158 | thread_list[i].start()
|
---|
159 |
|
---|
160 | #Wait for the thread_list tasks to finish
|
---|
161 | for i in range(0,len(thread_list)):
|
---|
162 | thread_list[i].join()
|
---|
163 |
|
---|
164 | print "COMPLETE!"
|
---|
165 |
|
---|
166 | sys.exit(0)
|
---|
167 |
|
---|