Skip to content

Commit

Permalink
Update MultiplyBy2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
LennoxLiu committed Apr 16, 2024
1 parent e85094f commit 67166b7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
26 changes: 26 additions & 0 deletions hpc/test/MultiplyBy2/Makefile
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions hpc/test/MultiplyBy2/client.sh
Original file line number Diff line number Diff line change
@@ -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
37 changes: 36 additions & 1 deletion hpc/test/MultiplyBy2/minimal-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@
#include <string>
#include <chrono>
#include <thread>

#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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:
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 67166b7

Please sign in to comment.