Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test (and fix) memory leaks with logger #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions frameos/src/frameos/metrics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(),
})
Expand Down
67 changes: 65 additions & 2 deletions frameos/src/frameos/tests/test_logger.nim
Original file line number Diff line number Diff line change
@@ -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

Loading