基于OpenSSL平均耗时测试逻辑,如何获取其最大内存消耗?
How to Track OpenSSL's Maximum Memory Usage Over 5 Runs
Got it, let's adjust your existing script to measure maximum memory consumption instead of average execution time. Here's a straightforward implementation that follows the same loop-based testing approach:
Modified Script
#!/bin/sh MAX_MEM=0 for i in {1..5} do # Capture maximum resident memory size (in KB) using /usr/bin/time current_mem=$((/usr/bin/time -f '%M' openssl enc -aes-128-ecb -K 1234567890 -in t -out g 1> /dev/null) 2>&1) # Update MAX_MEM if current run used more memory if [ "$current_mem" -gt "$MAX_MEM" ]; then MAX_MEM="$current_mem" fi done echo "Maximum memory usage across 5 runs: $MAX_MEM KB"
Key Changes Explained
- Memory Metric: We swapped the
timeformat flag from%e(elapsed time) to%M, which returns the maximum resident set size — the peak amount of physical memory the process used during its runtime, measured in KB. - Tracking Logic: Instead of summing values for an average, we initialize
MAX_MEMto 0 and update it only when the current run's memory usage exceeds the stored maximum. - Integer Comparison: Since
%Moutputs an integer, we use shell's built-in integer comparison (-gt) instead ofbcfor simplicity.
Notes
- Make sure you're using
/usr/bin/time(the standalone utility) rather than your shell's built-intimecommand — the built-in version may not support the-fformat flag. - The input file
tand output filegshould exist (or adjust the paths) to avoid errors during the OpenSSL execution.
内容的提问来源于stack exchange,提问作者Moser José
相关产品推荐
相关产品推荐

