Skip to content

How to create UDF

Shwet Shashank edited this page May 30, 2013 · 11 revisions

User defined classes are the basic building blocks of a run (test flow). These are functionalities that a user writes and add to the loader-server which are further used while defining a group for a run. Simplest example would be a functionality to make an HTTP call to a web-server.

1.1 Writing First UDF

In this example we will write a UDF which will give us a functionality to make HTTP Get call to a web-server. Later we will use this function to create a run and run it using loader.

Step 1: Prerequisites

Create a Maven Java project (we can use any build tool, here we will use maven) and add following repositories and dependencies in pom.xml.

 <dependency>
       <groupId>com.open.perf</groupId>
       <artifactId>loader-core</artifactId>
       <version>2.0.1</version>
 </dependency>

<repositories>
    <repository>
         <snapshots />
         <id>snapshots</id>
         <name>libs-snapshot</name> 
         <url>http://artifactory.nm.flipkart.com:8081/artifactory/libs-snapshot</url>
    </repository>                             
 </repositories>

For packaging we can use the maven build pluggin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <excludeScope>provided</excludeScope>
            <descriptorRefs>
                <descriptorRef>jar-with-      dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>assembly</goal>
                </goals>
            </execution>
        </executions>
    </plugin
</plugins>
</build>

Step 2: Write the class

For the HTTP calls we can use any HTTP client, here we will use ning client. Lets add dependency for that too.

<dependency>
    <groupId>com.ning</groupId>
    <artifactId>async-http-client</artifactId>
    <version>1.7.13</version>
</dependency>

By design every functionality will be a java class which needs to extend com.open.perf.function.PerformanceFunction and override execute method of the base class. The real function of the class goes in this method.

package com.open.test;
import com.open.perf.core.FunctionContext;
import com.open.perf.function.PerformanceFunction;

public class TestHttpGet extends PerformanceFunction {

	@Override
	public void execute(FunctionContext context) throws Exception {
		// Actual functionality goes here, lets implement it.

	}

}

Lets implement.

package com.open.test;

import java.util.concurrent.Future;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import com.open.perf.core.FunctionContext;
import com.open.perf.function.PerformanceFunction;

public class TestHttpGet extends PerformanceFunction {

	@Override
	public void execute(FunctionContext context) throws Exception {
		String url="http://127.0.0.1/index.html";
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
	    Future<Response> f = asyncHttpClient.prepareGet(url).execute();
	    Response r = f.get();
	}

}

Done. Now lets create a package and upload it on loader server and enable it to make HTTPGet calls. Run following command in your project directory

mvn clean compile package

It will clean the project, compile and create a jar. In target directory make sure you have two jar files, out of the two one will have “-jar-with-all-dependencies” as ending. If its not there something failed, you need to fix it.

Step3: Upload on Loader Server

Start the loader-server and visit http://:9999/home.html. You will see this page: Alt Image