#!/usr/bin/python # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 import json import random import uuid from locust import HttpUser, task, between from opentelemetry import context, baggage, trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor tracer_provider = TracerProvider() trace.set_tracer_provider(tracer_provider) tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) # Instrumenting manually to avoid error with locust gevent monkey RequestsInstrumentor().instrument() URLLib3Instrumentor().instrument() categories = [ "binoculars", "telescopes", "accessories", "assembly", "travel", "books", None, ] products = [ "0PUK6V6EV0", "1YMWWN1N4O", "2ZYFJ3GM2N", "66VCHSJNUP", "6E92ZMYYFZ", "9SIQT8TOJO", "L9ECAV7KIM", "LS4PSXUNUM", "OLJCESPC7Z", "HQTGWGPNH4", ] people_file = open('people.json') people = json.load(people_file) class WebsiteUser(HttpUser): wait_time = between(1, 10) @task(1) def index(self): self.client.get("/") @task(10) def browse_product(self): self.client.get("/api/products/" + random.choice(products)) @task(3) def get_recommendations(self): params = { "productIds": [random.choice(products)], } self.client.get("/api/recommendations", params=params) @task(3) def get_ads(self): params = { "contextKeys": [random.choice(categories)], } self.client.get("/api/data/", params=params) @task(3) def view_cart(self): self.client.get("/api/cart") @task(2) def add_to_cart(self, user=""): if user == "": user = str(uuid.uuid1()) product = random.choice(products) self.client.get("/api/products/" + product) cart_item = { "item": { "productId": product, "quantity": random.choice([1, 2, 3, 4, 5, 10]), }, "userId": user, } self.client.post("/api/cart", json=cart_item) @task(1) def checkout(self): # checkout call with an item added to cart user = str(uuid.uuid1()) self.add_to_cart(user=user) checkout_person = random.choice(people) checkout_person["userId"] = user self.client.post("/api/checkout", json=checkout_person) @task(1) def checkout_multi(self): # checkout call which adds 2-4 different items to cart before checkout user = str(uuid.uuid1()) for i in range(random.choice([2, 3, 4])): self.add_to_cart(user=user) checkout_person = random.choice(people) checkout_person["userId"] = user self.client.post("/api/checkout", json=checkout_person) def on_start(self): ctx = baggage.set_baggage("synthetic_request", "true") context.attach(ctx) self.index()