Thursday, June 23, 2016

JMeter - how to call external groovy script

Recently was problem to call external script from JMeter. It's really useful to have shared library between different scrips in JMeter. Recently I have to create separate maven project producing jar which should be placed to JMeter. It was quite time expensive and difficult to maintain and fragile. Call external script from BeanShell script is possible but it's not easy.

Luckily in JMeter 3.0 is functionality JSR223. Now it's easy task.

  • Create configuration element User defined variable and create property 'scriptHome'.
    ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer   .getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
    
    From now in JMeter is available variable scriptHome. Variable point to directory where is places .jmx file.
  • In directory where is placed .jmx test file create file 'helloWorld.groovy' with content:
    public class Test{
        def sayHello(){
             println "Hello world"
        }
    }
    
  • Finally create JSR223 Sampler. As script language choose Groovy. Paste following script:
    def scriptHome = vars.get("scriptHome");
    def gcl = new GroovyClassLoader()
    def clazz2 = gcl.parseClass(new File(scriptHome + "helloWorld.groovy"))
    def o = clazz2.newInstance()
    o.sayHello()
    
Now run test, on console will be output 'Hello world'.

Asynchronous thread with timeout

Recently I have problem to start asynchronous process with timeout. More precisely I want main process to start new thread with some computation task. If task execution exceed some given time period than task is killed. I know how to do that in old 1.4 Java with thread instance for task in some thread group and some watchdog thread.

After some time I wrote following code:

@Test
public void scheduler() throws Exception {
 final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);

 FutureTask task = new FutureTask(() -> {
  try {
   System.out.println("task started");
   TimeUnit.SECONDS.sleep(10);
   System.out.println("task je dokoncen");
  } catch (InterruptedException e) {
   System.out.println("interrupted");
   return "task is done with interruption";
  }
  return "task is done";
 });

 scheduler.schedule(task, 100, TimeUnit.MILLISECONDS);

 scheduler.schedule(() -> {
  System.out.println("kill job is started");
  task.cancel(true);
  System.out.println("kill job is done");
  return "";
 } , 2, TimeUnit.SECONDS);

 System.out.println("I'm going to wait for ending both tasks");

 TimeUnit.SECONDS.sleep(20);
}

Friday, June 17, 2016

How to download latest snapshot from maven repository

Previously I had problem to download latest snapshot build from maven repository. It's not about copying file from maven repository home ~/.m2/repository/. It's just part of process. First it's necessary to download latest build from maven repository. Easiest way is to push maven to do it. For example let's have package com.example:billing:1.0.0-SNAPSHOT:war. Command will look like:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get --batch-mode -Dmdep.useBaseVersion=true -Dartifact=com.example:billing:1.0.0-SNAPSHOT:war -DremoteRepositories=example-snapshots::default::http://companynexus.com/nexus/content/repositories/snapshots/

Detail description can by found at http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html.

Copy snapshot artifact from local repository can by done like this:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dmdep.useBaseVersion=true -DoutputDirectory=/opt/billing/ -Dartifact=com.example:billing:1.0.0-SNAPSHOT:war
Description can be found at http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html.