Friday, November 15, 2013

[Jmeter] Java Request extension for Governance/Registry API performance.


First  download the Jmeter and WSO2 Governance Registry from the following download links.

[1]http://jmeter.apache.org/download_jmeter.cgi (version 2.1.0).
[2]http://wso2.com/products/governance-registry/ (version 4.6.0).

1) Now you need to write the your governance/registry API client extending the AbstractJavaSamplerClient provided by Jmeter.

How to add dependency jars to class path:

* Unzip the wso2greg-4.6.0.zip and run the ant inside the bin directory, it will copy the  jars to repository/lib.

You need to copy the following jars list to IDE class path from <GREG_HOME/repository/lib>.

axiom_1.2.11.wso2v4.jar
axis2_1.6.1.wso2v10.jar
commons-codec_1.4.0.wso2v1.jar
commons-httpclient_3.1.0.wso2v2.jar
httpcore_4.3.0.wso2v1.jar
neethi_2.0.4.wso2v4.jar
org.wso2.carbon.authenticator.stub_4.2.0.jar
org.wso2.carbon.base_4.2.0.jar
org.wso2.carbon.core.common_4.2.0.jar
org.wso2.carbon.governance.api_4.2.1.jar
org.wso2.carbon.governance.client-4.2.0.jar
org.wso2.carbon.logging_4.2.0.jar
org.wso2.carbon.registry.admin.api_4.2.0.jar
org.wso2.carbon.registry.api_4.2.0.jar
org.wso2.carbon.registry.common_4.2.1.jar
org.wso2.carbon.registry.core_4.2.0.jar
org.wso2.carbon.registry.indexing.stub-4.2.0.jar
org.wso2.carbon.registry.ws.client_4.2.0.jar
org.wso2.carbon.registry.ws.stub_4.2.0.jar
org.wso2.carbon.user.api_4.2.0.jar
org.wso2.carbon.user.core_4.2.0.jar
org.wso2.carbon.utils_4.2.0.jar
org.wso2.securevault_1.0.0.wso2v2.jar
woden_1.0.0.M8-wso2v1.jar
wsdl4j_1.6.2.wso2v4.jar
XmlSchema_1.4.7.wso2v2.jar

* Unzip the apache-jmeter-2.9.zip and add the ApacheJMeter_java.jar,  ApacheJMeter_core.jar to the class path.

You need to implement 4 methods based on your use case.

i) public Arguments getDefaultParameters() {..}
  - This method can be used to initialize the parameters providing through the Jmeter UI.

ii) public void setupTest(JavaSamplerContext context) {..}
  -This is used to initialize parameters before executing the actual tests.

iii) public SampleResult runTest(JavaSamplerContext javaSamplerContext) {...}
  - This is used to implement the test.

iv) public void teardownTest(JavaSamplerContext context) {..}
  -If there are things to do after the test.

In my sample I’m going to use the governance API to get the endpoint which is stored in Governance Registry.

package org.performance.test460;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;
import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.governance.client.WSRegistrySearchClient;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.pagination.PaginationContext;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient;

public class WSO2Registry460Client extends AbstractJavaSamplerClient {

    private static class DataHolder {
        private String cookie = null;
        private Registry gov = null;
        private WSRegistrySearchClient wsRegistrySearchClient = null;
        private GenericArtifactManager artifactManager = null;

        public String getCookie() {
            return cookie;
        }

        public Registry getGov() {
            return gov;
        }

        public WSRegistrySearchClient getWsRegistrySearchClient() {
            return wsRegistrySearchClient;
        }

        private void setCookie(String cookie) {
            this.cookie = cookie;
        }

        private void setGov(Registry gov) {
            this.gov = gov;
        }

        private void setWsRegistrySearchClient(WSRegistrySearchClient wsRegistrySearchClient) {
            this.wsRegistrySearchClient = wsRegistrySearchClient;
        }

        private GenericArtifactManager getArtifactManager() {
            return artifactManager;
        }

        private void setArtifactManager(GenericArtifactManager artifactManager) {
            this.artifactManager = artifactManager;
        }
    }

    public static ConfigurationContext configContext = null;
    private static String CARBON_HOME = "";
    private static String axis2Repo = "";
    private static String axis2Conf = ServerConfiguration.getInstance()
            .getFirstProperty("Axis2Config.clientAxis2XmlLocation");
    private static String username = "";
    private static String password = "";
    private static String serverURL = "";
    private static String artifact_key = "";
    private static String artifact_name = "";

