go test -v -run=XXX -bench=BenchmarkIterate -benchtime=3s Benchmark with opt.Prefix set === goos: linux goarch: amd64 pkg: github.com/dgraph-io/badger BenchmarkIteratePrefixSingleKey/Key_lookups-4 10000 365539 ns/op --- BENCH: BenchmarkIteratePrefixSingleKey/Key_lookups-4 iterator_t
(b *testing.B)
| 269 | // |
| 270 | // Only my laptop there's a 20% improvement in latency with ~80 files. |
| 271 | func BenchmarkIteratePrefixSingleKey(b *testing.B) { |
| 272 | dir, err := ioutil.TempDir(".", "badger-test") |
| 273 | y.Check(err) |
| 274 | defer removeDir(dir) |
| 275 | opts := getTestOptions(dir) |
| 276 | opts.TableLoadingMode = options.LoadToRAM |
| 277 | db, err := Open(opts) |
| 278 | y.Check(err) |
| 279 | defer db.Close() |
| 280 | |
| 281 | N := 100000 // Should generate around 80 SSTables. |
| 282 | val := []byte("OK") |
| 283 | bkey := func(i int) []byte { |
| 284 | return []byte(fmt.Sprintf("%06d", i)) |
| 285 | } |
| 286 | |
| 287 | batch := db.NewWriteBatch() |
| 288 | for i := 0; i < N; i++ { |
| 289 | y.Check(batch.Set(bkey(i), val)) |
| 290 | } |
| 291 | y.Check(batch.Flush()) |
| 292 | var lsmFiles int |
| 293 | err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 294 | if strings.HasSuffix(path, ".sst") { |
| 295 | lsmFiles++ |
| 296 | } |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | return nil |
| 301 | }) |
| 302 | y.Check(err) |
| 303 | b.Logf("LSM files: %d", lsmFiles) |
| 304 | b.Logf("Key splits: %v", db.KeySplits(nil)) |
| 305 | b.Logf("Key splits with prefix: %v", db.KeySplits([]byte("09"))) |
| 306 | |
| 307 | b.Logf("Outer b.N: %d", b.N) |
| 308 | b.Run("Key lookups", func(b *testing.B) { |
| 309 | b.Logf("Inner b.N: %d", b.N) |
| 310 | for i := 0; i < b.N; i++ { |
| 311 | key := bkey(rand.Intn(N)) |
| 312 | err := db.View(func(txn *Txn) error { |
| 313 | opt := DefaultIteratorOptions |
| 314 | // NOTE: Comment opt.Prefix out here to compare the performance |
| 315 | // difference between providing Prefix as an option, v/s not. I |
| 316 | // see a 20% improvement when there are ~80 SSTables. |
| 317 | opt.Prefix = key |
| 318 | opt.AllVersions = true |
| 319 | |
| 320 | itr := txn.NewIterator(opt) |
| 321 | defer itr.Close() |
| 322 | |
| 323 | var count int |
| 324 | for itr.Seek(key); itr.ValidForPrefix(key); itr.Next() { |
| 325 | count++ |
| 326 | } |
| 327 | if count != 1 { |
| 328 | b.Fatalf("Count must be one key: %s. Found: %d", key, count) |
nothing calls this directly
no test coverage detected
searching dependent graphs…