Stress Test Using JMeter in Ruby - Part 2

JMeter

Due to update-to-date JMeter 3.0 require Java 7, but for current offical Java support in Mac OSX is 6. So, before running new JMeter you might need to upgrade your Java. Please check my gist for installation Java 7.

This article just show you code example of how to use loop number and thread number in gem 'ruby-jmeter'.

Currently, we need to do stress test on your API service.

That’s see how to write codes in different scenarios.

Reference: http://jmeter.apache.org/usermanual/functions.html

[Scenario 1] You want to change request data based on thread number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
test do
  defaults domain: 'YOUR.API.ENDPOINT', portocal: 'https' , port: '443'
  
  header [
    { name: 'Authorization', value: "Bearer 123456789" },
    { name: 'Content-Type', value: 'application/json' }
  ]

  post_body = {
    "order_number": "thread-${__threadNum}"
  }
 
  threads count: 1000, rampup: 1 ,loops: 10, scheduler: false do
      transaction "Shipments" do
        post name: 'Create',
             url: 'https://YOUR.API.ENDPOINT/v1/orders',
             raw_body: post_body.to_json do
               with_xhr
        end
      end
    view_results_tree
  end

  view_results_in_table
  graph_results
  aggregate_graph
  view_results_tree
  summary_report
end.run(
  gui: true,
  file: 'jmeter-order-api.jmx',
  log:  'jmeter-order-api.log',
  jtl:  'results-order-api.jtl'
)

[Scenario 2] You want to change request data based on thread number and iteration number

1
2
3
4
5
6
7
8
test do
  ...
  
  post_body = {
    "order_number": "thread-${__threadNum}-loop-${__BeanShell(vars.getIteration();,)}"
  }
  
  ...

[Scenario 3] You already have data array and you want to use the array element as request data and use iteration number as index to get data

use variables to set variable array

variables accept [{name: XXX, value: XXX}, {name: XXX, value: XXX}...]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'pg'

test do
  ...
  
  conn = PG.connect(dbname:   'DATABASE_STAGING_DBNAME',
                    host:     'DATABASE_STAGING_HOST',
                    user:     'DATABASE_STAGING_USER',
                    password: 'DATABASE_STAGING_PWD'
                   )
  orders = []
  conn.exec( "SELECT * FROM orders where order_state = 'ready'" ) do |result|
    result.each do |row|
      orders << row
    end
  end

  order_index = 0
  post_body_array = orders.inject([]) do |arr, order|
    arr << {
      name: order_index.to_s,
      value: {
        orders: [
          {
            id: order['id'],
            order_state: 'shipped'
          }
        ]
      }.to_json
    }
    order_index += 1
    arr
  end

  variables post_body_array

  threads count: 1, rampup: 1, loops: order_index do
    transaction "Update Order" do
        post name: 'Update',
             url: 'https://YOUR.API.ENDPOINT/v1/orders/update_url',
             raw_body: '${__BeanShell(vars.get("${__BeanShell(vars.getIteration();,)}"))}' do
               with_xhr
             end
    end
    view_results_tree
    debug_sampler
  end
  
  ...

[Scenario 4] You already have data array and you want to use the array element as request data and use thread number as index to get data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...

  threads count: order_index, rampup: 1 ,loops: 1 do
    transaction "Update Order" do
        post name: 'Update',
             url: 'https://YOUR.API.ENDPOINT/v1/orders/update_url',
             raw_body: '${__BeanShell(vars.get("${__threadNum}"))}' do
               with_xhr
             end
    end
    view_results_tree
    debug_sampler
  end
  
  ...
Comments

Comments

Google Analytics Alternative