From ededb685d9f80488db9a4d19f6e3ab42bdf8aa26 Mon Sep 17 00:00:00 2001 From: Pratik Prajapati <33730817+ppratikcr7@users.noreply.github.com> Date: Fri, 26 May 2023 23:17:50 +0530 Subject: [PATCH] updating locust scripts with updated api responses and handling factorial experiments (#855) Co-authored-by: jreddig <97543136+jreddig@users.noreply.github.com> --- backend/locust/createExperiment.py | 261 +++++++++++++++++---- backend/locust/load_test_upgrade_mathia.py | 41 +++- backend/locust/upgrade_mathia.py | 26 +- 3 files changed, 261 insertions(+), 67 deletions(-) diff --git a/backend/locust/createExperiment.py b/backend/locust/createExperiment.py index 1f3bd951b0..803cd9c96b 100644 --- a/backend/locust/createExperiment.py +++ b/backend/locust/createExperiment.py @@ -1,10 +1,11 @@ import random import requests +import uuid -def createExperiment(protocol, host, allExperimentPartitionIDConditionPair): +def createExperiment(protocol, host, allExperimentPartitionIDConditionPair, experimentType): url = protocol + f"://{host}/api/experiments" - # context = ["assign-prog", "app", "addition"] - context = ["addition"] + context = ["assign-prog"] + # context = ["addition"] states = ["enrolling"] postExperimentRules = ["assign", "continue"] @@ -27,57 +28,219 @@ def createExperiment(protocol, host, allExperimentPartitionIDConditionPair): conditionCode1 = "c" + str(random.randint(1,n)) conditionCode2 = "c" + str(random.randint(1,n)) - expId1 = "id" + str(random.randint(1,n)) - expPoint1 = "p" + str(random.randint(1,n)) - expId2 = "id" + str(random.randint(1,n)) - expPoint2 = "p" + str(random.randint(1,n)) + target1 = "id" + str(random.randint(1,n)) + site1 = "p" + str(random.randint(1,n)) + target2 = "id" + str(random.randint(1,n)) + site2 = "p" + str(random.randint(1,n)) + factor1 = "F" + str(random.randint(1,n)) + factor2 = "F" + str(random.randint(1,n)) + level1 = "L" + str(random.randint(1,n)) + level2 = "L" + str(random.randint(1,n)) + level3 = "L" + str(random.randint(1,n)) + factorialConditionCode1 = factor1 + "=" + level1 + "; " + factor2 + "=" + level3 + factorialConditionCode2 = factor1 + "=" + level2 + "; " + factor2 + "=" + level3 + levelId1 = str(uuid.uuid4()) + levelId2 = str(uuid.uuid4()) + levelId3 = str(uuid.uuid4()) - PartitionIDConditionPair1 = {"experimentPoint": expPoint1, "partitionId" : expId1, "condition" : conditionCode1} - PartitionIDConditionPair2 = {"experimentPoint": expPoint2, "partitionId" : expId2, "condition" : conditionCode2} - allExperimentPartitionIDConditionPair.append(PartitionIDConditionPair1) - allExperimentPartitionIDConditionPair.append(PartitionIDConditionPair2) - - # JSON data for creating an experiment: - data = { - "name": "LocustLoadTestExp"+ str(random.randint(1,n)), - "description": "Test experiment is created here", - "consistencyRule": consistencyRules[0], - "assignmentUnit": asssignmentUnits[0], - "group": groups[0], - "postExperimentRule": random.choice(postExperimentRules), - "state": random.choice(states), - "tags": ["Workspace", "Content"], - "context": [random.choice(context)], - "conditions": [ - { - "name": "condition1", - "description": "condition description 1", - "assignmentWeight": weight, - "conditionCode": conditionCode1 + if experimentType == 'Simple': + PartitionIDConditionPair1 = {"site": site1, "target" : target1, "condition" : conditionCode1} + PartitionIDConditionPair2 = {"site": site2, "target" : target2, "condition" : conditionCode2} + # JSON data for creating an experiment: + data = { + "name": "LocustLoadTestExp"+ str(random.randint(1,n)), + "description": "Test experiment is created here", + "consistencyRule": consistencyRules[0], + "assignmentUnit": asssignmentUnits[0], + "group": groups[0], + "postExperimentRule": random.choice(postExperimentRules), + "state": random.choice(states), + "tags": ["Workspace", "Content"], + "context": [random.choice(context)], + "filterMode": "includeAll", + "type": "Simple", + "conditions": [ + { + "name": "condition1", + "description": "condition description 1", + "assignmentWeight": weight, + "conditionCode": conditionCode1 + }, + { + "name": "condition2", + "description": "condition description 2", + "assignmentWeight": 100-weight, + "conditionCode": conditionCode2 + } + ], + "partitions": [ + { + "name": site1, + "site": site1, + "target": target1, + "description": target1 + }, + { + "name": site2, + "site": site2, + "target": target2, + "description": target2 + } + ], + "experimentSegmentInclusion": { + "userIds": [], + "groups": [ { "type": "All", "groupId": "All" } ], + "subSegmentIds": [], + "type": "private" }, - { - "name": "condition2", - "description": "condition description 2", - "assignmentWeight": 100-weight, - "conditionCode": conditionCode2 - } - ], - "partitions": [ - { - "name": expPoint1, - "expId": expId1, - "expPoint": expPoint1, - "description": expId1 + "experimentSegmentExclusion": { + "userIds": [], + "groups": [], + "subSegmentIds": [], + "type": "private" } + } + else: + PartitionIDConditionPair1 = {"site": site1, "target" : target1, "condition" : factorialConditionCode1} + PartitionIDConditionPair2 = {"site": site2, "target" : target2, "condition" : factorialConditionCode2} + # JSON data for creating an experiment: + data = { + "name": "LocustLoadTestExp"+ str(random.randint(1,n)), + "description": "Test experiment is created here", + "consistencyRule": consistencyRules[0], + "assignmentUnit": asssignmentUnits[0], + "group": groups[0], + "postExperimentRule": random.choice(postExperimentRules), + "state": random.choice(states), + "tags": ["Workspace", "Content"], + "context": [random.choice(context)], + "filterMode": "includeAll", + "type": "Factorial", + "conditions": [ + { + "name": "condition1", + "description": "condition description 1", + "assignmentWeight": weight, + "conditionCode": factorialConditionCode1, + "levelCombinationElements": [ + { + "level": { + "id": levelId1, + "name": level1, + "description": level1, + "payloadValue": level1, + "payloadType": "string" + } + }, + { + "level": { + "id": levelId3, + "name": level3, + "description": level3, + "payloadValue": level3, + "payloadType": "string" + } + } + ] + }, + { + "name": "condition2", + "description": "condition description 2", + "assignmentWeight": weight, + "conditionCode": factorialConditionCode2, + "levelCombinationElements": [ + { + "level": { + "id": levelId2, + "name": level2, + "description": level2, + "payloadValue": level2, + "payloadType": "string" + } + }, + { + "level": { + "id": levelId3, + "name": level3, + "description": level3, + "payloadValue": level3, + "payloadType": "string" + } + } + ] + }, + ], + "partitions": [ + { + "name": site1, + "site": site1, + "target": target1, + "description": target1 + }, + { + "name": site2, + "site": site2, + "target": target2, + "description": target2 + } + ], + "factors": [ + { + "name": factor1, + "description": factor1, + "levels": [ + { + "id": levelId1, + "name": level1, + "description": level1, + "payload": { + "type": "string", + "value": level1 + } + }, + { + "id": levelId2, + "name": level2, + "description": level2, + "payload": { + "type": "string", + "value": level2 + } + } + ] + }, + { + "name": factor2, + "description": factor2, + "levels": [ + { + "id": levelId3, + "name": level3, + "description": level3, + "payload": { + "type": "string", + "value": level3 + } + } + ] + } + ], + "conditionPayloads": [], + "experimentSegmentInclusion": { + "userIds": [], + "groups": [{ "type": "All", "groupId": "All" }], + "subSegmentIds": [], + "type": "private" }, - { - "name": expPoint2, - "expId": expId2, - "expPoint": expPoint2, - "description": expId2 + "experimentSegmentExclusion": { + "userIds": [], + "groups": [], + "subSegmentIds": [], + "type": "private" } - ] - } + } + allExperimentPartitionIDConditionPair.append(PartitionIDConditionPair1) + allExperimentPartitionIDConditionPair.append(PartitionIDConditionPair2) response = requests.post(url, json = data) if response.status_code != 200: print(f"createExperiment Failed with {response.status_code}") diff --git a/backend/locust/load_test_upgrade_mathia.py b/backend/locust/load_test_upgrade_mathia.py index 713def6b03..4b95a5dc25 100644 --- a/backend/locust/load_test_upgrade_mathia.py +++ b/backend/locust/load_test_upgrade_mathia.py @@ -4,6 +4,7 @@ from upgrade_mathia_data import modules, workspaces from locust import HttpUser, SequentialTaskSet, task, tag, between import createExperiment +import deleteExperiment schools = {} students = {} @@ -14,12 +15,27 @@ protocol = "http" host = "localhost:3030" +# clear existing experiments: +option = int(input("Enter 1 for delete a single random experiment and 2 to delete all experiments, else Enter 0 for not deleting any experiment: ")) + +if option == 1: + expIds = deleteExperiment.getExperimentIds(protocol, host) + deleteExperiment.deleteExperiment(protocol, host, expIds) +elif option == 2: + expIds = deleteExperiment.getExperimentIds(protocol, host) + for i in range(len(expIds)): + expIds = deleteExperiment.getExperimentIds(protocol, host) + deleteExperiment.deleteExperiment(protocol, host, expIds) +else: + pass + # create new experiments: experimentCount = int(input("Enter the number of experiments to be created: ")) - for i in range(experimentCount): + experimentType = int(input("Enter the experiment type: Enter 1 for Simple and 2 for Factorial:")) # returning the updated partionconditionpair list: - allExperimentPartitionIDConditionPair = createExperiment.createExperiment(protocol, host, allExperimentPartitionIDConditionPair) + experimentType = "Simple" if experimentType == 1 else "Factorial" + allExperimentPartitionIDConditionPair = createExperiment.createExperiment(protocol, host, allExperimentPartitionIDConditionPair, experimentType) ### Start enrolling students in the newly created experiment: ### #Return a new Student @@ -276,14 +292,17 @@ def getAllExperimentConditionsAssignProg(self): def markExperimentPoint(self): url = protocol + f"://{host}/api/mark" print("/mark for userid: " + self.student["studentId"]) - + if(len(allExperimentPartitionIDConditionPair) == 0): + print("No assigned conditions found") + return + else: + print("allExperimentPartitionIDConditionPair: ", allExperimentPartitionIDConditionPair) # pick a random pair of PartitionIdConditionId from allExperimentPartitionIDConditionPair markPartitionIDConditionPair = random.choice(allExperimentPartitionIDConditionPair) - data = { "userId": self.student["studentId"], - "experimentPoint": markPartitionIDConditionPair['experimentPoint'], - "partitionId": markPartitionIDConditionPair['partitionId'], + "experimentPoint": markPartitionIDConditionPair['site'], + "partitionId": markPartitionIDConditionPair['target'], "condition": markPartitionIDConditionPair['condition'] } @@ -307,14 +326,18 @@ def markExperimentPoint(self): def failedExperimentPoint(self): url = protocol + f"://{host}/api/failed" print("/failed for userid: " + self.student["studentId"]) - + if(len(allExperimentPartitionIDConditionPair) == 0): + print("No assigned conditions found") + return + else: + print("allExperimentPartitionIDConditionPair: ", allExperimentPartitionIDConditionPair) # pick a random pair of PartitionIdConditionId from allExperimentPartitionIDConditionPair markPartitionIDConditionPair = random.choice(allExperimentPartitionIDConditionPair) data = { "userId": self.student["studentId"], - "experimentPoint": markPartitionIDConditionPair['experimentPoint'], - "partitionId": markPartitionIDConditionPair['partitionId'], + "experimentPoint": markPartitionIDConditionPair['site'], + "partitionId": markPartitionIDConditionPair['target'], "condition": markPartitionIDConditionPair['condition'] } diff --git a/backend/locust/upgrade_mathia.py b/backend/locust/upgrade_mathia.py index df47a32632..e08a34e69e 100644 --- a/backend/locust/upgrade_mathia.py +++ b/backend/locust/upgrade_mathia.py @@ -13,21 +13,23 @@ allExperimentPartitionIDConditionPair = [] -# clear all existing experiments: - # Setting host URL's: protocol = "http" host = "localhost:3030" -option = int(input("Enter 1 for delete a single random experiment and 2 to delete all experiments: ")) + +# clear existing experiments: +option = int(input("Enter 1 for delete a single random experiment and 2 to delete all experiments, else Enter 0 for not deleting any experiment: ")) expIds = deleteExperiment.getExperimentIds(protocol, host) if option == 1: - deleteExperiment.deleteExperiment(protocol, host,expIds) -else: + deleteExperiment.deleteExperiment(protocol, host, expIds) +elif option == 2: for i in range(len(expIds)): expIds = deleteExperiment.getExperimentIds(protocol, host) deleteExperiment.deleteExperiment(protocol, host, expIds) +else: + pass # create new experiments: # set groupExp to True for creating a group level experiment or False for a individual level experiment: @@ -35,8 +37,10 @@ experimentCount = int(input("Enter the number of experiments to be created: ")) for i in range(experimentCount): + experimentType = int(input("Enter the experiment type: Enter 1 for Simple and 2 for Factorial:")) # returning the updated partionconditionpair list: - allExperimentPartitionIDConditionPair = createExperiment.createExperiment(protocol, host, groupExp, allExperimentPartitionIDConditionPair) + experimentType = "Simple" if experimentType == 1 else "Factorial" + allExperimentPartitionIDConditionPair = createExperiment.createExperiment(protocol, host, allExperimentPartitionIDConditionPair, experimentType) ### Start enrolling students in the newly created experiment: ### #Return a new or existing Student @@ -307,14 +311,18 @@ def markExperimentPoint(self): with student["lock"]: url = protocol + f"://{host}/api/mark" print("/mark for userid: " + student["studentId"]) - + if(len(allExperimentPartitionIDConditionPair) == 0): + print("No assigned conditions found") + return + else: + print("allExperimentPartitionIDConditionPair: ", allExperimentPartitionIDConditionPair) # pick a random pair of PartitionIdConditionId from allExperimentPartitionIDConditionPair markPartitionIDConditionPair = random.choice(allExperimentPartitionIDConditionPair) data = { "userId": student["studentId"], - "experimentPoint": markPartitionIDConditionPair['experimentPoint'], - "partitionId": markPartitionIDConditionPair['partitionId'], + "experimentPoint": markPartitionIDConditionPair['site'], + "partitionId": markPartitionIDConditionPair['target'], "condition": markPartitionIDConditionPair['condition'] }