From 67166b7d83763e00d92f640a77dc2e5c8441ae8e Mon Sep 17 00:00:00 2001 From: Lennox <105095085+LennoxLiu@users.noreply.github.com> Date: Tue, 16 Apr 2024 10:37:11 +0200 Subject: [PATCH] Update MultiplyBy2 test --- hpc/test/MultiplyBy2/Makefile | 26 +++++++++++++++++ hpc/test/MultiplyBy2/client.sh | 31 +++++++++++++++++++++ hpc/test/MultiplyBy2/minimal-server.cpp | 37 ++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 hpc/test/MultiplyBy2/Makefile create mode 100644 hpc/test/MultiplyBy2/client.sh diff --git a/hpc/test/MultiplyBy2/Makefile b/hpc/test/MultiplyBy2/Makefile new file mode 100644 index 0000000..8f19ff9 --- /dev/null +++ b/hpc/test/MultiplyBy2/Makefile @@ -0,0 +1,26 @@ +all: build-server build-lb run + +load-balancer-files = ../../LoadBalancer.cpp ../../LoadBalancer.hpp ../../../lib/httplib.h ../../../lib/json.hpp ../../../lib/umbridge.h + +build-server: + g++ -O3 -w -std=c++11 minimal-server.cpp -o server -lssl -lcrypto -pthread + +build-lb: + g++ -O3 -Wno-unused-result -std=c++17 $(load-balancer-files) -o ../../load-balancer -pthread + +run: + rm -f retry-port-job_id.txt + rm -f retry-respond-job_id.txt + mkdir -p logs + rm -f logs/* + + if ! printenv PORT > /dev/null; then \ + echo "PORT environment variable not set. Using default value 4242."; \ + export PORT=4242; \ + fi && \ + export HQ_SUBMIT_DELAY_MS=100 && \ + while nc -z localhost $$PORT; do \ + read -p "Port $$PORT is already in use. Please enter a different port: " NEW_PORT; \ + PORT=$${NEW_PORT:-$$PORT}; \ + done; \ + cd ../../ && ./load-balancer \ No newline at end of file diff --git a/hpc/test/MultiplyBy2/client.sh b/hpc/test/MultiplyBy2/client.sh new file mode 100644 index 0000000..3b714f7 --- /dev/null +++ b/hpc/test/MultiplyBy2/client.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# export TEST_DELAY=1e4 + +if [ -z "$PORT" ]; then + PORT="4242" + fi + +echo "Using URL http://localhost:$PORT" + +echo "Sending requests..." + +for i in {1..300} +do + # Expected output: {"output":[[200.0]]} + # Check if curl output equals expected output + # If not, print error message + + if [ "$(curl -s http://localhost:$PORT/Evaluate -X POST -d '{"name": "forward", "input": [[100.0]]}')" == '{"output":[[200.0]]}' ]; then + echo -n "y" + else + echo $(curl -s http://localhost:$PORT/Evaluate -X POST -d '{"name": "forward", "input": [[100.0]]}') + echo -n "n" + #echo "Error: curl output does not equal expected output" + fi & + +done + +echo "Requests sent. Waiting for responses..." + +wait \ No newline at end of file diff --git a/hpc/test/MultiplyBy2/minimal-server.cpp b/hpc/test/MultiplyBy2/minimal-server.cpp index 156dda8..2505713 100644 --- a/hpc/test/MultiplyBy2/minimal-server.cpp +++ b/hpc/test/MultiplyBy2/minimal-server.cpp @@ -2,9 +2,36 @@ #include #include #include - +#include +#include +#include +#include +#include #include "../../../lib/umbridge.h" + +bool isPortInUse(int port) { + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + std::cerr << "Failed to create socket." << std::endl; + return false; + } + + struct sockaddr_in serverAddress; + memset(&serverAddress, 0, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(port); + + if (bind(sockfd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) { + close(sockfd); + return true; + } + + close(sockfd); + return false; +} + class ExampleModel : public umbridge::Model { public: @@ -79,6 +106,14 @@ int main(int argc, char *argv[]) else { port = atoi(port_cstr); + std::cout << "Using port [ " << port_cstr << " ] as specified by environment variable PORT." << std::endl; + if (isPortInUse(port)) + { + std::cerr << "Port " << port << " is already in use. Exiting." << std::endl; + + exit(-1); + } + } char const *delay_cstr = std::getenv("TEST_DELAY");