Skip to content

Instantly share code, notes, and snippets.

@mxpv
Created June 20, 2022 03:12
Show Gist options
  • Save mxpv/8811a208ebee32af5e0fe8155a751f10 to your computer and use it in GitHub Desktop.
Save mxpv/8811a208ebee32af5e0fe8155a751f10 to your computer and use it in GitHub Desktop.
func TestBenchmarkListImages(t *testing.T) {
tmpDir := t.TempDir()
specFile := filepath.Join(tmpDir, "1.json")
testSpec := imagespec.Image{
Author: "1",
Architecture: "2",
Variant: "3",
OS: "4",
Config: imagespec.ImageConfig{
User: "c",
Entrypoint: []string{"c", "d", "d"},
Cmd: []string{"a", "b", "c"},
WorkingDir: "b",
StopSignal: "a",
},
RootFS: imagespec.RootFS{
Type: "123",
},
}
data, err := json.Marshal(&testSpec)
require.NoError(t, err)
err = os.WriteFile(specFile, data, 0777)
require.NoError(t, err)
// Simulate reading of image spec from disk
getImageSpec = func(ctx context.Context, image containerd.Image) (imagespec.Image, error) {
data, err := os.ReadFile(specFile)
if err != nil {
return imagespec.Image{}, err
}
var out imagespec.Image
err = json.Unmarshal(data, &out)
if err != nil {
return imagespec.Image{}, err
}
return out, nil
}
t.Cleanup(func() { getImageSpec = retrieveImageSpec })
var (
ctx, db = makeTestDB(t)
imageStore = metadata.NewImageStore(db)
c = newTestCRIService(containerd.WithImageStore(imageStore))
buffer = make([]byte, 10)
)
const count = 30000
// Add image records, 3 references for each image.
for i := 0; i < count; i++ {
if i%10 == 0 {
t.Logf("Adding: %d, Progress: %d", i, i*100/count)
}
_, err := rand.Read(buffer)
assert.NoError(t, err)
name := digest.NewDigestFromBytes(digest.SHA256, buffer)
image1 := images.Image{
Name: name.String(),
Target: fakeTarget,
Labels: map[string]string{
imageLabelConfigDigest: name.String(),
imageLabelSize: "1000",
imageLabelKey: imageLabelValue,
},
}
image2 := images.Image{
Name: "gcr.io/library/busybox@" + name.String(),
Target: fakeTarget,
Labels: map[string]string{
imageLabelConfigDigest: name.String(),
imageLabelSize: "1000",
imageLabelKey: imageLabelValue,
},
}
image3 := images.Image{
Name: "gcr.io/library/busybox:" + name.String(),
Target: fakeTarget,
Labels: map[string]string{
imageLabelConfigDigest: name.String(),
imageLabelSize: "1000",
imageLabelKey: imageLabelValue,
},
}
_, err = imageStore.Create(ctx, image1)
require.NoError(t, err)
_, err = imageStore.Create(ctx, image2)
require.NoError(t, err)
_, err = imageStore.Create(ctx, image3)
require.NoError(t, err)
}
start := time.Now()
_, err = c.ListImages(ctx, &runtime.ListImagesRequest{})
assert.NoError(t, err)
duration := time.Since(start)
t.Logf("Execution time: %s", duration)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment