Skip to content

Instantly share code, notes, and snippets.

@rhopp
Last active April 19, 2018 13:13
Show Gist options
  • Save rhopp/e4fd70a0494fd740e33c4c243b302ef5 to your computer and use it in GitHub Desktop.
Save rhopp/e4fd70a0494fd740e33c4c243b302ef5 to your computer and use it in GitHub Desktop.
che-stress script
Both scripts expects file "users.tokens" with as many user active tokens as needed (one token per line)
# che-stress.py
Usage:
* Replace URL in the file (currently there is che-starter from prod-preview) on line 15
* Chose how many create workspace requests should be sent per user - variable `workspacesCount` on line 5
* run `python che-stress.py`. This creates thread for each user and sends `workspacesCount` requests.
# che-cleanup.py
This script deletes all workspaces for given users
* Replace che-starter url on line 9
* Run `python che-cleanup.py`
import os, threading, time, random
import requests
tokenFileName = "users.tokens"
tokenFile = open(tokenFileName, 'r')
def getWorkspaceStatus(id, token):
headers = {"Authorization":"Bearer "+token, "Content-Type":"application/json"}
workspaceResponse = requests.get("https://rhche.prod-preview.openshift.io/api/workspace/"+workspace["id"],headers=headers)
return workspaceResponse.json()["status"]
def waitForWorkspaceToStop(id, token):
while getWorkspaceStatus(id, token) != "STOPPED":
print "Waiting for workspace to stop"
time.sleep(1)
def deleteWorker(userTokenTuple):
headers = {"Authorization":"Bearer "+userTokenTuple[1], "Content-Type":"application/json"}
r = requests.get("https://rhche.prod-preview.openshift.io/api/workspace",headers=headers)
resp_json=r.json()
for workspace in resp_json:
print "Deleting workspace: "+workspace["id"]
if workspace["status"] != "STOPPED":
print "Have to stop workspace first"
requests.delete("https://rhche.prod-preview.openshift.io/api/workspace/"+workspace["id"]+"/runtime",headers=headers)
waitForWorkspaceToStop(workspace["id"], userTokenTuple[1])
deleteResponse = requests.delete("https://rhche.prod-preview.openshift.io/api/workspace/"+workspace["id"],headers=headers)
if deleteResponse.status_code == 204:
print "Successfully deleted workspace "+workspace["id"]
else:
print "Failed to delete workspace "+workspace["id"]+", reason: "+deleteResponse.text
threads = []
counter = 1
tokenFile.seek(0)
for line in tokenFile:
t=threading.Thread(target=deleteWorker, args=((counter,line.rstrip()),))
threads.append(t)
t.start()
counter+=1
for t in threads:
t.join()
import os, threading, time, random
import requests
# How many workspaces should be created for each user
workspacesCount=10
tokenFileName = "users.tokens"
tokenFile = open(tokenFileName, 'r')
jsonBody='{"branch": "master","description": "mycustomdescription","repo": "https://github.com/osiotest/vertx-http-booster","stackId": "vert.x"}'
def sendRequest(userTokenTuple):
params = {"masterUrl":"test","namespace":"test"}
headers = {"Authorization":"Bearer "+userTokenTuple[1], "Content-Type":"application/json"}
# print userTokenTuple
r = requests.post("http://che-starter-dsaas-preview.b6ff.rh-idev.openshiftapps.com/workspace",headers=headers,params=params, data=jsonBody)
if r.status_code != 200:
print "=================================ERROR CREATING WORKSPACE==================================="
print "Thread #"+str(userTokenTuple[0])+" response.status_code"+str(r.status_code)
print "Thread #"+str(userTokenTuple[0])+" response.text"+r.text
else:
print "Workspace successfully created"
def createWorker(userTokenTuple):
for i in range(workspacesCount):
print "Thread #"+str(userTokenTuple[0])+" try: "+str(i)
sendRequest(userTokenTuple)
time.sleep(2)
threads = []
counter = 1
for line in tokenFile:
t=threading.Thread(target=createWorker, args=((counter,line.rstrip()),))
threads.append(t)
t.start()
counter+=1
for t in threads:
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment