Class: RubyJmeter::ExtendedDSL

Inherits:
DSL
  • Object
show all
Includes:
Parser
Defined in:
lib/ruby-jmeter/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#fill_in, #parse_http_request, #parse_test_type, #parse_uri, #parse_url, #raw_body

Methods inherited from DSL

#access_log_sampler, #aggregate_graph, #aggregate_report, #ajp13_sampler, #assertion_results, #beanshell_assertion, #beanshell_listener, #beanshell_postprocessor, #beanshell_preprocessor, #beanshell_sampler, #beanshell_timer, #bsf_assertion, #bsf_listener, #bsf_postprocessor, #bsf_preprocessor, #bsf_sampler, #bsf_timer, #compare_assertion, #comparison_assertion_visualizer, #constant_throughput_timer, #constant_timer, #counter, #cssjquery_extractor, #csv_data_set_config, #debug_postprocessor, #debug_sampler, #distribution_graphalpha, #duration_assertion, #foreach_controller, #ftp_request, #ftp_request_defaults, #gaussian_random_timer, #generate_summary_results, #graph_results, #html_assertion, #html_link_parser, #html_parameter_mask, #http_authorization_manager, #http_request, #http_request_defaults, #http_url_rewriting_modifier, #if_controller, #include_controller, #java_request, #java_request_defaults, #jdbc_connection_configuration, #jdbc_postprocessor, #jdbc_preprocessor, #jdbc_request, #jms_pointtopoint, #jms_publisher, #jms_subscriber, #jsr223_assertion, #jsr223_listener, #jsr223_postprocessor, #jsr223_preprocessor, #jsr223_sampler, #jsr223_timer, #junit_request, #keystore_configuration, #ldap_extended_request, #ldap_extended_request_defaults, #ldap_request, #ldap_request_defaults, #login_config_element, #mail_reader_sampler, #mailer_visualizer, #md5hex_assertion, #module_controller, #monitor_results, #once_only_controller, #os_process_sampler, #poisson_random_timer, #random_controller, #random_order_controller, #random_variable, #recording_controller, #regex_user_parameters, #regular_expression_extractor, #response_time_graph, #result_status_action_handler, #runtime_controller, #save_responses_to_a_file, #simple_config_element, #simple_controller, #simple_data_writer, #smime_assertion, #smtp_sampler, #spline_visualizer, #summary_report, #switch_controller, #synchronizing_timer, #tcp_sampler, #tcp_sampler_config, #test_action, #test_plan, #uniform_random_timer, #user_parameters, #view_results_in_table, #view_results_tree, #while_controller, #xml_assertion, #xml_schema_assertion, #xpath_assertion, #xpath_extractor

Constructor Details

#initialize(params = {}) ⇒ ExtendedDSL

Returns a new instance of ExtendedDSL.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-jmeter/dsl.rb', line 6

def initialize(params = {})
  @root = Nokogiri::XML(<<-EOF.strip_heredoc)
    <?xml version="1.0" encoding="UTF-8"?>
    <jmeterTestPlan version="1.2" properties="2.4" jmeter="2.9 r1437961">
    <hashTree>
    </hashTree>
    </jmeterTestPlan>
  EOF
  node = RubyJmeter::TestPlan.new(params)

  @current_node = @root.at_xpath("//jmeterTestPlan/hashTree")
  @current_node = attach_to_last(node)
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



4
5
6
# File 'lib/ruby-jmeter/dsl.rb', line 4

def root
  @root
end

Instance Method Details

#console_status_logger(name = "jp@gc - Console Status Logger", params = {}, &block) ⇒ Object Also known as: console



291
292
293
294
# File 'lib/ruby-jmeter/dsl.rb', line 291

def console_status_logger(name="jp@gc - Console Status Logger", params={}, &block)
  node = RubyJmeter::GCConsoleStatusLogger.new(name, params)
  attach_node(node, &block)
end

#delete(*args, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/ruby-jmeter/dsl.rb', line 107

def delete(*args, &block)
  params = args.shift || {}
  params = { url: params }.merge(args.shift || {}) if params.class == String
  params[:method] ||= 'DELETE'
  params[:name] ||= params[:url]
  parse_http_request(params)
  node = RubyJmeter::HttpRequest.new(params)
  attach_node(node, &block)
end

#dummy_sampler(name = "jp@gc - Dummy Sampler", params = {}, &block) ⇒ Object Also known as: dummy



305
306
307
308
# File 'lib/ruby-jmeter/dsl.rb', line 305