    private static ThreadLocal dataHolder = new ThreadLocal() {
        protected DataHolder initialValue() {
            return new DataHolder();
        }
    };

    private static void initialize() throws Exception {
        System.setProperty("javax.net.ssl.trustStore", CARBON_HOME
                + File.separator + "repository" + File.separator + "resources"
                + File.separator + "security" + File.separator
                + "wso2carbon.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
        System.setProperty("javax.net.ssl.trustStoreType", "JKS");
        System.setProperty("carbon.repo.write.mode", "true");
        if (configContext != null) {
            configContext = ConfigurationContextFactory
                    .createConfigurationContextFromFileSystem(axis2Repo,
                            axis2Conf);
        }
        DataHolder holder = dataHolder.get();
        holder.setWsRegistrySearchClient(new WSRegistrySearchClient());
        // authenticate user.
        holder.setCookie(holder.getWsRegistrySearchClient().authenticate(configContext, serverURL, username,
                password));
        // initialize registry.
        Registry registry = new WSRegistryServiceClient(serverURL, holder.getCookie());
        // initialize governance registry
        holder.setGov(GovernanceUtils.getGovernanceUserRegistry(registry,
                username));
        // Should be load the governance artifact.
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) holder.getGov());
        PaginationContext.init(0, 1, "", "", Integer.MAX_VALUE);
        holder.getWsRegistrySearchClient().init(holder.getCookie(), serverURL, configContext);
        holder.setArtifactManager(new GenericArtifactManager(holder.getGov(), artifact_key));
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult result = new SampleResult();
        boolean success = true;
        result.sampleStart();
        result.setDataType(SampleResult.TEXT);

        try {
            DataHolder holder = dataHolder.get();
            Map> listMap = new HashMap>();
            // Create the search attribute map
            listMap.put("overview_name", new ArrayList() {
                {
                    add(artifact_name);
                }
            });
            // Find the results.

            GenericArtifact[] genericArtifacts = holder.getArtifactManager().findGenericArtifacts(listMap);

            String productionEp = null;
            for (GenericArtifact artifact : genericArtifacts) {
                String[] endPoint = artifact.getAttributes("endpoints_entry");

                for (String ep : endPoint) {
                    if (ep.startsWith("Prod:")) {
                        productionEp = ep.split("Prod:")[1];
                    }
                }
            }

            if (productionEp != null) {
                result.setResponseMessage("Succesfully picked the production endpoint");
                result.setResponseData(productionEp);
                result.setSuccessful(true);
            }

        } catch (Exception e) {
            result.setResponseMessage("Failed picked the production endpoint");
            result.setSuccessful(false);
            e.printStackTrace();
        } finally {
            PaginationContext.destroy();
        }

        result.setSuccessful(success);
        result.sampleEnd();
        return result;
    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments args = new Arguments();
        args.addArgument("CARBON_HOME", "");
        args.addArgument("serverURL", "");
        args.addArgument("username", "");
        args.addArgument("password", "");
        args.addArgument("artifact_key", "");
        args.addArgument("artifact_name", "");
        return args;
    }

    @Override
    public void setupTest(JavaSamplerContext context) {

        CARBON_HOME = context.getParameter("CARBON_HOME");
        serverURL = context.getParameter("serverURL");
        username = context.getParameter("username");
        password = context.getParameter("password");
        axis2Repo = CARBON_HOME + File.separator + "repository"
                + File.separator + "deployment" + File.separator + "client";
        artifact_key = context.getParameter("artifact_key");
        artifact_name = context.getParameter("artifact_name");

        try {
            initialize();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void teardownTest(JavaSamplerContext context) {

    }
}


2. Build the jar (org.performance.test460.jar) file and drop it to apache-jmeter-2.9/lib/ext.

3. Copy all the jars in <GREG_HOME>/repository/lib to , apache-jmeter-2.9/lib.

4. Start the WSO2 Governance Registry server.

5. Create a service as mentioned in the following image.

6. Start the JMeter.

i) Right click on the “Test Plan” → “Threads” → “Thread Group”.

ii)Right click on the “Thread Group” → “Sampler” → “Java Request”

iii) Select the class name. (org.performance.test460.WSO2Registry460Client)

iv) Fill the input values as shown in the following image.


v) Right click on the “Thread Group” → “Listeners” → “Summary Report”

vi ) Right click on the “Thread Group” → “Listeners” → “View Results Tree”


7. Click on the “Start” button to send a request to validate the test.



8 Load test/results.

Eg : For 100 concurrency , 10 minutes.




For 200 concurrency, 10 minutes,