Skip to content

Commit

Permalink
chore(pkg): some refactors + a benchmark test for autogenerate.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Aug 31, 2023
1 parent 4fa67c5 commit 38d44a9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
22 changes: 3 additions & 19 deletions pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
)

Expand Down Expand Up @@ -80,29 +79,14 @@ func Run(opts Options) error {
func autogenerateConfigs(opts Options, jsonData []byte) error {
slog.SetDefault(slog.With("target-distro", opts.Distro))

distroFilter := func(distro string) bool {
matched, _ := regexp.MatchString(opts.Distro, distro)
return matched
}

kernelreleaseFilter := func(kernelrelease string) bool {
matched, _ := regexp.MatchString(opts.KernelRelease, kernelrelease)
return matched
}

kernelversionFilter := func(kernelversion string) bool {
matched, _ := regexp.MatchString(opts.KernelVersion, kernelversion)
return matched
}

// Generate a dynamic struct with all needed distros
// NOTE: we might need a single distro when `lastDistro` is != "*";
// else, we will add all SupportedDistros found in constants.go
distroCtr := 0
instanceBuilder := dynamicstruct.NewStruct()
for distro, _ := range root.SupportedDistros {
distroStr := string(distro)
if distroFilter(distroStr) {
if opts.DistroFilter(distroStr) {
tag := fmt.Sprintf(`json:"%s"`, distroStr)
instanceBuilder.AddField(distroStr, []validate.KernelEntry{}, tag)
distroCtr++
Expand Down Expand Up @@ -133,10 +117,10 @@ func autogenerateConfigs(opts Options, jsonData []byte) error {
errGrp.Go(func() error {
for _, kernelEntry := range kernelEntries {
// Skip unneeded kernel entries
if !kernelreleaseFilter(kernelEntry.KernelRelease) {
if !opts.KernelVersionFilter(kernelEntry.KernelRelease) {
continue
}
if !kernelversionFilter(kernelEntry.KernelVersion) {
if !opts.KernelVersionFilter(kernelEntry.KernelVersion) {
continue
}

Expand Down
25 changes: 24 additions & 1 deletion pkg/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@ import (
"testing"
)

func TestAutogenerate(t *testing.T) {
func BenchmarkAutogenerate(b *testing.B) {
testCacheData = true // enable json data caching for subsequent tests
opts := Options{
Options: root.Options{
RepoRoot: "./test/",
Architecture: "x86_64",
DriverVersion: []string{"5.0.1+driver"},
},
DriverName: "falco",
Auto: true,
}

b.StopTimer()

for n := 0; n < b.N; n++ {
b.StartTimer()
err := Run(opts)
assert.NoError(b, err)
b.StopTimer()
_ = os.RemoveAll("./test/")
}
}

func TestGenerate(t *testing.T) {
testCacheData = true // enable json data caching for subsequent tests
tests := map[string]struct {
opts Options
Expand Down
16 changes: 16 additions & 0 deletions pkg/root/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/viper"
"log/slog"
"regexp"
)

type Target struct {
Expand Down Expand Up @@ -34,6 +35,21 @@ func (t Target) ToGlob() string {
return fmt.Sprintf("%s_%s_%s.yaml", t.Distro, t.KernelRelease, t.KernelVersion)
}

func (t Target) DistroFilter(distro string) bool {
matched, _ := regexp.MatchString(t.Distro, distro)
return matched
}

func (t Target) KernelReleaseFilter(kernelrelease string) bool {
matched, _ := regexp.MatchString(t.KernelRelease, kernelrelease)
return matched
}

func (t Target) KernelVersionFilter(kernelversion string) bool {
matched, _ := regexp.MatchString(t.KernelVersion, kernelversion)
return matched
}

type Options struct {
DryRun bool
RepoRoot string
Expand Down
21 changes: 3 additions & 18 deletions pkg/utils/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,6 @@ func LoopBucketFiltered(client *s3.Client,
dkDistro := kDistro.ToDriverkitDistro()
opts.Distro = string(dkDistro)

distroFilter := func(distro string) bool {
matched, _ := regexp.MatchString(opts.Distro, distro)
return matched
}

kernelreleaseFilter := func(kernelrelease string) bool {
matched, _ := regexp.MatchString(opts.KernelRelease, kernelrelease)
return matched
}

kernelversionFilter := func(kernelversion string) bool {
matched, _ := regexp.MatchString(opts.KernelVersion, kernelversion)
return matched
}

prefix := filepath.Join("driver", driverVersion, opts.Architecture)
params := &s3.ListObjectsV2Input{
Bucket: aws.String(S3Bucket),
Expand Down Expand Up @@ -93,15 +78,15 @@ func LoopBucketFiltered(client *s3.Client,
if i > 0 && i <= len(matches) {
switch name {
case "Distro":
if !distroFilter(matches[i]) {
if !opts.DistroFilter(matches[i]) {
continue keyLoop
}
case "KernelRelease":
if !kernelreleaseFilter(matches[i]) {
if !opts.KernelReleaseFilter(matches[i]) {
continue keyLoop
}
case "KernelVersion":
if !kernelversionFilter(matches[i]) {
if !opts.KernelVersionFilter(matches[i]) {
continue keyLoop
}
}
Expand Down

0 comments on commit 38d44a9

Please sign in to comment.