Jmeter Script to test performance on our aws hosted application

USE CASE: We send request to Apigee api which calls our lambda function and do some work. Apigiee accepts zipped encoded request. We need to send our access token with our payload.

Here is how i wrote this to accomplish this.

  1. First open Jmeter → create a test plan under test plan → add thread group
  2. Under thread group → Create a http request sampler called authentication
  3. Add your end point path in my case it post request..
  4. Add a body content that is needed, i my case i add credential

5. Add a header manager config element. and add content type of application/json

6. Also create a Regular Expression Extractor.

7. Add a New sampler (HTTP REQUEST) → add header manager config element add your authorization token.

Please note if you choose to write a beanshell code for this here is a sample that i had initially done.

Beanshell pre-processor instead of adding http header manager….but recommend above.. Just wanted to show you option if you want to write code.

import org.apache.jmeter.protocol.http.control.Header;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileUtils;
import java.nio.charset.StandardCharsets;
import java.io.*;sampler.getHeaderManager().add(new Header(“Authorization”,”Bearer “ + vars.get(“BEARER”)));

Now lets submit our payload

{
“id”:”${randomid}”,
“pmmVersion”:”2.0",
“policyData” : “${postDataEncoded64}”
}

There is a requirement that we have to zip and encode our post data… We can’t just sent a request as above.

8. Create a jsr223 Pre-processor and lets write our code.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileUtils;
import java.nio.charset.StandardCharsets;
import java.io.*;String bodyString = sampler.getArguments().getArgument(0).getValue();
ByteArrayOutputStream rstBao = new ByteArrayOutputStream();
String JsonRequest = FileUtils.readFileToString(new File("${filepath}"));
GZIPOutputStream zos = new GZIPOutputStream(rstBao);
zos.write(JsonRequest.getBytes());
IOUtils.closeQuietly(zos);
byte[] bytes = rstBao.toByteArray();
//Here is where i am able to encode the bytes to base 64
Base64.encodeBase64String(bytes);
vars.put("postDataEncoded64",Base64.encodeBase64String(bytes));
//log.info("khemlall this is the content"+ Base64.encodeBase64String(bytes));
log.info(vars.get("postDataEncoded64"));
rstBao.close();

Filepath is from the CSV data set config.

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.