|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
17 | 17 | import random
|
18 |
| -from locust import HttpUser, TaskSet, between |
| 18 | +import uuid |
| 19 | +from locust import HttpUser, task, between |
19 | 20 |
|
20 | 21 | from opentelemetry import trace
|
21 | 22 | from opentelemetry.sdk.trace import TracerProvider
|
|
33 | 34 | URLLib3Instrumentor().instrument()
|
34 | 35 |
|
35 | 36 | products = [
|
36 |
| - '0PUK6V6EV0', |
37 |
| - '1YMWWN1N4O', |
38 |
| - '2ZYFJ3GM2N', |
39 |
| - '66VCHSJNUP', |
40 |
| - '6E92ZMYYFZ', |
41 |
| - '9SIQT8TOJO', |
42 |
| - 'L9ECAV7KIM', |
43 |
| - 'LS4PSXUNUM', |
44 |
| - 'OLJCESPC7Z'] |
| 37 | + "0PUK6V6EV0", |
| 38 | + "1YMWWN1N4O", |
| 39 | + "2ZYFJ3GM2N", |
| 40 | + "66VCHSJNUP", |
| 41 | + "6E92ZMYYFZ", |
| 42 | + "9SIQT8TOJO", |
| 43 | + "L9ECAV7KIM", |
| 44 | + "LS4PSXUNUM", |
| 45 | + "OLJCESPC7Z" |
| 46 | +] |
45 | 47 |
|
46 |
| -def index(l): |
47 |
| - l.client.get("/") |
| 48 | +people = [ |
| 49 | + { |
| 50 | + |
| 51 | + "address": { |
| 52 | + "streetAddress": "1600 Amphitheatre Parkway", |
| 53 | + "zipCode": 94043, |
| 54 | + "city": "Mountain View", |
| 55 | + "state": "CA", |
| 56 | + "country": "United States" |
| 57 | + }, |
| 58 | + "userCurrency": "USD", |
| 59 | + "creditCard": { |
| 60 | + "creditCardNumber": "4432-8015-6152-0454", |
| 61 | + "creditCardExpirationMonth": 1, |
| 62 | + "creditCardExpirationYear": 2039, |
| 63 | + "creditCardCvv": 672 |
| 64 | + } |
| 65 | + }, |
| 66 | + { |
| 67 | + |
| 68 | + "address": { |
| 69 | + "streetAddress": "One Microsoft Way", |
| 70 | + "zipCode": 98052, |
| 71 | + "city": "Redmond", |
| 72 | + "state": "WA", |
| 73 | + "country": "United States" |
| 74 | + }, |
| 75 | + "userCurrency": "USD", |
| 76 | + "creditCard": { |
| 77 | + "creditCardNumber": "4532-4211-7434-1278", |
| 78 | + "creditCardExpirationMonth": 2, |
| 79 | + "creditCardExpirationYear": 2039, |
| 80 | + "creditCardCvv": 114 |
| 81 | + } |
| 82 | + }, |
| 83 | + { |
| 84 | + |
| 85 | + "address": { |
| 86 | + "streetAddress": "One Apple Park Way", |
| 87 | + "zipCode": 95014, |
| 88 | + "city": "Cupertino", |
| 89 | + "state": "CA", |
| 90 | + "country": "United States" |
| 91 | + }, |
| 92 | + "userCurrency": "USD", |
| 93 | + "creditCard": { |
| 94 | + "creditCardNumber": "4532-6178-2799-1951", |
| 95 | + "creditCardExpirationMonth": 3, |
| 96 | + "creditCardExpirationYear": 2039, |
| 97 | + "creditCardCvv": 239 |
| 98 | + } |
| 99 | + }, |
| 100 | + { |
| 101 | + |
| 102 | + "address": { |
| 103 | + "streetAddress": "1 Hacker Way", |
| 104 | + "zipCode": 94025, |
| 105 | + "city": "Menlo Park", |
| 106 | + "state": "CA", |
| 107 | + "country": "United States" |
| 108 | + }, |
| 109 | + "userCurrency": "USD", |
| 110 | + "creditCard": { |
| 111 | + "creditCardNumber": "4539-1103-5661-7083", |
| 112 | + "creditCardExpirationMonth": 4, |
| 113 | + "creditCardExpirationYear": 2039, |
| 114 | + "creditCardCvv": 784 |
| 115 | + } |
| 116 | + }, |
| 117 | + { |
| 118 | + |
| 119 | + "address": { |
| 120 | + "streetAddress": "410 Terry Ave N", |
| 121 | + "zipCode": 98109, |
| 122 | + "city": "Seattle", |
| 123 | + "state": "WA", |
| 124 | + "country": "United States" |
| 125 | + }, |
| 126 | + "userCurrency": "USD", |
| 127 | + "creditCard": { |
| 128 | + "creditCardNumber": "4916-0816-6217-7968", |
| 129 | + "creditCardExpirationMonth": 5, |
| 130 | + "creditCardExpirationYear": 2039, |
| 131 | + "creditCardCvv": 397 |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + |
| 136 | + "address": { |
| 137 | + "streetAddress": "100 Winchester Circle", |
| 138 | + "zipCode": 95032, |
| 139 | + "city": "Los Gatos", |
| 140 | + "state": "CA", |
| 141 | + "country": "United States" |
| 142 | + }, |
| 143 | + "userCurrency": "USD", |
| 144 | + "creditCard": { |
| 145 | + "creditCardNumber": "4929-5431-0337-5647", |
| 146 | + "creditCardExpirationMonth": 6, |
| 147 | + "creditCardExpirationYear": 2039, |
| 148 | + "creditCardCvv": 793 |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + |
| 153 | + "address": { |
| 154 | + "streetAddress": "150 Elgin St", |
| 155 | + "zipCode": 214, |
| 156 | + "city": "Ottawa", |
| 157 | + "state": "ON", |
| 158 | + "country": "Canada" |
| 159 | + }, |
| 160 | + "userCurrency": "CAD", |
| 161 | + "creditCard": { |
| 162 | + "creditCardNumber": "4763-1844-9699-8031", |
| 163 | + "creditCardExpirationMonth": 7, |
| 164 | + "creditCardExpirationYear": 2039, |
| 165 | + "creditCardCvv": 488 |
| 166 | + } |
| 167 | + }, |
| 168 | + { |
| 169 | + |
| 170 | + "address": { |
| 171 | + "streetAddress": "1355 Market St", |
| 172 | + "zipCode": 94103, |
| 173 | + "city": "San Francisco", |
| 174 | + "state": "CA", |
| 175 | + "country": "United States" |
| 176 | + }, |
| 177 | + "userCurrency": "USD", |
| 178 | + "creditCard": { |
| 179 | + "creditCardNumber": "4929-6495-8333-3657", |
| 180 | + "creditCardExpirationMonth": 8, |
| 181 | + "creditCardExpirationYear": 2039, |
| 182 | + "creditCardCvv": 159 |
| 183 | + } |
| 184 | + }, |
| 185 | + { |
| 186 | + |
| 187 | + "address": { |
| 188 | + "streetAddress": "2200 Mission College Blvd", |
| 189 | + "zipCode": 95054, |
| 190 | + "city": "Santa Clara", |
| 191 | + "state": "CA", |
| 192 | + "country": "United States" |
| 193 | + }, |
| 194 | + "userCurrency": "USD", |
| 195 | + "creditCard": { |
| 196 | + "creditCardNumber": "4485-4803-8707-3547", |
| 197 | + "creditCardExpirationMonth": 9, |
| 198 | + "creditCardExpirationYear": 2039, |
| 199 | + "creditCardCvv": 682 |
| 200 | + } |
| 201 | + } |
| 202 | +] |
48 | 203 |
|
49 |
| -def setCurrency(l): |
50 |
| - currencies = ['EUR', 'USD', 'JPY', 'CAD'] |
51 |
| - l.client.post("/setCurrency", |
52 |
| - {'currency_code': random.choice(currencies)}) |
53 | 204 |
|
54 |
| -def browseProduct(l): |
55 |
| - l.client.get("/product/" + random.choice(products)) |
| 205 | +class WebsiteUser(HttpUser): |
| 206 | + wait_time = between(1, 10) |
56 | 207 |
|
57 |
| -def viewCart(l): |
58 |
| - l.client.get("/cart") |
| 208 | + @task(1) |
| 209 | + def index(self): |
| 210 | + self.client.get("/") |
59 | 211 |
|
60 |
| -def addToCart(l): |
61 |
| - product = random.choice(products) |
62 |
| - l.client.get("/product/" + product) |
63 |
| - l.client.post("/cart", { |
64 |
| - 'product_id': product, |
65 |
| - 'quantity': random.choice([1,2,3,4,5,10])}) |
| 212 | + @task(10) |
| 213 | + def browse_product(self): |
| 214 | + self.client.get("/product/" + random.choice(products)) |
66 | 215 |
|
67 |
| -def checkout(l): |
68 |
| - addToCart(l) |
69 |
| - l.client.post("/cart/checkout", { |
70 |
| - |
71 |
| - 'street_address': '1600 Amphitheatre Parkway', |
72 |
| - 'zip_code': '94043', |
73 |
| - 'city': 'Mountain View', |
74 |
| - 'state': 'CA', |
75 |
| - 'country': 'United States', |
76 |
| - 'credit_card_number': '4432-8015-6152-0454', |
77 |
| - 'credit_card_expiration_month': '1', |
78 |
| - 'credit_card_expiration_year': '2039', |
79 |
| - 'credit_card_cvv': '672', |
80 |
| - }) |
| 216 | + @task(3) |
| 217 | + def view_cart(self): |
| 218 | + self.client.get("/cart") |
81 | 219 |
|
82 |
| -class UserBehavior(TaskSet): |
| 220 | + @task(2) |
| 221 | + def add_to_cart(self, user=""): |
| 222 | + if user == "": |
| 223 | + user = str(uuid.uuid1()) |
| 224 | + product = random.choice(products) |
| 225 | + self.client.get("/product/" + product) |
| 226 | + cart_item = { |
| 227 | + "item": { |
| 228 | + "productId": product, |
| 229 | + "quantity": random.choice([1, 2, 3, 4, 5, 10]) |
| 230 | + }, |
| 231 | + "userId": user |
| 232 | + } |
| 233 | + self.client.post("/api/cart", json=cart_item) |
83 | 234 |
|
84 |
| - def on_start(self): |
85 |
| - index(self) |
| 235 | + @task(1) |
| 236 | + def checkout(self): |
| 237 | + # checkout call with an item added to cart |
| 238 | + user = str(uuid.uuid1()) |
| 239 | + self.add_to_cart(user=user) |
| 240 | + checkout_person = random.choice(people) |
| 241 | + checkout_person["userId"] = user |
| 242 | + self.client.post("/api/checkout", json=checkout_person) |
86 | 243 |
|
87 |
| - tasks = {index: 1, |
88 |
| - setCurrency: 2, |
89 |
| - browseProduct: 10, |
90 |
| - addToCart: 2, |
91 |
| - viewCart: 3, |
92 |
| - checkout: 1} |
| 244 | + @task(1) |
| 245 | + def checkout_multi(self): |
| 246 | + # checkout call which adds 2-4 different items to cart before checkout |
| 247 | + user = str(uuid.uuid1()) |
| 248 | + for i in range(random.choice([2, 3, 4])): |
| 249 | + self.add_to_cart(user=user) |
| 250 | + checkout_person = random.choice(people) |
| 251 | + checkout_person["userId"] = user |
| 252 | + self.client.post("/api/checkout", json=checkout_person) |
93 | 253 |
|
94 |
| -class WebsiteUser(HttpUser): |
95 |
| - tasks = [UserBehavior] |
96 |
| - wait_time = between(1, 10) |
| 254 | + def on_start(self): |
| 255 | + self.index() |
0 commit comments