How to use JedisPool in Jmeter

derzeit Source

I want to loadtest redis with jmeter. I already have a working JSR223Sampler with a groovy-script

import redis.clients.jedis.Jedis;
import java.util.concurrent.ThreadLocalRandom;

String varuser = "user:" + ThreadLocalRandom.current().nextInt(1, 500);

Jedis jedis = new Jedis(IP, port);
String result = jedis.hgetAll(varuser);
SampleResult.setResponseData(result.toString().getBytes());

under a ThreadGroup with 10 'users'. Works fine. Now I want to raise the number of 'users' to 1000. Performance will be influenced negatively by the high number of connections, right? So I want to get the advantages of a connection pool. According to

https://github.com/xetorthio/jedis/wiki/Getting-started

JedisPool is the answer. My question is how my jmeter testplan have to look like? Where do I have to put the jmeter-element (which one?) in which JedisPool will be created? Not under my ThreadGroup, right? And how I can use the connections from the pool in my sampler-script from above?

EDIT

So with Dmitris advise I createt two different testplans. Both not working. Maybe somebody sees my mistake?

First plan:

TestPlan

|---setUp Thread Group

----|---JSR223 Sampler (1)

|---Thread Group

----|---JSR223 Sampler (2)

|---View Results Tree

with JSR223 Sampler (1)

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

JedisPool pool = new JedisPool(new JedisPoolConfig(), IP);
prop.put('pool', pool);

and JSR223 Sampler (2)

import redis.clients.jedis.Jedis;
import java.util.concurrent.ThreadLocalRandom;

String varuser = "user:" + ThreadLocalRandom.current().nextInt(1, 500);

Jedis jedis = props.get('pool');
String result = jedis.hgetAll(varuser);
SampleResult. ...
jedis.close();

My second TestPlan looks like

TestPlan

|---ThreadGroup

----|---Test Action

--------|---JSR223 PreProcessor

----|---JSR223 Sampler

----|---View Results Tree

with PreProcessor like the above JSR223 Sampler (1) and JSR223 Sampler like the above JSR223 Sampler (2).

First Plan comes with the response message

cannot cast object '[email protected]' with class
'redis.clients.jedis.JedisPool' to class 'redis.clients.jedis.Jedis'

Second Plan responds

no such property: pool for class: Script41082

What I'm doing wrong?

groovyredisjmeterjedis

Answers

answered 9 months ago Dmitri T #1

I would recommend creating JedisPool somewhere in setUp Thread Group using either JSR223 Sampler (if you want to measure pool creation time) or Test Action Sampler and JSR223 PreProcessor combination (if you don't want to include pool creation request into your test results)

Once done you can put this "pool" into JMeter Properties like:

  1. In setUp Thread Group

    def pool = new JedisPool(new JedisPoolConfig(), 'localhost')
    props.put('pool', pool)
    
  2. Somewhere in the middle of your test

    def pool = props.get('pool')
    

See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests

answered 9 months ago user7294900 #2

You are getting JedisPool from properties and try to cast it to Jedis which is impossible, you need to get Jedis from JedisPool.getResource():

import redis.clients.jedis.JedisPool;
....
JedisPool pool = props.get('pool');

Jedis jedis = pool.getResource();

comments powered by Disqus