From f4b766e86d070acec4f45da61ab7c0a73333826c Mon Sep 17 00:00:00 2001 From: sentriz Date: Thu, 4 Jul 2019 14:08:44 +0100 Subject: [PATCH] add scanner benchmarking --- scanner/scanner_test.go | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 scanner/scanner_test.go diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go new file mode 100644 index 0000000..be9d653 --- /dev/null +++ b/scanner/scanner_test.go @@ -0,0 +1,66 @@ +package scanner + +import ( + "io/ioutil" + "log" + "testing" + + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" + + "github.com/sentriz/gonic/db" +) + +var testScanner *Scanner + +func init() { + db, err := db.New(":memory:") + if err != nil { + log.Fatalf("error opening database: %v\n", err) + } + // benchmarks aren't real code are they? >:) + // here is an absolute path to my music directory + testScanner = New(db, "/home/senan/music") + log.SetOutput(ioutil.Discard) +} + +func resetTables(db *gorm.DB) { + tx := db.Begin() + defer tx.Commit() + tx.Exec("delete from tracks") + tx.Exec("delete from artists") + tx.Exec("delete from albums") +} + +func resetTablesPause(db *gorm.DB, b *testing.B) { + b.StopTimer() + defer b.StartTimer() + resetTables(db) +} + +func BenchmarkScanFresh(b *testing.B) { + for n := 0; n < b.N; n++ { + resetTablesPause(testScanner.db, b) + testScanner.Start() + } +} + +func BenchmarkScanIncremental(b *testing.B) { + // do a full scan and reset + testScanner.Start() + b.ResetTimer() + // do the inc scans + for n := 0; n < b.N; n++ { + testScanner.Start() + } +} + +// RESULTS fresh +// 20 times / 1.436 +// 20 times / 1.39 + +// RESULTS inc +// 100 times / 1.86 +// 100 times / 1.9 +// 100 times / 1.5 +// 100 times / 1.48