Class: BlazemeterApi

Inherits:
Object
  • Object
show all
Defined in:
lib/blazemeter.rb

Constant Summary collapse

@@url =
'https://a.blazemeter.com'

Instance Method Summary collapse

Constructor Details

#initialize(user_key) ⇒ BlazemeterApi


36
37
38
# File 'lib/blazemeter.rb', line 36

def initialize(user_key)
  @user_key = user_key
end

Instance Method Details

#get(path) ⇒ Object


70
71
72
73
74
75
76
# File 'lib/blazemeter.rb', line 70

def get(path)
  uri = URI.parse(@@url+path)
  https = get_https(uri)
  req = Net::HTTP::Get.new(uri.request_uri)
	response = https.request(req)
	return response
end

#get_https(uri) ⇒ Object


40
41
42
43
44
45
# File 'lib/blazemeter.rb', line 40

def get_https(uri)
  https = Net::HTTP.new(uri.host,uri.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
	return https
end

#getFile(url, filepath) ⇒ Object


78
79
80
81
82
83
84
85
86
87
# File 'lib/blazemeter.rb', line 78

def getFile(url, filepath)
	uri = URI.parse(url)
  https = get_https(uri)
  resp = https.get(uri.request_uri)
  File.open(filepath, "w") do |file|
    file.binmode #windows
    file.write resp.body
    file.close
  end	
end

#getOptionsObject


195
196
197
198
199
200
# File 'lib/blazemeter.rb', line 195

def getOptions()
  path = '/api/rest/blazemeter/getAvailableOptions.json?user_key=' + @user_key
	response = get(path)
  ret = JSON.parse(response.body)
	return ret["options"]
end

#normalizeOptions(options) ⇒ Object


89
90
91
92
93
94
95
96
97
# File 'lib/blazemeter.rb', line 89

def normalizeOptions(options)
  options['options'].each do |index, item|
 case index
      when "LOCATION"
  options["options"]["LOCATION"] = Blazemeter::Common.get_location(item.downcase)
   end
  end
	return options
end

#post(path, options = nil) ⇒ Object


47
48
49
50
51
52
53
54
55
# File 'lib/blazemeter.rb', line 47

def post(path, options=nil)
  uri = URI.parse(@@url+path)
	options = options.to_json
  https = get_https(uri)
  req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
	req.body = options
	response = https.request(req)
	return response
end

#postData(path, filepath) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/blazemeter.rb', line 57

def postData(path, filepath)
  uri = URI.parse(@@url+path)
	postdata = Hash.new
	file = File.open(filepath, "rb")
  f = file.read
	postdata['data'] = f.to_s
  https = get_https(uri)
  req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
	req.body = postdata.to_json
	response = https.request(req)
	return response
end

#testCreate(test_name = nil, options = nil) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/blazemeter.rb', line 99

def testCreate(test_name=nil, options=nil)
 if !test_name
   test_name = "Automatic Ruby Test "+Time.new.inspect
 end
 
 path = '/api/rest/blazemeter/testCreate.json?user_key=' + @user_key + '&test_name=' + URI.escape(test_name) 
 options = normalizeOptions(options)
 response = post(path, options)

 if response.body == ''
   puts "BlazeMeter server not responding"
return nil
 end
 
 ret = JSON.parse(response.body)

 if ret["error"]
   puts "BlazeMeter returned error: "+ret["error"]
 else
   if ret["response_code"] == 200
 puts "BlazeMeter test created with id "+ret["test_id"].to_s
 puts "Access test online at "+@@url+"/node/"+ret["test_id"].to_s
    return ret["test_id"]
else 
   puts "BlazeMeter server response error:"+ret["response_code"].to_s
end
 end   
 
end

#testGetArchive(test_id) ⇒ Object


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/blazemeter.rb', line 214

def testGetArchive(test_id)
  path = '/api/rest/blazemeter/testGetArchive.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
	has_report = false
	response = get(path)
  ret = JSON.parse(response.body)
	if !ret["error"] and ret["response_code"] == 200
    ret["reports"].each_with_index {|val, index| 
if ret["reports"][index]["zip_url"]
  zip_url = ret["reports"][index]["zip_url"]
  filename = File.basename zip_url
  filepath = Dir.home+"/"+ filename
  zip_url = zip_url + '?api_key=' + @user_key #we need api_key to access report
  getFile(zip_url,filepath)
  #todo: check that its downloaded
  puts "Zip report downloaded to "+filepath
  has_report = true
end
 }
  if !has_report
    puts "No reports found for test "+test_id
  end
  else
   puts "Error retrieving archive: " + ret["error"]
  end   
end

#testGetStatus(test_id, detailed = false) ⇒ Object


184
185
186
187
188
189
190
191
192
193
# File 'lib/blazemeter.rb', line 184

def testGetStatus(test_id, detailed = false)
  path = '/api/rest/blazemeter/testGetStatus.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
	if detailed
 path = path + '&detailed=true'
	end
  
	response = get(path)
  ret = JSON.parse(response.body)
	return ret 
end

#testLoad(test_id) ⇒ Object


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/blazemeter.rb', line 202

def testLoad(test_id)
  path = '/api/rest/blazemeter/testUpdate.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
	response = post(path)
  ret = JSON.parse(response.body)
	if !ret["error"] and ret["response_code"] == 200
    return ret["options"]
  else
   puts "Error loading test: " + ret["error"]
return nil
  end   
end

#testScriptUpload(test_id, filepath, filename = nil) ⇒ Object


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/blazemeter.rb', line 240

def testScriptUpload(test_id, filepath, filename = nil)
  path = '/api/rest/blazemeter/testScriptUpload.json?user_key=' + @user_key + '&test_id=' + test_id.to_s
  if (filename) 
path = path + "&file_name=" + URI.escape(filename)
  end
	
  response = postData(path, filepath)
 
 if response.body == ''
   puts "BlazeMeter server not responding"
return nil
 end
 
 ret = JSON.parse(response.body)
 
	if ret["error"]
   puts "BlazeMeter returned error: "+ret["error"]
 else
   if ret["response_code"] == 200
 puts "JMX script uploaded sucessfully to BlazeMeter"
    return true
else 
     puts "JMX script couldn't be uploaded. Error:"+ret["error"].to_s
end
 end
 return false
 
end

#testStart(test_id) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/blazemeter.rb', line 129

def testStart(test_id)
	path = '/api/rest/blazemeter/testStart.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
  response = get(path)
  ret = JSON.parse(response.body)
	if !ret["error"] and ret["response_code"] == 200
puts "BlazeMeter test started"
   return true
  else
#todo: what to do with error? log? throw exception?
 puts "Test not started. Error: "+ret["error"]
   return false
 end   
end

#testStop(test_id) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/blazemeter.rb', line 143

def testStop(test_id)
  path = '/api/rest/blazemeter/testStop.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
	response = get(path)
 
  ret = JSON.parse(response.body)
	if !ret["error"] and ret["response_code"] == 200
puts "BlazeMeter test stopped"
   return true
  else
#todo: what to do with error? log? throw exception?
puts "Test not stopped. Error: "+ret["error"]
   return false
 end   
end

#testUpdate(test_id, options = nil) ⇒ Object


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/blazemeter.rb', line 158

def testUpdate(test_id, options=nil)
  path = '/api/rest/blazemeter/testUpdate.json?user_key=' + @user_key + '&test_id=' + test_id.to_s 
  options = normalizeOptions(options)
	response = post(path, options)
 
 if response.body == ''
   puts "BlazeMeter server not responding"
return nil
 end
 
 ret = JSON.parse(response.body)
 
	if ret["error"]
   puts "BlazeMeter returned error: "+ret["error"]
 else
   if ret["response_code"] == 200
 puts "BlazeMeter test updated sucessfully"
    return true
else 
     puts "BlazeMeter test not updated. Error:"+ret["error"].to_s
end
 end
 return false
 
end