Verified Commit 7e7420e2 authored by Mike Barkmin's avatar Mike Barkmin 🕶
Browse files

improve load-testing

parent 07117e2f
Pipeline #230088168 passed with stages
in 9 minutes and 24 seconds
.vscode
public
# Created by https://www.gitignore.io/api/python,pycharm
......
......@@ -4,10 +4,14 @@ url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
black = "*"
[packages]
locust = "*"
hcloud = "*"
[requires]
python_version = "3.7"
python_version = "3"
[pipenv]
allow_prereleases = true
This diff is collapsed.
......@@ -22,4 +22,4 @@ runcmd:
- systemctl enable docker
- docker pull "registry.gitlab.com/openpatch/runner-java:v1.0.0"
- docker network create "openpatch-runner-net"
- docker run --name=runner -p 80:80 -v /var/run/docker.sock:/var/run/docker.sock --net openpatch-runner-net registry.gitlab.com/openpatch/runner:v1.0.1
- docker run -d --restart=unless-stopped --name=runner -p 80:80 -e 'OPENPATCH_PROCESSES=16' -v /var/run/docker.sock:/var/run/docker.sock --net openpatch-runner-net registry.gitlab.com/openpatch/runner:v1.0.1
import os
import sys
import time
import subprocess
import pipes
from typing import List
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.servers.client import BoundServer, CreateServerResponse
from hcloud.server_types.client import ServerType
def exists_remote(host, path):
"""Test if a file exists at path on a host accessible with SSH."""
status = subprocess.call(
["ssh", host, "test -f {}".format(pipes.quote(path))],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if status == 0:
return True
if status == 1 or status == 255:
return False
def up(server_type="cx11"):
client = Client(token=os.getenv("HCLOUD_API_TOKEN"))
with open("cloud-config.yml", "r") as user_data:
response = client.servers.create(
response: CreateServerResponse = client.servers.create(
name="load-testing",
server_type=ServerType(name=server_type),
image=Image(name="ubuntu-18.04"),
image=Image(name="ubuntu-20.04"),
user_data=user_data.read(),
ssh_keys=client.ssh_keys.get_all(),
)
server = response.server
print(f"Created {server.public_net.ipv4.ip}")
server: BoundServer = response.server
return server.public_net.ipv4.ip
def down():
client = Client(token=os.getenv("HCLOUD_API_TOKEN"))
servers = client.servers.get_all()
for server in servers:
server.delete()
print(f"Deleted {server}")
server: BoundServer = client.servers.get_by_name("load-testing")
server.delete()
print(f"Deleted {server.name}")
if __name__ == "__main__":
command = sys.argv[1]
if command == "up":
if len(sys.argv) == 3:
up(server_type=sys.argv[2])
else:
up()
try:
start_time = time.time()
if len(sys.argv) == 3:
ip = up(server_type=sys.argv[2])
else:
ip = up()
print(f"Wait for {ip} to become ready")
while not exists_remote(
f"root@{ip}", "/var/lib/cloud/instance/boot-finished"
):
print(".", end="", flush=True)
time.sleep(5)
print("")
end_time = time.time()
print(f"{ip} is up and initialized in {round(end_time - start_time)}s")
except KeyboardInterrupt:
print(f"Abort! Start deleting {ip}")
down()
elif command == "down":
down()
import json
from locust import HttpLocust, TaskSet, between, task
from locust import HttpUser, between, task
# Simple Java Hello World
......@@ -33,13 +33,15 @@ public class HelloWorldTestOk {
"""
class RunnerTaskSet(TaskSet):
class WebsiteRunner(HttpUser):
wait_time = between(5, 8)
@task
def run(self):
runner_data = {
"image": "registry.gitlab.com/openpatch/runner-java:v1.0.0",
"user": "locust",
"timeout": 20,
"timeout": 600,
"payload": {
"sources": [
{
......@@ -55,11 +57,4 @@ class RunnerTaskSet(TaskSet):
],
},
}
response = self.client.post("/api/v1/run", json=runner_data)
payload = json.loads(response.json().get("payload"))
print(payload.get("test_results"))
class WebsiteRunner(HttpLocust):
task_set = RunnerTaskSet
wait_time = between(5, 8)
response = self.client.post("/api/v1/run", json=runner_data)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment