Performance testing is a crucial aspect of ensuring that your web applications run efficiently under various conditions. In this section, we will explore how to use Playwright to conduct performance testing, focusing on measuring and analyzing the performance of web applications.

Key Concepts in Performance Testing

  1. Load Time: The time it takes for a page to fully load.
  2. Response Time: The time taken for a server to respond to a request.
  3. Throughput: The number of requests a system can handle in a given time period.
  4. Resource Utilization: The usage of CPU, memory, and network resources during the test.

Setting Up Playwright for Performance Testing

Before diving into performance testing, ensure you have Playwright installed and set up in your project. If not, refer to the "Installing Playwright" section in Module 2.

Installing Additional Tools

For performance testing, you might need additional tools like lighthouse for more detailed insights. Install it using:

npm install -g lighthouse

Writing a Basic Performance Test

Let's start by writing a simple script to measure the load time of a webpage.

Example Code

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  const start = Date.now();
  await page.goto('https://example.com');
  const loadTime = Date.now() - start;

  console.log(`Page load time: ${loadTime}ms`);

  await browser.close();
})();

Explanation

  • Importing Playwright: We import the chromium module from Playwright to launch a browser instance.
  • Measuring Load Time: We record the time before and after the page.goto() function to calculate the page load time.
  • Output: The script logs the load time in milliseconds.

Analyzing Performance Metrics

Using Lighthouse

Lighthouse provides a comprehensive analysis of web performance. You can run it from the command line:

lighthouse https://example.com --output html --output-path ./report.html

This command generates an HTML report with detailed performance metrics.

Key Metrics to Analyze

  • First Contentful Paint (FCP): Time taken for the first piece of content to appear.
  • Time to Interactive (TTI): Time taken for the page to become fully interactive.
  • Speed Index: How quickly the contents of a page are visibly populated.

Practical Exercise

Task

Write a Playwright script to measure the response time of a specific API endpoint.

Solution

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  const response = await page.goto('https://api.example.com/data');
  const responseTime = response.timing().responseEnd - response.timing().requestStart;

  console.log(`API response time: ${responseTime}ms`);

  await browser.close();
})();

Explanation

  • Response Timing: We use the response.timing() method to calculate the time between the request start and response end.

Common Mistakes and Tips

  • Network Conditions: Simulate different network conditions using Playwright's page.emulateNetworkConditions() to test performance under various scenarios.
  • Resource Monitoring: Use browser developer tools to monitor CPU and memory usage during tests.

Conclusion

In this section, we explored how to use Playwright for performance testing, focusing on measuring load and response times. By integrating tools like Lighthouse, you can gain deeper insights into your application's performance. In the next section, we will delve into visual testing with Playwright, enhancing your testing strategy further.

© Copyright 2024. All rights reserved