Automate Image Compression using JMeter

How many of you do blogging regularly? Not me, for sure. If you come across any website that loads very slowly, there might be various reasons for that. One of them it would be, very large image size. There are various ways to compress images. You can use Microsoft Office Picture Manager, you can reduce the dimension of the image, you can save the image in grayscale or you can use online image compression tools such as TinyPNG. In this blog, we are going to see about how to automate image compression using JMeter.

What is TinyPNG?

Assume that, you need to compress one image, just only one image. Just visit tinypng.com and upload your image. TinyPNG will compress your image and provide the compressed image.

Automate Image Compression using JMeter
Automate Image Compression using JMeter

It reduces selectively few colors, so that you will get your image in a few bytes. Basically, you do not see any difference between the actual image and the compressed image. Many bloggers are using this online tool to compress high resolution images.

How to automate this process?

I was facing an issue, if i want to compress multiple images without visiting the online tool. Of course, there are plugins available, but if you do not want to leverage any such hooks, then my tool will be helpful.

Automating the image compression will save your time. In just a few clicks, you will get your compressed images.

Prerequisites

Before you begin downloading JMeter file, please subscribe for developer API from tinypng.

Go to tinypng.com/developers and enter your name and email ID to get your unique API key. Once you successfully subscribed, you get an API key.

Developer API
Developer API

Also, make sure that you have the latest version of JMeter.

How to use this tool?

You can download my tool from this link. You can fork it on GitHub. Before you execute the JMeter file, click on Beanshell Sampler and change the folder path in line 1 as shown below.

File folder = new File(“C://folderpath//yourimages”);

File[] files = folder.listFiles();

int counter = 1;

for (File file : files) {

    vars.put(“file_” + counter, file.getAbsolutePath());

    counter++;

}

Now open the HTTP Header Manager item and add the following header.

Header Name                :        Authorization

Header Value                :        Basic tinypng api key

JMeter Tool
JMeter Tool

That is it. You are good to run the JMeter file. Once the execution is completed, you can see the compressed images in the JMETER\bin\ folder.

How the tool works?

The key elements in JMeter are BeanShell Sampler, Upload File, and Download File.

  1. BeanShell Sampler, identifies how many files are there in the folder and retrieves the file names and store in the variable ${current_file}
  2. The upload File element is an HTTP sampler which uploads the image with the API key.
  3. The saving File element is also an HTTP sampler which downloads the image and save it locally.

Tool Limitations

  1. Make sure that you have only image type files in the folder.
  2. BeanShell Sampler will not loop thru the sub folders.
  3. The basic plan of TinyPNG allows you to compress only 500 images per month. If you need more images to compress, you need to switch it to paid plans.

You can fork it in GitHub and add other functionalities such as file type check etc. Similar implementation can be done for Tiny JPG as well.

Your turn: Please let me know if you find any defects in the tool. Also, please let me know if you need any help in setting up this tool.

About the Author

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Hamster - Launch JMeter Recent Test Plans SwiftlyDownload for free
+
Share via
Copy link