3

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

Adrian
  • 143
  • 1
  • 4
  • 13

2 Answers2

1

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

scripting / emulateing a terminal can be problematic. Your code is executing, and printing the command "flush_all" but it seems like the enter/newline command is not being processed.

There are 2 options that might work for you

Here is an article on how to send the carriage return over telnet.

How to send carriage return over telnet?

Or, you may have more luck with the SSH jmeter plugin, which I think is built in to the extra libs, but if not i can send you a link. this will allow you to log in, and execute a command in a given dir, its simple and pretty clean

Community
  • 1
  • 1
Dan
  • 421
  • 5
  • 12
  • Please do cite the relevant parts of that link, in case the link breaks – Johannes Jander Feb 10 '16 at 21:19
  • Sorry, good point Johannes, enter-key in telnet will send either \r or \r\n or set how telnet sends the enter key by switching it to by using the telnet command "toggle crlf" before you make your connection – Dan Feb 10 '16 at 21:48