API Testing : Check API performance

Verify that the API correctly handles API performance and returns the correct resources for each API performance metric.

Language: Java

Framework: Rest assured

copy
1import org.junit.jupiter.api.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class ApiPerformanceTest {67 private String baseUrl = "https:/​/​your.api.base.url";89 @Test10 public void testApiPerformance() {11 /​/​ Assumptions12 /​/​ - API contains performance metrics under "/​api/​metrics" endpoint13 /​/​ - Metrics are returned in JSON format14 /​/​ - API can handle at least 1000 requests per minute1516 given().baseUri(baseUrl)17 .when().get("/​api/​metrics")18 .then().statusCode(200)19 .body("metrics", not(empty()))20 .body("metrics.requestPerMinute", greaterThanOrEqualTo(1000));21 }2223 /​/​ Uncomment the following code to connect to remote client with desired capabilities24 /​*25 @Test26 public void testApiPerformanceOnRemoteClient() {2728 /​/​ Desired capabilities for remote client29 DesiredCapabilities capabilities = new DesiredCapabilities();30 capabilities.setCapability("browserName", "Chrome");31 capabilities.setCapability("version", "91");3233 RemoteWebDriver driver = null;3435 try {36 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);3738 /​/​ Test code with remote driver39 /​/​ ...4041 } catch (MalformedURLException e) {42 e.printStackTrace();43 } finally {44 if (driver != null) {45 driver.quit();46 }47 }48 }49 */​50}

Language: Javascript

copy
1/​/​ Mocha + Chai.23/​/​Assuming the API URL is 'api.com/​performance'4/​/​Assuming the API returns performance metrics in JSON format5/​/​Assuming the expected performance metrics are 'loadTime', 'responseTime', 'throughput'67const expect = require('chai').expect;8const axios = require('axios');910describe('API Performance Test', function() {11 let apiResponse;12 13 before(async function() {14 /​/​Assuming API requires authentication15 const requestConfig = {16 auth: {17 username: 'apiusername',18 password: 'apipassword'19 }20 };21 22 apiResponse = await axios.get('api.com/​performance', requestConfig);23 });24 25 it('should return performance metrics in JSON format', function() {26 expect(apiResponse.headers['content-type']).to.equal('application/​json')27 });28 29 it('should return expected performance metrics', function() {30 const performanceMetrics = Object.keys(apiResponse.data);31 expect(performanceMetrics).to.include.members(['loadTime', 'responseTime', 'throughput']);32 });33 34 /​/​Assuming there is a benchmark for each performance metric and values are within acceptable range35 it('should return performance metrics within acceptable range', function() {36 const metrics = apiResponse.data;37 expect(metrics.loadTime).to.be.within(0, benchmarkLoadTime);38 expect(metrics.responseTime).to.be.within(0, benchmarkResponseTime);39 expect(metrics.throughput).to.be.within(0, benchmarkThroughput);40 });41 42 after(function() {43 /​/​Assuming clean up is needed44 });45});4647/​/​Assuming desired capabilities to connect to remote client48/​/​ const webdriver = require('selenium-webdriver');49/​/​ const remote = require('selenium-webdriver/​remote');5051/​/​ let driver = new webdriver.Builder()52/​/​ .usingServer('http:/​/​remoteipaddress:4444/​wd/​hub')53/​/​ .withCapabilities(webdriver.Capabilities.chrome())54/​/​ .setFirefoxOptions(new firefox.Options().headless())55/​/​ .build();

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing