shyaway

Swagger CodeGen 을 이용한 JMeter Project 만들기 본문

Dev Tool

Swagger CodeGen 을 이용한 JMeter Project 만들기

shyaway 2018. 5. 8. 09:43

Extract JMeter test plans with Swagger CodeGen


JMeter is a great tool. It generates HTTP request for you once properly set up with a test plan and you can assert the response by adding an assertion step in your plan. You can easily perform stress test with this tool. If you've ever worked on a RESTful API project, you may have already heard of it or used it.


But making a test plan is not an easy task. You have to fill in those forms like HTTP URL, port, protocol, parameters, loop count, and number of threads, and add summary report and etc. I'm working on a project that has more than 130 APIs. Making all of them one by one will take like forever.


So I came up with a tool that does the heavy lifting for me. I'm already using Swagger UI on the project, so I started out with searching an extension in the swagger and fortunately, it has Swagger CodeGen. It just automatically converts OPEN API formatted JSON or YAML API description into miscellaneous clients and platforms.


You can actually see the live demo in here https://editor.swagger.io//?_ga=2.265510287.1018892707.1525671527-715046869.1523851982#/ . Just type in a proper JSON and select the one you want to get among the list of " Generate Client " or " Generate Server ". 


I'm going to deal with the private network circumstance where you can have no access to the external Internet. Here goes the installation of CodeGen in your local environment.



STEP 1. Install Java



STEP 2. Set up an environment variable

Copy the path for your JDK installation folder ( typically, it's located in C:\Program Files\Java\jdk{your-download-version} )

1. Execute sysdm.cpl on the command prompt
2. Click the advanced tab in the system property popup.
3. Click the environment variables on the bottom right.
4. Add JAVA_HOME in the system variable section, and paste the JDK installation path.
5. Edit Path item that already exists in the same section, and add  %JAVA_HOME%\bin; there.


STEP 3. Download Swagger CodeGen

Click the Download Zip button directly or use Git client to pull the resources from https://github.com/swagger-api/swagger-codegen 


STEP 4. Build CodeGen

1. Execute command prompt.
2. Go to the unzip folder of the codegen sources. type cd {your-swagger-codegen-path} in the prompt window.
3. Execute ./mvnw clean package

It took 20 minutes to complete the build job in my case. You have to be patient. If you see nothing happen in the prompt window, just type Enter then you will see the endless Maven Dependency Download messages rolling down on the console. It takes a long time, you just have to wait !!


STEP 5. Generate Codes

1. Prepare a OPEN API formatted JSON or YAML. ( Take this one as an example, the one that you can see on the left in this link Sample )
2. Put your JSON in somewhere.
3. Run the command below.

java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i C:\Users\YourName\YourDownloadedJSON.json -l jmeter -o C:\MyTestPlans\JMeter

After running this command, you're going to see that there're a bunch of jmx files and CSV pairs, each of them just matches the APIs that you've specified on your API description ( JSON / YAML ). Those CSVs are the list of parameters  that the API requires to pass in. Usually, the default value is 0. So, you have to put a proper test value in the file. If you want to use User Defined Variables in JMeter, you have to go to the Thread Group and see the nested menu in the API, and delete the CSV option there. Unless your User Defined Variables will be overridden by the CSV values.


You can extract not only JMeter...

but also you can generate C#, PHP, or JAVA HTTP Request clients by just switching the -l value like below.

java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i C:\Users\YourName\YourDownloadedJSON.json -l php -o C:\MyTestClient\PHP


java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i C:\Users\YourName\YourDownloadedJSON.json -l sharp -o C:\MyTestClient\CSharp


java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i C:\Users\YourName\YourDownloadedJSON.json -l java -o C:\MyTestClient\Java


CodeGen just automatically generates parameter type, parameter model ( class ), HTTP Request codes, project structures, necessary packages or third party resources, and all of the prerequisites to call the APIs in a specific platform or language.


Truly saved ton of times with this tool !




'Dev Tool' 카테고리의 다른 글

Quick Diagram Tool for C#  (2) 2018.07.28
Comments