def dummy_sampler(name="jp@gc - Dummy Sampler", params={}, &block)
  node = RubyJmeter::GCDummySampler.new(name, params)
  attach_node(node, &block)
end

#exists(variable, &block) ⇒ Object



163
164
165
166
167
168
# File 'lib/ruby-jmeter/dsl.rb', line 163

def exists(variable, &block)
  params ||= {}
  params[:condition] = "'${#{variable}}'.length > 0"
  node = RubyJmeter::IfController.new(params)
  attach_node(node, &block)
end

#extract(params, &block) ⇒ Object Also known as: web_reg_save_param



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruby-jmeter/dsl.rb', line 219

def extract(params, &block)
  node = if params[:regex]
    params[:refname] = params[:name]
    params[:regex] = params[:regex] #CGI.escapeHTML 
    params[:template] = params[:template] || "$1$"
    RubyJmeter::RegularExpressionExtractor.new(params)
  else
    params[:refname] = params[:name]
    params[:xpathQuery] = params[:xpath]
    RubyJmeter::XpathExtractor.new(params)
  end
  attach_node(node, &block)
end

#flood(token, params = {}) ⇒ Object Also known as: grid



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/ruby-jmeter/dsl.rb', line 350

def flood(token, params={})
  if params[:region] == 'local'
    logger.info "Starting test ..."
    params[:started] = Time.now
    run params
    params[:stopped] = Time.now
    logger.info "Completed test ..."
    logger.debug "Uploading results ..." if params[:debug]
  end
  RestClient.proxy = params[:proxy] if params[:proxy]
  begin
    file = Tempfile.new(['jmeter', '.jmx'])
    file.write(doc.to_xml(:indent => 2))
    file.rewind

    response = RestClient.post "#{params[:endpoint] ? params[:endpoint] : 'https://api.flood.io'}/floods?auth_token=#{token}",
    {
      :flood => {
        :tool => 'jmeter',
        :url => params[:url],
        :plan => File.new("#{file.path}", 'rb'),
        :plan_cache => '',
        :name => params[:name],
        :notes => params[:notes],
        :tag_list => params[:tag_list],
        :threads => params[:threads],
        :rampup => params[:ramup],
        :duration => params[:duration],
        # specials for API
        :started => params[:started],
        :stopped => params[:stopped]
      },
      :results => (File.new("#{params[:jtl] ? params[:jtl] : 'jmeter.jtl'}", 'rb') if params[:region] == 'local'),
      :region => params[:region],
      :multipart => true,
      :content_type => 'application/octet-stream'          
    }.merge(params)
    if response.code == 200
      logger.info "Flood results at: #{JSON.parse(response)["response"]["results"]["link"]}"
    else
      logger.fatal "Sorry there was an error: #{JSON.parse(response)["error_description"]}"
    end
  rescue => e
    logger.fatal "Sorry there was a fatal error: #{e.message}"
  end
end

#get(*args, &block) ⇒ Object Also known as: visit

HTTP Samplers



83
84
85
86
87
88
89
90
91
# File 'lib/ruby-jmeter/dsl.rb', line 83

def get(*args, &block)
  params = args.shift || {}
  params = { url: params }.merge(args.shift || {}) if params.class == String
  params[:method] ||= 'GET'
  params[:name] ||= params[:url]
  parse_http_request(params)
  node = RubyJmeter::HttpRequest.new(params)
  attach_node(node, &block)
end

#http_cache_manager(params = {}, &block) ⇒ Object Also known as: cache



41
42
43
44
# File 'lib/ruby-jmeter/dsl.rb', line 41

def http_cache_manager(params={}, &block)
  params[:clearEachIteration] = true if params.keys.include? 'clear_each_iteration'
  super
end


34
35
36
37
# File 'lib/ruby-jmeter/dsl.rb', line 34

def http_cookie_manager(params={}, &block)
  params[:clearEachIteration] = true if params.keys.include? 'clear_each_iteration'
  super
end

#http_header_manager(params, &block) ⇒ Object Also known as: header



53
54
55
56
57
58
# File 'lib/ruby-jmeter/dsl.rb', line 53

def http_header_manager(params, &block)
  if params.is_a?(Hash) 
    params['Header.name'] = params[:name]
  end
  super
end

#jmx(params = {}) ⇒ Object



318
319
320
321
# File 'lib/ruby-jmeter/dsl.rb', line 318

def jmx(params={})
  file(params)
  logger.info "Test plan saved to: #{params[:file]}"
end

#latencies_over_time(name = "jp@gc - Response Latencies Over Time", params = {}, &block) ⇒ Object



