github.com/quay/claircore@v1.5.28/datastore/postgres/indexpackage_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_IndexPackages(b *testing.B) {
    16  	integration.NeedDB(b)
    17  	ctx := context.Background()
    18  	benchmarks := []struct {
    19  		// the name of this benchmark
    20  		name string
    21  		// number of packages to index.
    22  		pkgs int
    23  		// the layer that holds the discovered packages
    24  		layer *claircore.Layer
    25  		// whether the generated package array contains duplicate packages
    26  		duplicates bool
    27  	}{
    28  		{
    29  			name: "10 packages",
    30  			pkgs: 10,
    31  			layer: &claircore.Layer{
    32  				Hash: test.RandomSHA256Digest(b),
    33  			},
    34  		},
    35  		{
    36  			name: "10 packages with duplicates",
    37  			pkgs: 10,
    38  			layer: &claircore.Layer{
    39  				Hash: test.RandomSHA256Digest(b),
    40  			},
    41  			duplicates: true,
    42  		},
    43  		{
    44  			name: "50 packages",
    45  			pkgs: 50,
    46  			layer: &claircore.Layer{
    47  				Hash: test.RandomSHA256Digest(b),
    48  			},
    49  		},
    50  		{
    51  			name: "50 packages with duplicates",
    52  			pkgs: 50,
    53  			layer: &claircore.Layer{
    54  				Hash: test.RandomSHA256Digest(b),
    55  			},
    56  			duplicates: true,
    57  		},
    58  		{
    59  			name: "100 packages",
    60  			pkgs: 100,
    61  			layer: &claircore.Layer{
    62  				Hash: test.RandomSHA256Digest(b),
    63  			},
    64  		},
    65  		{
    66  			name: "100 packages with duplicates",
    67  			pkgs: 100,
    68  			layer: &claircore.Layer{
    69  				Hash: test.RandomSHA256Digest(b),
    70  			},
    71  			duplicates: true,
    72  		},
    73  		{
    74  			name: "250 packages",
    75  			pkgs: 250,
    76  			layer: &claircore.Layer{
    77  				Hash: test.RandomSHA256Digest(b),
    78  			},
    79  		},
    80  		{
    81  			name: "250 packages",
    82  			pkgs: 250,
    83  			layer: &claircore.Layer{
    84  				Hash: test.RandomSHA256Digest(b),
    85  			},
    86  			duplicates: true,
    87  		},
    88  		{
    89  			name: "500 packages",
    90  			pkgs: 500,
    91  			layer: &claircore.Layer{
    92  				Hash: test.RandomSHA256Digest(b),
    93  			},
    94  		},
    95  		{
    96  			name: "500 packages with duplicates",
    97  			pkgs: 500,
    98  			layer: &claircore.Layer{
    99  				Hash: test.RandomSHA256Digest(b),
   100  			},
   101  			duplicates: true,
   102  		},
   103  		{
   104  			name: "1000 packages",
   105  			pkgs: 1000,
   106  			layer: &claircore.Layer{
   107  				Hash: test.RandomSHA256Digest(b),
   108  			},
   109  		},
   110  		{
   111  			name: "1000 packages with duplicates",
   112  			pkgs: 1000,
   113  			layer: &claircore.Layer{
   114  				Hash: test.RandomSHA256Digest(b),
   115  			},
   116  			duplicates: true,
   117  		},
   118  		{
   119  			name: "2000 packages",
   120  			pkgs: 2000,
   121  			layer: &claircore.Layer{
   122  				Hash: test.RandomSHA256Digest(b),
   123  			},
   124  		},
   125  		{
   126  			name: "2000 packages with duplicates",
   127  			pkgs: 2000,
   128  			layer: &claircore.Layer{
   129  				Hash: test.RandomSHA256Digest(b),
   130  			},
   131  			duplicates: true,
   132  		},
   133  		{
   134  			name: "3000 packages",
   135  			pkgs: 3000,
   136  			layer: &claircore.Layer{
   137  				Hash: test.RandomSHA256Digest(b),
   138  			},
   139  		},
   140  		{
   141  			name: "3000 packages with duplicates",
   142  			pkgs: 3000,
   143  			layer: &claircore.Layer{
   144  				Hash: test.RandomSHA256Digest(b),
   145  			},
   146  			duplicates: true,
   147  		},
   148  		{
   149  			name: "4000 packages",
   150  			pkgs: 4000,
   151  			layer: &claircore.Layer{
   152  				Hash: test.RandomSHA256Digest(b),
   153  			},
   154  		},
   155  		{
   156  			name: "4000 packages with duplicates",
   157  			pkgs: 4000,
   158  			layer: &claircore.Layer{
   159  				Hash: test.RandomSHA256Digest(b),
   160  			},
   161  			duplicates: true,
   162  		},
   163  		{
   164  			name: "5000 packages",
   165  			pkgs: 5000,
   166  			layer: &claircore.Layer{
   167  				Hash: test.RandomSHA256Digest(b),
   168  			},
   169  		},
   170  		{
   171  			name: "5000 packages with duplicates",
   172  			pkgs: 5000,
   173  			layer: &claircore.Layer{
   174  				Hash: test.RandomSHA256Digest(b),
   175  			},
   176  			duplicates: true,
   177  		},
   178  	}
   179  
   180  	for _, bench := range benchmarks {
   181  		b.Run(bench.name, func(b *testing.B) {
   182  			ctx := zlog.Test(ctx, b)
   183  			pool := pgtest.TestIndexerDB(ctx, b)
   184  			store := NewIndexerStore(pool)
   185  
   186  			// gen a scnr and insert
   187  			vscnrs := test.GenUniquePackageScanners(1)
   188  			err := pgtest.InsertUniqueScanners(ctx, pool, vscnrs)
   189  
   190  			// gen packages
   191  			var pkgs []*claircore.Package
   192  			if bench.duplicates {
   193  				pkgs, err = test.GenDuplicatePackages(bench.pkgs)
   194  				if err != nil {
   195  					b.Fatalf("failed to generate duplicate packages: %v", err)
   196  				}
   197  			} else {
   198  				pkgs = test.GenUniquePackages(bench.pkgs)
   199  			}
   200  
   201  			// insert layer
   202  			insertLayer := `INSERT INTO layer (hash) VALUES ($1);`
   203  			_, err = pool.Exec(ctx, insertLayer, bench.layer.Hash)
   204  			if err != nil {
   205  				b.Fatalf("failed to insert test layer: %v", err)
   206  			}
   207  
   208  			b.ResetTimer()
   209  			for i := 0; i < b.N; i++ {
   210  				// run the indexing
   211  				err = store.IndexPackages(ctx, pkgs, bench.layer, vscnrs[0])
   212  				if err != nil {
   213  					b.Fatalf("failed to index packages: %v", err)
   214  				}
   215  			}
   216  		})
   217  	}
   218  
   219  }