-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Optimize BackupManager cleanupOldBackups performance #13
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
base: re-write
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.memoryvault | ||
|
|
||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Test | ||
| import java.io.File | ||
| import kotlin.system.measureTimeMillis | ||
|
|
||
| class BackupManagerBenchmark { | ||
|
|
||
| @Test | ||
| fun benchmarkCleanup() { | ||
| runBlocking { | ||
| val tempDir = File(System.getProperty("java.io.tmpdir"), "backup_benchmark") | ||
| tempDir.mkdirs() | ||
|
|
||
| // Create 30000 dummy backup files | ||
| for (i in 1..30000) { | ||
| val file = File(tempDir, "vault_backup_$i.mvlt.gz") | ||
| file.createNewFile() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The benchmark is good, but all created files will have very similar file.createNewFile()
file.setLastModified(System.currentTimeMillis() - i * 1000L) |
||
| } | ||
|
Comment on lines
+10
to
+20
|
||
|
|
||
| val manager = BackupManager(File("dummy")) | ||
|
|
||
| val time = measureTimeMillis { | ||
| manager.cleanupOldBackups(tempDir, 10) | ||
| } | ||
|
|
||
| // Print to standard error as it might bypass gradle's output buffering | ||
| System.err.println("Baseline Cleanup Time: $time ms") | ||
| tempDir.deleteRecursively() | ||
|
Comment on lines
+7
to
+30
|
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of this PR is performance optimization, but this sorting method is inefficient.
sortedByDescendingmay callit.lastModified()multiple times per file, which is a system call. To truly optimize this, you should cache thelastModifiedvalue before sorting, as mentioned in the PR description. This ensureslastModified()is called only once per file.