33import time
44from typing import List , Dict
55import datetime
6+ import sys
7+
8+ MEMORY_LEAK_DETECTION_THRESHOLD = 1.05
69
710def stability_test (fps ):
811 # Creates the pipeline and a default device implicitly
@@ -13,12 +16,12 @@ def stability_test(fps):
1316 monoRight = p .create (dai .node .Camera ).build (dai .CameraBoardSocket .CAM_C )
1417 stereo = p .create (dai .node .StereoDepth )
1518 spatialDetectionNetwork = p .create (dai .node .SpatialDetectionNetwork ).build (camRgb , stereo , "yolov6-nano" , fps = fps )
16- # encoderMjpeg = p.create(dai.node.VideoEncoder)
17- # fullResCameraOutput = camRgb.requestFullResolutionOutput()
18- # encoderMjpeg.build(fullResCameraOutput, profile=dai.VideoEncoderProperties.Profile.MJPEG)
19+ encoderMjpeg = p .create (dai .node .VideoEncoder )
20+ fullResCameraOutput = camRgb .requestFullResolutionOutput ()
21+ encoderMjpeg .build (fullResCameraOutput , profile = dai .VideoEncoderProperties .Profile .MJPEG )
1922
20- # encoderH264 = p.create(dai.node.VideoEncoder)
21- # encoderH264.build(fullResCameraOutput, profile=dai.VideoEncoderProperties.Profile.H264_MAIN)
23+ encoderH264 = p .create (dai .node .VideoEncoder )
24+ encoderH264 .build (fullResCameraOutput , profile = dai .VideoEncoderProperties .Profile .H264_MAIN )
2225
2326 # Stereo settings
2427 stereo .setExtendedDisparity (True )
@@ -40,17 +43,17 @@ def stability_test(fps):
4043 spatialDetectionNetwork .passthroughDepth .link (spatialBenchmark .input )
4144 benchmarkReportQueues ["spatial" ] = spatialBenchmark .report .createOutputQueue (blocking = False )
4245
43- # H264Benchmark = p.create(dai.node.BenchmarkIn)
44- # H264Benchmark.logReportsAsWarnings(False)
45- # H264Benchmark.sendReportEveryNMessages(fps*5)
46- # benchmarkReportQueues["H264"] = H264Benchmark.report.createOutputQueue(blocking=False)
47- # encoderH264.out.link(H264Benchmark.input)
46+ H264Benchmark = p .create (dai .node .BenchmarkIn )
47+ H264Benchmark .logReportsAsWarnings (False )
48+ H264Benchmark .sendReportEveryNMessages (fps * 5 )
49+ benchmarkReportQueues ["H264" ] = H264Benchmark .report .createOutputQueue (blocking = False )
50+ encoderH264 .out .link (H264Benchmark .input )
4851
49- # MJPEGBenchmark = p.create(dai.node.BenchmarkIn)
50- # MJPEGBenchmark.logReportsAsWarnings(False)
51- # MJPEGBenchmark.sendReportEveryNMessages(fps*5)
52- # # benchmarkReportQueues["MJPEG"] = MJPEGBenchmark.report.createOutputQueue(blocking=False)
53- # # encoderMjpeg.out.link(MJPEGBenchmark.input)
52+ MJPEGBenchmark = p .create (dai .node .BenchmarkIn )
53+ MJPEGBenchmark .logReportsAsWarnings (False )
54+ MJPEGBenchmark .sendReportEveryNMessages (fps * 5 )
55+ benchmarkReportQueues ["MJPEG" ] = MJPEGBenchmark .report .createOutputQueue (blocking = False )
56+ encoderMjpeg .out .link (MJPEGBenchmark .input )
5457
5558 # IMU
5659 imu = p .create (dai .node .IMU )
@@ -66,6 +69,11 @@ def stability_test(fps):
6669 print ("Starting the stability test..." )
6770 tStart = time .time ()
6871 p .start ()
72+
73+ # Delay so the device process finishes starting up
74+ time .sleep (10 )
75+ initialProcessMemoryUsage = p .getDefaultDevice ().getProcessMemoryUsage ()
76+ print (f"Initial depthai-device process memory usage is: { initialProcessMemoryUsage } kB" )
6977 while True :
7078 for name , queue in benchmarkReportQueues .items ():
7179 report = queue .get (timeout = datetime .timedelta (minutes = 1 )) # 1 minute timeout
@@ -77,6 +85,12 @@ def stability_test(fps):
7785 else :
7886 raise RuntimeError (f"Timeout reached for { name } benchmark report" )
7987 queue .tryGetAll () # Clear the queue
88+
89+ # Detect memory leaks
90+ processMemoryUsage = p .getDefaultDevice ().getProcessMemoryUsage ()
91+ if processMemoryUsage > MEMORY_LEAK_DETECTION_THRESHOLD * initialProcessMemoryUsage :
92+ raise RuntimeError ("Memory used by depthai-device process increased above the given threshold - potential memory leak detected" )
93+ print (f"Memory used by depthai-device process: { processMemoryUsage } kB. Current time: { time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime ())} " )
8094 print (f"Running for { datetime .timedelta (seconds = time .time () - tStart )} " , flush = True )
8195
8296if __name__ == "__main__" :
0 commit comments