Skip to content

Commit

Permalink
socket-test.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Oct 13, 2024
1 parent 140f8a2 commit 3ea4556
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ funcs-test: src/funcs-test.cpp funcs utils quants
$(CXX) $(CXXFLAGS) src/funcs-test.cpp -o funcs-test funcs.o utils.o quants.o $(LIBS)
quants-test: src/quants.cpp utils quants
$(CXX) $(CXXFLAGS) src/quants-test.cpp -o quants-test utils.o quants.o $(LIBS)
socket-test: src/socket-test.cpp socket
$(CXX) $(CXXFLAGS) src/socket-test.cpp -o socket-test socket.o $(LIBS)
tokenizer-test: src/tokenizer-test.cpp tokenizer funcs commands utils quants
$(CXX) $(CXXFLAGS) src/tokenizer-test.cpp -o tokenizer-test tokenizer.o funcs.o commands.o utils.o quants.o $(LIBS)
commands-test: src/commands-test.cpp funcs commands utils quants transformer socket
Expand Down
75 changes: 75 additions & 0 deletions src/socket-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "./socket.hpp"
#include <chrono>
using namespace std::chrono;

unsigned int packageSizes[] = { 128, 256, 512, 768, 1024, 1280, 1518, 2048, 4096, 8192, 16384, 32768, 65536 };
unsigned int packageSizesCount = sizeof(packageSizes) / sizeof(unsigned int);
unsigned int maPackageSize = packageSizes[packageSizesCount - 1];
unsigned int nAttempts = 1000;
int port = 7721;

void server() {
printf("nAttempts: %d\n", nAttempts);
SocketServer server(port);
Socket socket = server.accept();
char buffer[maPackageSize];

for (long i = 0; i < packageSizesCount; i++) {
unsigned int currentPackageSize = packageSizes[i];

long long totalReadTime = 0;
long long totalWriteTime = 0;
long long totalTime = 0; // [us]
for (long a = 0; a < nAttempts; a++) {
auto t0 = high_resolution_clock::now();
socket.read(buffer, currentPackageSize);
auto t1 = high_resolution_clock::now();
socket.write(buffer, currentPackageSize);
auto t2 = high_resolution_clock::now();

totalReadTime += duration_cast<microseconds>(t1 - t0).count();
totalWriteTime += duration_cast<microseconds>(t2 - t1).count();
totalTime += duration_cast<microseconds>(t2 - t0).count();
}

double nPingPongs = 1000.0 / ((totalTime / 1000.0) / (double) nAttempts);
printf("[%6d bytes] write: %5lld us, read: %5lld us, total: %5lld us, nPingPongs: %.2f\n",
currentPackageSize, totalReadTime, totalWriteTime, totalTime, nPingPongs);
}
}

void client(char* host) {
char** hosts = new char*[1];
hosts[0] = host;
int* ports = new int[1];
ports[0] = port;

SocketPool* pool = SocketPool::connect(1, hosts, ports);
char buffer[maPackageSize];
pool->setTurbo(true);


for (long i = 0; i < packageSizesCount; i++) {
unsigned int currentPackageSize = packageSizes[i];

for (long a = 0; a < nAttempts; a++) {
pool->write(0, buffer, currentPackageSize);
pool->read(0, buffer, currentPackageSize);
}
}

delete pool;
delete[] hosts;
delete[] ports;
}

int main(int argc, char *argv[]) {
initSockets();
if (argc > 1 && strcmp(argv[1], "server") == 0) {
server();
} else if (argc > 2 && strcmp(argv[1], "client") == 0) {
client(argv[2]);
} else {
printf("Invalid arguments\n");
}
}

0 comments on commit 3ea4556

Please sign in to comment.