-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add benchmark test for scans (#1067)
--------- Signed-off-by: Ivan Milchev <[email protected]>
- Loading branch information
Showing
5 changed files
with
6,768 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: Benchmark main | ||
|
||
## Only trigger tests if source is changing | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- '**.go' | ||
- '**.mod' | ||
- 'go.sum' | ||
|
||
permissions: | ||
# deployments permission to deploy GitHub pages website | ||
deployments: write | ||
# contents permission to update benchmark contents in gh-pages branch | ||
contents: write | ||
|
||
jobs: | ||
go-bench: | ||
runs-on: ubuntu-latest | ||
env: | ||
BRANCH_NAME: ${{ github.head_ref || github.ref_name }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Import environment variables from file | ||
run: cat ".github/env" >> $GITHUB_ENV | ||
- name: Install Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ">=${{ env.golang-version }}" | ||
cache: false | ||
- name: Run benchmark | ||
run: make benchmark/go | tee benchmark.txt | ||
|
||
# Remove log statements and leave just the benchmark results | ||
- name: Cleanup benchmark file | ||
run: sed -i -n '/goos:/,$p' benchmark.txt | ||
|
||
# Download previous benchmark result from cache (if exists) | ||
- name: Download previous benchmark data | ||
uses: actions/cache/restore@v4 | ||
with: | ||
path: ./cache | ||
key: ${{ runner.os }}-benchmark | ||
# Run `github-action-benchmark` action | ||
- name: Store benchmark result | ||
uses: benchmark-action/github-action-benchmark@v1 | ||
with: | ||
# What benchmark tool the output.txt came from | ||
tool: 'go' | ||
# Where the output from the benchmark tool is stored | ||
output-file-path: benchmark.txt | ||
# Where the previous data file is stored | ||
external-data-json-path: ./cache/benchmark-data.json | ||
save-data-file: false | ||
|
||
- name: Download previous benchmark data | ||
uses: actions/cache/restore@v4 | ||
with: | ||
path: ./cache | ||
key: ${{ runner.os }}-benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package benchmark | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.mondoo.com/cnquery/v10" | ||
"go.mondoo.com/cnquery/v10/mqlc" | ||
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory" | ||
"go.mondoo.com/cnquery/v10/providers-sdk/v1/testutils" | ||
"go.mondoo.com/cnspec/v10/policy" | ||
"go.mondoo.com/cnspec/v10/policy/scan" | ||
) | ||
|
||
func init() { | ||
log.Logger = log.Logger.Level(zerolog.Disabled) | ||
zerolog.SetGlobalLevel(zerolog.Disabled) | ||
} | ||
|
||
func BenchmarkScan_SingleAsset(b *testing.B) { | ||
ctx := context.Background() | ||
runtime := testutils.Local() | ||
conf := mqlc.NewConfig(runtime.Schema(), cnquery.DefaultFeatures) | ||
job := &scan.Job{ | ||
Inventory: &inventory.Inventory{ | ||
Spec: &inventory.InventorySpec{ | ||
Assets: []*inventory.Asset{ | ||
{ | ||
Connections: []*inventory.Config{ | ||
{ | ||
Type: "k8s", | ||
Options: map[string]string{ | ||
"path": "../testdata/1pod.yaml", | ||
}, | ||
Discover: &inventory.Discovery{ | ||
Targets: []string{"pods"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
loader := policy.DefaultBundleLoader() | ||
bundle, err := loader.BundleFromPaths("../testdata/kubernetes-security.mql.yaml") | ||
require.NoError(b, err) | ||
|
||
_, err = bundle.CompileExt(context.Background(), policy.BundleCompileConf{ | ||
CompilerConfig: conf, | ||
RemoveFailing: true, | ||
}) | ||
require.NoError(b, err) | ||
|
||
job.Bundle = bundle | ||
|
||
scanner := scan.NewLocalScanner(scan.DisableProgressBar()) | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
res, err := scanner.RunIncognito(ctx, job) | ||
require.NoError(b, err) | ||
require.NotNil(b, res) | ||
} | ||
} | ||
|
||
func BenchmarkScan_MultipleAssets(b *testing.B) { | ||
ctx := context.Background() | ||
runtime := testutils.Local() | ||
conf := mqlc.NewConfig(runtime.Schema(), cnquery.DefaultFeatures) | ||
job := &scan.Job{ | ||
Inventory: &inventory.Inventory{ | ||
Spec: &inventory.InventorySpec{ | ||
Assets: []*inventory.Asset{ | ||
{ | ||
Connections: []*inventory.Config{ | ||
{ | ||
Type: "k8s", | ||
Options: map[string]string{ | ||
"path": "../testdata/2pods.yaml", | ||
}, | ||
Discover: &inventory.Discovery{ | ||
Targets: []string{"pods"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
loader := policy.DefaultBundleLoader() | ||
bundle, err := loader.BundleFromPaths("../testdata/kubernetes-security.mql.yaml") | ||
require.NoError(b, err) | ||
|
||
_, err = bundle.CompileExt(context.Background(), policy.BundleCompileConf{ | ||
CompilerConfig: conf, | ||
RemoveFailing: true, | ||
}) | ||
require.NoError(b, err) | ||
|
||
job.Bundle = bundle | ||
|
||
scanner := scan.NewLocalScanner(scan.DisableProgressBar()) | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
res, err := scanner.RunIncognito(ctx, job) | ||
require.NoError(b, err) | ||
require.NotNil(b, res) | ||
} | ||
} |
Oops, something went wrong.