Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMeter - Execute command over telnet using beanshell

I'm trying to write a jmeter sampler in beanshell to execute a memcached telnet interface command, specifically flush_all. I need this to clear the cache after each test as it causes tests in quick succession to fail.

I have the following code:

import org.apache.commons.net.telnet.TelnetClient;

TelnetClient telnet = new TelnetClient();
telnet.connect( "memcachedServer.dev", 11211 );

//InputStream in = telnet.getInputStream(); 
PrintStream out = new PrintStream( telnet.getOutputStream() );

out.println("flush_all\r");
out.println("quit\r");

telnet.disconnect();

It seems to execute with no problems but the cache is not cleared. I have tried the code with and without the "\r" but neither way works.

Anyone know what's wrong?

Thanks, Adrian

like image 918
Adrian Avatar asked Mar 12 '26 13:03

Adrian


1 Answers

Any reason for not using TCP Sampler?

  1. Add TCP Sampler to your test plan (where you need to flush caches)
  2. Configure host and port
  3. Put the following lines into "Text to send" area:

    flush_all${CR}${LF}
    quit${CR}${LF}
    
  4. Add Beanshell PreProcessor as a child of the TCP Sampler
  5. Put the following code into the PreProcessor's "Script" area:

    vars.put("LF",URLDecoder.decode("%0D", "ASCII"));
    vars.put("CR",URLDecoder.decode("%0A", "ASCII")); 
    

See How To Send Control Characters Using The JMeter TCP Sampler? guide for more detailed information.

like image 144
Dmitri T Avatar answered Mar 15 '26 07:03

Dmitri T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!