286
287
288
289
# File 'lib/ruby-jmeter/dsl.rb', line 286

def latencies_over_time(name="jp@gc - Response Latencies Over Time", params={}, &block)
  node = RubyJmeter::GCLatenciesOverTime.new(name, params)
  attach_node(node, &block)
end

#loop_controller(params, &block) ⇒ Object Also known as: Loop



172
173
174
175
# File 'lib/ruby-jmeter/dsl.rb', line 172

def loop_controller(params, &block)
  params[:loops] = params[:count] || 1
  super
end

#out(params = {}) ⇒ Object

API Methods



314
315
316
# File 'lib/ruby-jmeter/dsl.rb', line 314

def out(params={})
  puts doc.to_xml(:indent => 2)
end

#post(*args, &block) ⇒ Object Also known as: submit



95
96
97
98
99
100
101
102
103
# File 'lib/ruby-jmeter/dsl.rb', line 95

def post(*args, &block)
  params = args.shift || {}
  params = { url: params }.merge(args.shift || {}) if params.class == String
  params[:method] ||= 'POST'
  params[:name] ||= params[:url]
  parse_http_request(params)
  node = RubyJmeter::HttpRequest.new(params)
  attach_node(node, &block)
end

#put(*args, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/ruby-jmeter/dsl.rb', line 117

def put(*args, &block)
  params = args.shift || {}
  params = { url: params }.merge(args.shift || {}) if params.class == String
  params[:method] ||= 'PUT'
  params[:name] ||= params[:url]
  parse_http_request(params)
  node = RubyJmeter::HttpRequest.new(params)
  attach_node(node, &block)
end

#random_timer(delay = 0, range = 0, &block) ⇒ Object Also known as: think_time



235
236
237
238
239
240
241
# File 'lib/ruby-jmeter/dsl.rb', line 235

def random_timer(delay=0, range=0, &block)
  params={}
  params[:delay] = delay
  params[:range] = range
  node = RubyJmeter::GaussianRandomTimer.new(params)
  attach_node(node, &block)
end

#response_assertion(params = {}, &block) ⇒ Object Also known as: assert, web_reg_find



245
246
247
248
249
250
251
252
# File 'lib/ruby-jmeter/dsl.rb', line 245

def response_assertion(params={}, &block)
  params[:test_type] = parse_test_type(params)
  params[:match] = params.values.first
  node = RubyJmeter::ResponseAssertion.new(params)
  node.doc.xpath("//stringProp[@name='Assertion.scope']").remove if
    params[:scope] == 'main' || params['scope'] == 'main'
  attach_node(node, &block)
end

#response_codes_per_second(name = "jp@gc - Response Codes per Second", params = {}, &block) ⇒ Object

JMeter Plugins



261
262
263
264
# File 'lib/ruby-jmeter/dsl.rb', line 261

def response_codes_per_second(name="jp@gc - Response Codes per Second", params={}, &block)
  node = RubyJmeter::GCResponseCodesPerSecond.new(name, params)
  attach_node(node, &block)
end

#response_times_distribution(name = "jp@gc - Response Times Distribution", params = {}, &block) ⇒ Object



266
267
268
269
# File 'lib/ruby-jmeter/dsl.rb', line 266

def response_times_distribution(name="jp@gc - Response Times Distribution", params={}, &block)
  node = RubyJmeter::GCResponseTimesDistribution.new(name, params)
  attach_node(node, &block)
end

#response_times_over_time(name = "jp@gc - Response Times Over Time", params = {}, &block) ⇒ Object



271
272
273
274
# File 'lib/ruby-jmeter/dsl.rb', line 271

def response_times_over_time(name="jp@gc - Response Times Over Time", params={}, &block)
  node = RubyJmeter::GCResponseTimesOverTime.new(name, params)
  attach_node(node, &block)
end

#response_times_percentiles(name = "jp@gc - Response Times Percentiles", params = {}, &block) ⇒ Object



276
277
278
279
# File 'lib/ruby-jmeter/dsl.rb', line 276

def response_times_percentiles(name="jp@gc - Response Times Percentiles", params={}, &block)
  node = RubyJmeter::GCResponseTimesPercentiles.new(name, params)
  attach_node(node, &block)
end

#run(params = {}) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/ruby-jmeter/dsl.rb', line 331

