From 73895b7f677697f7360341a5d579452524205abb Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Sat, 11 May 2024 02:17:56 +0200 Subject: [PATCH] test logger memory usage --- frameos/src/frameos/metrics.nim | 8 +++ frameos/src/frameos/tests/test_logger.nim | 67 ++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/frameos/src/frameos/metrics.nim b/frameos/src/frameos/metrics.nim index e6b71eef..97d074a2 100644 --- a/frameos/src/frameos/metrics.nim +++ b/frameos/src/frameos/metrics.nim @@ -33,6 +33,13 @@ proc getMemoryUsage(self: MetricsLoggerThread): JsonNode = "active": memoryInfo.active, } +proc getProcessMemory*(): JsonNode = + result = %*{ + "total": system.getTotalMem(), + "occupied": system.getOccupiedMem(), + "free": system.getFreeMem(), + } + proc getCPUUsage(self: MetricsLoggerThread): float = result = psutil.cpuPercent(interval = 1) @@ -49,6 +56,7 @@ proc logMetrics(self: MetricsLoggerThread) = "load": self.getLoadAverage(), "cpuTemperature": self.getCPUTemperature(), "memoryUsage": self.getMemoryUsage(), + "processMemory": getProcessMemory(), "cpuUsage": self.getCPUUsage(), "openFileDescriptors": self.getOpenFileDescriptors(), }) diff --git a/frameos/src/frameos/tests/test_logger.nim b/frameos/src/frameos/tests/test_logger.nim index 2535c02a..cc39c79f 100644 --- a/frameos/src/frameos/tests/test_logger.nim +++ b/frameos/src/frameos/tests/test_logger.nim @@ -1,8 +1,71 @@ +import json, zippy, strutils, jester, os import ../logger import ../config -let testConfig = loadConfig("./frame.json") -block test_basic_things: +block test_memory_usage: + let port = 16667.Port + var requests = 0 + var received = 0 + var thread: Thread[void] + proc createThreadRunner() {.thread.} = + router myrouter: + post "/api/log": + echo "log!" + # echo uncompress(request.body) + let response = parseJson(if request.body == "": "{\"logs\":[]}" else: uncompress(request.body)) + received += response{"logs"}.len + requests += 1 + echo "received: ", received, ", requests: ", requests + resp Http200, "OK" + var jester = initJester(myrouter, newSettings(port = port)) + jester.serve() # blocks forever + createThread(thread, createThreadRunner) + + let testConfig = loadConfig("./frame.json") + testConfig.serverPort = port.int + testConfig.serverHost = "0.0.0.0" + testConfig.debug = false let logger = newLogger(testConfig) doAssert logger.enabled == true + echo testConfig.serverPort + + let memory1 = system.getOccupiedMem() + echo memory1 + + for i in 0..10: + logger.log(%*{"msg": repeat("0123456789", 100)}) # 1kb message + + sleep(500) + + # We only sent the first message + doAssert requests == 1 + doAssert received == 1 + + # We should have 10 logs in the queue + let memory2 = system.getOccupiedMem() + echo memory2 + doAssert memory2 > memory1 + 1000 * 10 + + sleep(1000) + + # We should have sent another 11 messages + doAssert requests == 2 + doAssert received == 11 + + let memory3 = system.getOccupiedMem() + echo memory3 + + doAssert memory3 < memory2 + + # for i in 0..1000: + # logger.log(%*{"msg": repeat("0123456789", 100)}) # 1kb message + + # sleep(1000) + # let memory3 = system.getOccupiedMem() + # echo memory3 + + # echo received + + # doAssert memory2 > memory1 + 1000 * 10 +