Class: Loadimpact::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/loadimpact/client.rb

Overview

Public: Various methods useful for managing LoadImpact's Platform API. All methods are instance methods and should be called on the Client object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Public: Initialize a Client.

api_key - A String API key.



13
14
15
16
17
# File 'lib/loadimpact/client.rb', line 13

def initialize(api_key)
  @api_key = api_key
  self.class.format(:json)
  self.class.basic_auth(api_key, "")
end

Instance Attribute Details

#api_keyObject

Public: Gets/Sets the String API key of the Client.



8
9
10
# File 'lib/loadimpact/client.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#create_test_config(user_scenario_id, name, url, config) ⇒ Object

Public: Create TestConfig.

user_scenario_id - The Integer id of UserScenario. name - The String name of TestConfig. url - The String target url of Test. config - The Hash options used for configuration:

:users  - The Integer number of users during Test (required).
:duration - The Integer duration (in minutes) of Test (required).

Examples

my_config = { users: 50, duration: 5 }
client.create_test_config(
  12345,
  "My test configuration",
  "http://google.com",
  my_config)
# => #<Loadimpact::TestConfig>

Returns TestConfig object. Raises LoadimpactError if invalid HTTP request.

Raises:



156
157
158
159
160
161
162
# File 'lib/loadimpact/client.rb', line 156

def create_test_config(user_scenario_id, name, url, config)
  payload = test_config_payload(user_scenario_id, name, url, config)
  res = self.class.post("/test-configs", body: payload)
  raise LoadimpactError.new(res["message"]) if res.key?("message")
  hsh = JSON.parse(res.body)
  TestConfig.new(self.class, hsh)
end

#create_test_for(test_config_id) ⇒ Object

Public: Start a Test based on the specified TestConfig.

test_config_id - The Integer id of TestConfig.

Examples

client.create_test_for(12345)
# => #<Loadimpact::Test>

Returns Test object. Raises LoadimpactError if invalid HTTP request. Raises NoConcurrentTestsError if another Test status is “Running”.



176
177
178
179
180
181
182
183
184
185
# File 'lib/loadimpact/client.rb', line 176

def create_test_for(test_config_id)
  res = self.class.post("/test-configs/#{test_config_id}/start")
  if res.code == 500
    raise NoConcurrentTestsError.new
  elsif res.key?("message")
    raise LoadimpactError.new(res["message"])
  end
  hsh = JSON.parse(res.body)
  Test.new(@client, hsh)
end

#create_user_scenario(loadscript, name) ⇒ Object

Public: Create UserScenario.

name - The String name of UserScenario. loadscript - The String Lua load script of the UserScenario.

Examples

client.create_user_scenario(
  "My user scenario",
  "http.request({\"GET\", \"http://google.com\"})")
# => #<Loadimpact::UserScenario>

Returns UserScenario object. Raises LoadimpactError if invalid HTTP request. Raises DuplicateUserScenarioError if UserScenario name already exists.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/loadimpact/client.rb', line 123

def create_user_scenario(loadscript, name)
  payload = user_scenario_payload(loadscript, name)
  res = self.class.post("/user-scenarios", body: payload)
  if res.code == 409
    raise DuplicateUserScenarioError.new
  elsif res.key?("message")
    raise LoadimpactError.new(res["message"])
  end
  hsh = JSON.parse(res.body)
  UserScenario.new(self.class, hsh)
end

#find(options = {}) ⇒ Object

Public: Find specified object.

options - The Hash options used to find the object:

:user_scenario - The Integer id of the desired UserScenario (optional).
:test_config - The Integer id of the desired TestConfig (optional).
:test - The Integer id of the desired Test (optional).

Examples

client.find(user_scenario: 12345)
# => #<Loadimpact::UserScenario>

Returns UserScenario object if :user_scenario specified. Returns TestConfig object if :test_config specified. Returns Test object if :test specified. Returns nil if invalid options specified. Raises LoadimpactError if invalid HTTP request.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/loadimpact/client.rb', line 36

def find(options = {})
  if options[:user_scenario]
    id = options[:user_scenario]
    res = self.class.get("/user-scenarios/#{id}")
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    hsh = JSON.parse(res.body)
    UserScenario.new(self.class, hsh)
  elsif options[:test_config]
    id = options[:test_config]
    res = self.class.get("/test-configs/#{id}")
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    hsh = JSON.parse(res.body)
    TestConfig.new(self.class, hsh)
  elsif options[:test]
    id = options[:test]
    res = self.class.get("/tests/#{id}")
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    hsh = JSON.parse(res.body)
    Test.new(self.class, hsh)
  else
    nil
  end
end

#test_configsObject

Public: Retrieve TestConfigs associated with a user's account.

Examples

client.test_configs
# => [#<Loadimpact::TestConfig>, #<Loadimpact::TestConfig>]

Returns Array of TestConfig objects. Raises LoadimpactError if invalid HTTP request.



85
86
87
88
89
90
# File 'lib/loadimpact/client.rb', line 85

def test_configs
  self.class.get("/test-configs").map do |res|
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    TestConfig.new(self.class, res)
  end
end

#testsObject

Public: Retrieve Tests associated with a user's account.

Examples

client.tests
# => [#<Loadimpact::Test>, #<Loadimpact::Test>]

Returns Array of Test objects. Raises LoadimpactError if invalid HTTP request.



101
102
103
104
105
106
# File 'lib/loadimpact/client.rb', line 101

def tests
  self.class.get("/tests").map do |res|
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    Test.new(self.class, res)
  end
end

#user_scenariosObject

Public: Retrieve UserScenarios associated with a user's account.

Examples

client.user_scenarios
# => [#<Loadimpact::UserScenario>, #<Loadimpact::UserScenario>]

Returns Array of UserScenario objects. Raises LoadimpactError if invalid HTTP request.



69
70
71
72
73
74
# File 'lib/loadimpact/client.rb', line 69

def user_scenarios
  self.class.get("/user-scenarios").map do |res|
    raise LoadimpactError.new(res["message"]) if res.key?("message")
    UserScenario.new(self.class, res)
  end
end