def run(params={})
  file(params)
  logger.warn "Test executing locally ..."
  properties = params[:properties] || "#{File.dirname(__FILE__)}/helpers/jmeter.properties"
  cmd = "#{params[:path]}jmeter #{"-n" unless params[:gui] } -t #{params[:file]} -j #{params[:log] ? params[:log] : 'jmeter.log' } -l #{params[:jtl] ? params[:jtl] : 'jmeter.jtl' } -q #{properties}"
  logger.debug cmd if params[:debug]
  Open3.popen2e("#{cmd}") do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      logger.debug line.chomp if params[:debug]
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      abort "FAILED !!! #{cmd}"
    end
  end
  logger.info "Local Results at: #{params[:jtl] ? params[:jtl] : 'jmeter.jtl'}"
end

#soapxmlrpc_request(params, &block) ⇒ Object Also known as: soap

Other Samplers



135
136
137
138
# File 'lib/ruby-jmeter/dsl.rb', line 135

def soapxmlrpc_request(params, &block)
  params[:method] ||= 'POST'
  super
end

#thread_group(*args, &block) ⇒ Object Also known as: threads



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby-jmeter/dsl.rb', line 64

def thread_group(*args, &block)
  params = args.shift || {}
  params = { count: params }.merge(args.shift || {}) if params.class == Fixnum
  params[:num_threads]        = params[:count] || 1
  params[:ramp_time]          = params[:rampup] || (params[:num_threads]/2.0).ceil
  params[:start_time]         = params[:start_time] || Time.now.to_i * 1000
  params[:end_time]           = params[:end_time] || Time.now.to_i * 1000
  params[:duration]         ||= 60
  params[:continue_forever] ||= false
  params[:loops]              = -1 if params[:continue_forever]
  node = RubyJmeter::ThreadGroup.new(params)
  attach_node(node, &block)
end

#throughput_controller(params, &block) ⇒ Object Also known as: Throughput



179
180
181
182
183
184
185
186
187
# File 'lib/ruby-jmeter/dsl.rb', line 179

def throughput_controller(params, &block)
  params[:style] = 1 if params[:percent]
  params[:maxThroughput] = params[:total] || params[:percent] || 1

  node = RubyJmeter::ThroughputController.new(params)
  node.doc.xpath(".//FloatProperty/value").first.content = params[:maxThroughput].to_f
 
  attach_node(node, &block)
end

#throughput_shaper(name = "jp@gc - Throughput Shaping Timer", steps = [], params = {}, &block) ⇒ Object Also known as: shaper



298
299
300
301
# File 'lib/ruby-jmeter/dsl.rb', line 298

def throughput_shaper(name="jp@gc - Throughput Shaping Timer", steps=[], params={}, &block)
  node = RubyJmeter::GCThroughputShapingTimer.new(name, steps)
  attach_node(node, &block)
end

#to_docObject



327
328
329
# File 'lib/ruby-jmeter/dsl.rb', line 327

def to_doc
  doc.clone
end

#to_xmlObject



323
324
325
# File 'lib/ruby-jmeter/dsl.rb', line 323

def to_xml
  doc.to_xml(:indent => 2)
end

#transaction_controller(*args, &block) ⇒ Object Also known as: transaction

Controllers



152
153
154
155
156
157
158
159
# File 'lib/ruby-jmeter/dsl.rb', line 152

def transaction_controller(*args, &block)
  params = args.shift || {}
  params = { name: params }.merge(args.shift || {}) if params.class == String
  params[:parent] ||= true
  params[:includeTimers] = params[:include_timers] || false
  node = RubyJmeter::TransactionController.new(params)
  attach_node(node, &block)
end

#transactions_per_second(name = "jp@gc - Transactions per Second", params = {}, &block) ⇒ Object



281
282
283
284
# File 'lib/ruby-jmeter/dsl.rb', line 281

def transactions_per_second(name="jp@gc - Transactions per Second", params={}, &block)
  node = RubyJmeter::GCTransactionsPerSecond.new(name, params)
  attach_node(node, &block)
end

#user_defined_variables(params, &block) ⇒ Object Also known as: variables

Config Elements



23
24
25
26
27
28
# File 'lib/ruby-jmeter/dsl.rb', line 23

def user_defined_variables(params, &block)
  if params.is_a?(Hash) 
    params['Argument.name'] = params[:name]
  end
  super
end

#with_user_agent(device) ⇒ Object



48
49
50
51
# File 'lib/ruby-jmeter/dsl.rb', line 48

def with_user_agent(device)
  http_header_manager name: 'User-Agent', 
                      value: RubyJmeter::UserAgent.new(device).string
end

#with_xhrObject



127
128
129
130
# File 'lib/ruby-jmeter/dsl.rb', line 127

def with_xhr
  http_header_manager name: 'X-Requested-With', 
                      value: 'XMLHttpRequest'
end