github.com/quay/claircore@v1.5.28/datastore/postgres/packagesbylayer_benchmark_test.go (about) 1 package postgres 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/quay/zlog" 8 9 "github.com/quay/claircore" 10 "github.com/quay/claircore/test" 11 "github.com/quay/claircore/test/integration" 12 pgtest "github.com/quay/claircore/test/postgres" 13 ) 14 15 func Benchmark_PackagesByLayer(b *testing.B) { 16 integration.NeedDB(b) 17 ctx := context.Background() 18 benchmarks := []struct { 19 name string 20 hash claircore.Digest 21 pkgs int 22 scnrs int 23 }{ 24 { 25 name: "10 package, 5 scanners", 26 hash: test.RandomSHA256Digest(b), 27 pkgs: 10, 28 scnrs: 5, 29 }, 30 { 31 name: "50 packages, 25 scanners", 32 hash: test.RandomSHA256Digest(b), 33 pkgs: 50, 34 scnrs: 25, 35 }, 36 { 37 name: "100 packages, 50 scanners", 38 hash: test.RandomSHA256Digest(b), 39 pkgs: 100, 40 scnrs: 50, 41 }, 42 { 43 name: "500 packages, 250 scanners", 44 hash: test.RandomSHA256Digest(b), 45 pkgs: 500, 46 scnrs: 250, 47 }, 48 { 49 name: "1000 packages, 500 scanners", 50 hash: test.RandomSHA256Digest(b), 51 pkgs: 1000, 52 scnrs: 500, 53 }, 54 { 55 name: "2000 packages, 1000 scanners", 56 hash: test.RandomSHA256Digest(b), 57 pkgs: 2000, 58 scnrs: 1000, 59 }, 60 { 61 name: "3000 packages, 2000 scanners", 62 hash: test.RandomSHA256Digest(b), 63 pkgs: 3000, 64 scnrs: 1000, 65 }, 66 { 67 name: "3000 packages, 500 scanners", 68 hash: test.RandomSHA256Digest(b), 69 pkgs: 3000, 70 scnrs: 500, 71 }, 72 { 73 name: "3000 packages, 250 scanners", 74 hash: test.RandomSHA256Digest(b), 75 pkgs: 3000, 76 scnrs: 250, 77 }, 78 { 79 name: "3000 packages, 50 scanners", 80 hash: test.RandomSHA256Digest(b), 81 pkgs: 2000, 82 scnrs: 50, 83 }, 84 { 85 name: "3000 packages, 10 scanners", 86 hash: test.RandomSHA256Digest(b), 87 pkgs: 2000, 88 scnrs: 10, 89 }, 90 } 91 92 for _, bench := range benchmarks { 93 b.Run(bench.name, func(b *testing.B) { 94 ctx := zlog.Test(ctx, b) 95 pool := pgtest.TestIndexerDB(ctx, b) 96 store := NewIndexerStore(pool) 97 98 // generate a specific number of packages 99 pkgs := test.GenUniquePackages(bench.pkgs) 100 101 // index them into the database 102 err := pgtest.InsertPackages(ctx, pool, pkgs) 103 if err != nil { 104 b.Fatalf("failed to insert packages: %v", err) 105 } 106 107 // create scnr mocks 108 vscnrs := test.GenUniquePackageScanners(bench.scnrs) 109 err = pgtest.InsertUniqueScanners(ctx, pool, vscnrs) 110 if err != nil { 111 b.Fatalf("failed to insert scnrs: %v", err) 112 } 113 114 // create scanartifacts 115 err = pgtest.InsertPackageScanArtifacts(ctx, pool, bench.hash, pkgs, vscnrs) 116 if err != nil { 117 b.Fatalf("failed to insert scan artifacts for test: %v", err) 118 } 119 120 b.ResetTimer() 121 for i := 0; i < b.N; i++ { 122 _, err := store.PackagesByLayer(ctx, bench.hash, vscnrs) 123 if err != nil { 124 b.Fatalf("failed to retrieve packages by layer: %v", err) 125 } 126 } 127 }) 128 } 129 }