class BlazemeterApi

Public Class Methods

new(user_key) click to toggle source

@@url = 'vitali.a.blazemeter.com' @@url = 'localhost/ablazemetercom'

# File lib/blazemeter.rb, line 36
def initialize(user_key)
  @user_key = user_key
end

Public Instance Methods

get(path) click to toggle source
# 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
getFile(url, filepath) click to toggle source
# 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
getOptions() click to toggle source
# 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
get_https(uri) click to toggle source
# 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
normalizeOptions(options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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