How to use ruby-jmeter

Tomoharu Tsutsumi
2 min readNov 20, 2022

--

You can do load testing easily with ruby jmeter. There are two types in jmeter, which are CUI and GUI. I chose CUI because it is really monotonous to push buttons on screen for load testing and it is possible to manage test environment with codes by using ruby-jmeter.

Install

# gemfile
gem 'ruby-jmeter'

# and in terminal

bundle

How to write test

  test do
threads count: 1, loops: 1, rampup: 10 do
variables [
{ name: 'next_year', value: "#{Time.current.next_year.year}" },
{ name: 'today', value: "#{Date.today.strftime('%Y/%m/%d')}" },
{ name: 'next_year_day', value: "#{Time.current.next_year.year}/11/01 16:45" },
]
cookies
visit name: 'go to sign in page', url: 'path/to/your/page' do
extract name: 'csrf-token', xpath: "//meta[@name='csrf-token']/@content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[@name='csrf-param']/@content", tolerant: true
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'sign in', url: 'path/to/your/page', fill_in: {
'${csrf-param}' => '${csrf-token}',
'user[email]' => '${__threadNum}' + '@gmail.com',
'user[password]' => 'password',
}
end
end.run(
jtl: 'jmeter_results.jtl',
properties: 'jmeter.properties'
)

Test codes have to be surrounded test do ~ end. It is possible to define how many users execute the tests by count option, how many times one user executes the test with loops and within how long threads are created with rampup. Ruby on Rails uses a hash in order to protect our services from CSRF, so you have to add http_header_manager.

In variables, you can define some variables. If your software has a log in function, you should cookies because users can keep their sessions by using it. Moreover, we can decide the page we want users to go to with visit. Submit enables us to send data to a server, in other words users in threads can post data.

Test results

In the tests above, results are recorded in jmeter-results.jtl file. You will find some data in the file like this.

timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1668769564436,1278,go to sign in page,200,OK,ThreadGroup 1-1,text,true,,15910,154,1,1,http://localhost:3000/users/sign_in,1263,0,46
1668769566916,1106,sign in,200,OK,ThreadGroup 1-1,text,true,,15951,586,1,1,http://localhost:3000/users/sign_in,1104,0,0

There is much information about your software. We have to focus on “Latency” among them. “Latency” is the time your software takes to respond to the actions of users. In this case, my software’s latency is 1263 milliseconds and 1104 milliseconds.

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)