-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathlocustfile.py
120 lines (100 loc) · 3.16 KB
/
locustfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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()