Skip to content

Commit

Permalink
updating locust scripts with updated api responses and handling facto…
Browse files Browse the repository at this point in the history
…rial experiments (#855)

Co-authored-by: jreddig <[email protected]>
  • Loading branch information
ppratikcr7 and jreddig authored May 26, 2023
1 parent 54ea501 commit ededb68
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 67 deletions.
261 changes: 212 additions & 49 deletions backend/locust/createExperiment.py
Original file line number Diff line number Diff line change
@@ -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"]

Expand All @@ -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}")
Expand Down
41 changes: 32 additions & 9 deletions backend/locust/load_test_upgrade_mathia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down Expand Up @@ -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']
}

Expand All @@ -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']
}

Expand Down
Loading

0 comments on commit ededb68

Please sign in to comment.