Skip to content

Commit

Permalink
Improve speed test some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Jan 25, 2024
1 parent fa2b9f8 commit efb9358
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
1 change: 1 addition & 0 deletions integration-tests/speed-test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ speed-test-test-*
*.o
*.obj
*.lst
logs/
73 changes: 47 additions & 26 deletions integration-tests/speed-test/source/app.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import slf4d;
import slf4d.provider;
import slf4d.writer;
import slf4d.default_provider;
import handy_httpd;

Expand All @@ -8,40 +10,59 @@ import requester;
import test;

int main() {
auto prov = new shared DefaultProvider(true, Levels.INFO);
prov.getLoggerFactory().setModuleLevelPrefix("handy_httpd", Levels.WARN);
prov.getLoggerFactory().setModuleLevelPrefix("requester-", Levels.INFO);
auto prov = new shared FilesOnlyLoggingProvider();
prov.loggerFactory.setModuleLevelPrefix("handy_httpd", Levels.WARN);
prov.loggerFactory.setModuleLevelPrefix("requester-", Levels.INFO);
configureLoggingProvider(prov);

SpeedTest singleThreadTest = new SpeedTest(
getTestingServer(1),
1,
LimitType.Time,
10_000
);
bool singleThreadTestSuccess = singleThreadTest.run();
const cpuThreads = threadsPerCPU();

SpeedTest balancedThreadTest = new SpeedTest(
getTestingServer(threadsPerCPU),
threadsPerCPU / 2,
LimitType.Time,
10_000
return runTests(
() => new SpeedTest("Single-Thread BlockingWorkerPool", getBlockingServer(), 4, LimitType.RequestCount, 10_000),
() => new SpeedTest("Single-Thread DistributingWorkerPool", getTestingServer(1), 1, LimitType.Time, 10_000),
() => new SpeedTest("Multi-Thread DistributingWorkerPool", getTestingServer(cpuThreads), cpuThreads / 2, LimitType.Time, 10_000)
);
bool balancedThreadTestSuccess = balancedThreadTest.run();
}

int runTests(SpeedTest delegate()[] tests...) {
bool[] results = new bool[tests.length];
foreach (i, test; tests) {
results[i] = test().run();
}
foreach (result; results) {
if (!result) return 1;
}
return 0;
}

return singleThreadTestSuccess && balancedThreadTestSuccess ? 0 : 1;
HttpServer getBlockingServer() {
import handy_httpd.components.worker_pool;
ServerConfig config = ServerConfig.defaultValues();
return new HttpServer(toHandler(&handlerFunction), new BlockingWorkerPool(1024), config);
}

HttpServer getTestingServer(uint workerPoolSize) {
ServerConfig config = ServerConfig.defaultValues();
config.workerPoolSize = workerPoolSize;
config.enableWebSockets = false;
config.connectionQueueSize = 1000;
config.receiveBufferSize = 1024;
infoF!"Starting testing server with %d workers."(config.workerPoolSize);
config.port = 8080;

return new HttpServer((ref ctx) {
ctx.response.writeBodyString("Testing server");
}, config);
return new HttpServer(&handlerFunction, config);
}

void handlerFunction(ref HttpRequestContext ctx) {
ctx.response.writeBodyString("Testing server");
}

class FilesOnlyLoggingProvider : LoggingProvider {
private shared DefaultLoggerFactory loggerFactory;

public shared this() {
auto handler = new shared SerializingLogHandler(
new DefaultStringLogSerializer(false),
new RotatingFileLogWriter("logs")
);
this.loggerFactory = new shared DefaultLoggerFactory(handler, Levels.INFO);
}

public shared shared(DefaultLoggerFactory) getLoggerFactory() {
return this.loggerFactory;
}
}
9 changes: 6 additions & 3 deletions integration-tests/speed-test/source/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import requester;
* spawn some threads that each send requests to the server.
*/
class SpeedTest {
private string name;
private RequesterThread[] requesters;
private HttpServer server;
private Thread serverThread;

this(HttpServer server, uint requesterCount, LimitType limitType, ulong limit) {
this(string name, HttpServer server, uint requesterCount, LimitType limitType, ulong limit) {
this.name = name;
this.server = server;
for (uint i = 0; i < requesterCount; i++) {
requesters ~= new RequesterThread(i, limitType, limit);
Expand Down Expand Up @@ -60,8 +62,8 @@ class SpeedTest {
const double testDurationMs = testDuration.total!"msecs";
double requestsPerSecond = successfulRequests / testDurationMs * 1000.0;

writeln("Test Results");
writeln("------------");
writeln("Test Results: " ~ name);
writeln("----------------------------------------------------------------");
writefln!"%d requester threads.\n%d server worker threads.\n%d CPU threads.\n"(
requesters.length,
server.config.workerPoolSize,
Expand All @@ -73,6 +75,7 @@ class SpeedTest {
successRate
);
writefln!"%.1f requests per second."(requestsPerSecond);
writeln();
return successRate > 0.9999;
}
}
2 changes: 1 addition & 1 deletion source/handy_httpd/components/distributing_worker_pool.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DistributingWorkerPool : RequestWorkerPool {
uint attempts = 0;
while (true) {
if (lastWorkerIdx >= workers.length) lastWorkerIdx = 0;
if (attempts % 100 == 0) {
if (attempts > 0 && attempts % 1000 == 0) {
warnF!"Failed to submit socket to a worker in %d attempts."(attempts);
}
Worker worker = workers[lastWorkerIdx++];
Expand Down

0 comments on commit efb9358

Please sign in to comment.