Log management and rotation in Jenkins - Time & Space Complexity
When Jenkins manages and rotates logs, it processes log files to keep them from growing too large. We want to understand how the time it takes grows as the number or size of logs increases.
How does Jenkins handle more logs or bigger logs affect the time it spends on rotation?
Analyze the time complexity of the following Jenkins pipeline snippet for log rotation.
pipeline {
agent any
stages {
stage('Rotate Logs') {
steps {
script {
def logs = findFiles(glob: '**/*.log')
for (log in logs) {
if (log.length > 10485760) { // 10MB
sh "mv ${log.path} ${log.path}.old"
}
}
}
}
}
}
}
This code finds all log files and moves those larger than 10MB to an old file, simulating log rotation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over all log files found by
findFiles. - How many times: Once for each log file in the workspace.
As the number of log files increases, the script checks each one once. The time grows directly with the number of logs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 logs | 10 checks and possible moves |
| 100 logs | 100 checks and possible moves |
| 1000 logs | 1000 checks and possible moves |
Pattern observation: The time increases steadily as more logs are present, roughly one operation per log file.
Time Complexity: O(n)
This means the time to rotate logs grows linearly with the number of log files Jenkins processes.
[X] Wrong: "The time to rotate logs depends on the size of each log file."
[OK] Correct: The script only checks the size and moves files if needed, but it does not read the entire file content. So the size check is quick, and the main time depends on how many files there are, not their size.
Understanding how log rotation scales helps you design pipelines that handle many logs efficiently. This skill shows you can think about how tasks grow with data size, a key part of DevOps work.
"What if the script compressed each log file instead of just moving it? How would the time complexity change?"