From 7e78c5b98d488fe7ffbf04289937c40f64727474 Mon Sep 17 00:00:00 2001 From: Jordi Bisbal Ansaldo Date: Fri, 17 Nov 2023 11:10:31 +0100 Subject: [PATCH] Added playwright user to generate browser traffic --- src/loadgenerator/locustfile.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/loadgenerator/locustfile.py b/src/loadgenerator/locustfile.py index ea40f0614d..42f4c7312a 100644 --- a/src/loadgenerator/locustfile.py +++ b/src/loadgenerator/locustfile.py @@ -5,9 +5,11 @@ import json +import os import random import uuid from locust import HttpUser, task, between +from locust_plugins.users.playwright import PlaywrightUser, pw, PageWithRetry, event from opentelemetry import context, baggage, trace from opentelemetry.metrics import set_meter_provider @@ -129,3 +131,33 @@ def on_start(self): ctx = baggage.set_baggage("synthetic_request", "true") context.attach(ctx) self.index() + + +browser_traffic_enabled = os.environ.get('LOCUST_BROWSER_TRAFFIC_ENABLED', False) + +if browser_traffic_enabled: + class WebsiteBrowserUser(PlaywrightUser): + headless = True # to use a headless browser, without a GUI + multiplier = 1 # run concurrent playwright sessions/browsers for each Locust user + wait_time = between(1, 10) + + @task + @pw + async def open_cart_browser_page(self, page: PageWithRetry): + try: + async with event(self, "Load up Cart Page"): + await page.goto("/cart") + except: + pass + + @task + @pw + async def open_home_browser_page(self, page: PageWithRetry): + try: + async with event(self, "Load up Astronomy Shop home page"): + await page.goto("/") + async with event(self, "Click on a button"): + async with page.expect_navigation(wait_until="domcontentloaded"): + await page.click('button:has-text("Go Shopping")') + except: + pass \ No newline at end of file