sqldb: fix error comparison and refactor sqlite bench helpers
What changed, and why it matters
This commit only changes benchmark test code in LND. It replaces one style of error checking with another and removes duplicated code in a performance test. There is no change to production code, user-facing behavior, or wallet security.
No security action needed. Treat as routine code-quality/test refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies sqldb/sqlite_bench_test.go. It introduces a helper getInvoiceByHashForBench that uses errors.Is semantics (via testify’s require.ErrorIs) instead of direct err != sql.ErrNoRows comparison, and refactors two SQLite benchmark functions to call that helper. The concurrent benchmark also fixes a classic loop-variable capture bug by passing the hash as a parameter to the goroutine. All changes are confined to test/benchmark code.
Changed components
sqldb/sqlite_bench_test.goInspect captured patch +17 / −17
diff --git a/sqldb/sqlite_bench_test.go b/sqldb/sqlite_bench_test.go
index 168ef96..9a606d5 100644
--- a/sqldb/sqlite_bench_test.go
+++ b/sqldb/sqlite_bench_test.go
@@ -15,6 +15,17 @@ import (
"github.com/stretchr/testify/require"
)
+// getInvoiceByHashForBench fetches an invoice by hash and reports any
+// error except sql.ErrNoRows (expected when the invoice doesn't exist).
+func getInvoiceByHashForBench(b *testing.B, store *SqliteStore,
+ ctx context.Context, hash []byte) {
+
+ _, err := store.GetInvoiceByHash(ctx, hash)
+ if err != nil {
+ require.ErrorIs(b, err, sql.ErrNoRows)
+ }
+}
+
// BenchmarkSqliteMaxConns benchmarks sequential reads against a SQLite
// database with varying MaxConnections settings.
//
@@ -92,11 +103,7 @@ func BenchmarkSqliteMaxConns(b *testing.B) {
for b.Loop() {
hash := hashes[i%numInvoices]
i++
-
- _, err := store.GetInvoiceByHash(ctx, hash)
- if err != nil {
- require.ErrorIs(b, err, sql.ErrNoRows)
- }
+ getInvoiceByHashForBench(b, store, ctx, hash)
}
})
}
@@ -173,20 +180,13 @@ func BenchmarkSqliteMaxConnsConcurrentReads(b *testing.B) {
wg.Add(goroutines)
for g := range goroutines {
- go func() {
+ hash := hashes[g%numInvoices]
+ go func(h []byte) {
defer wg.Done()
-
- hash := hashes[g%numInvoices]
- _, err := store.GetInvoiceByHash(
- ctx, hash,
+ getInvoiceByHashForBench(
+ b, store, ctx, h,
)
- if err != nil &&
- err != sql.ErrNoRows {
-
- b.Errorf("GetInvoice:"+
- " %v", err)
- }
- }()
+ }(hash)
}
wg.Wait()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.