Skip to content

Commit

Permalink
feat: benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 17, 2024
1 parent fc935e6 commit 34a2de0
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 61 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ ginkgo:
test: ginkgo
ginkgo -r -v

.PHONY: bench
bench:
go run cmd/bench/bench.go

fmt:
go fmt ./...

Expand Down
104 changes: 104 additions & 0 deletions cmd/bench/bench.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"flag"
"time"

"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/shutdown"
pkgGenerator "github.com/flanksource/duty/tests/generator"
"github.com/flanksource/duty/tests/setup"
)

var count = 2_000

func generateConfigItems(ctx context.Context, count int) error {
start := time.Now()
for {
generator := pkgGenerator.ConfigGenerator{
Nodes: pkgGenerator.ConfigTypeRequirements{
Count: 3,
},
Namespaces: pkgGenerator.ConfigTypeRequirements{
Count: 10,
},
DeploymentPerNamespace: pkgGenerator.ConfigTypeRequirements{
Count: 10,
},
ReplicaSetPerDeployment: pkgGenerator.ConfigTypeRequirements{
Count: 2,
Deleted: 1,
},
PodsPerReplicaSet: pkgGenerator.ConfigTypeRequirements{
Count: 2,
NumChangesPerConfig: 1,
NumInsightsPerConfig: 2,
},
Tags: map[string]string{
"cluster": "homelab",
},
}

generator.GenerateKubernetes()
if err := generator.Save(ctx.DB()); err != nil {
return err
}

var totalConfigs int64
if err := ctx.DB().Table("config_items").Count(&totalConfigs).Error; err != nil {
return err
}

if totalConfigs > int64(count) {
break
}

logger.Infof("created configs: %d/%d", totalConfigs, count)
}

var configs int64
if err := ctx.DB().Table("config_items").Count(&configs).Error; err != nil {
return err
}

var changes int64
if err := ctx.DB().Table("config_changes").Count(&changes).Error; err != nil {
return err
}

logger.Infof("configs %d, changes: %d in %s", configs, changes, time.Since(start))
return nil
}

func main() {
shutdown.WaitForSignal()
flag.IntVar(&count, "count", count, "generates at least these number of configs")
flag.Parse()

// start a postgres db with RLS disabled
if err := run(); err != nil {
shutdown.ShutdownAndExit(1, err.Error())
}

// TODO: run benchmark on another database RLS enabled
// can't use the same database to avoid caches from the previous benchmark.

shutdown.ShutdownAndExit(0, "exiting ...")
}

func run() error {
// setup a db with RLS disabled
ctx, err := setup.SetupDB("test", setup.WithoutRLS)
if err != nil {
return err
}

if err := generateConfigItems(ctx, count); err != nil {
return err
}

// Run fetch queries

return nil
}
7 changes: 6 additions & 1 deletion shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ var Shutdown = sync.OnceFunc(func() {

func ShutdownAndExit(code int, msg string) {
Shutdown()
logger.StandardLogger().WithSkipReportLevel(1).Errorf(msg)
if code == 0 {
logger.StandardLogger().WithSkipReportLevel(1).Infof(msg)
} else {
logger.StandardLogger().WithSkipReportLevel(1).Errorf(msg)
}

os.Exit(code)
}

Expand Down
13 changes: 7 additions & 6 deletions tests/config_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package tests

import (
"github.com/flanksource/duty/models"
pkgGenerator "github.com/flanksource/duty/tests/generator"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = ginkgo.Describe("Config Generator", ginkgo.Ordered, func() {
generator := ConfigGenerator{
Nodes: ConfigTypeRequirements{
generator := pkgGenerator.ConfigGenerator{
Nodes: pkgGenerator.ConfigTypeRequirements{
Count: 3,
},
Namespaces: ConfigTypeRequirements{
Namespaces: pkgGenerator.ConfigTypeRequirements{
Count: 2,
},
DeploymentPerNamespace: ConfigTypeRequirements{
DeploymentPerNamespace: pkgGenerator.ConfigTypeRequirements{
Count: 1,
},
ReplicaSetPerDeployment: ConfigTypeRequirements{
ReplicaSetPerDeployment: pkgGenerator.ConfigTypeRequirements{
Count: 4,
Deleted: 3,
},
PodsPerReplicaSet: ConfigTypeRequirements{
PodsPerReplicaSet: pkgGenerator.ConfigTypeRequirements{
Count: 1,
NumChangesPerConfig: 5,
NumInsightsPerConfig: 2,
Expand Down
13 changes: 7 additions & 6 deletions tests/config_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/tests/fixtures/dummy"
pkgGenerator "github.com/flanksource/duty/tests/generator"
"github.com/flanksource/duty/types"
"github.com/flanksource/duty/upstream"
"github.com/google/uuid"
Expand Down Expand Up @@ -464,12 +465,12 @@ var _ = ginkgo.Describe("config relationship deletion test", func() {
})

var _ = ginkgo.Describe("config relationship depth", ginkgo.Ordered, func() {
generator := ConfigGenerator{
Nodes: ConfigTypeRequirements{Count: 3},
Namespaces: ConfigTypeRequirements{Count: 2},
DeploymentPerNamespace: ConfigTypeRequirements{Count: 2},
ReplicaSetPerDeployment: ConfigTypeRequirements{Count: 4, Deleted: 3},
PodsPerReplicaSet: ConfigTypeRequirements{Count: 1, NumChangesPerConfig: 5, NumInsightsPerConfig: 2},
generator := pkgGenerator.ConfigGenerator{
Nodes: pkgGenerator.ConfigTypeRequirements{Count: 3},
Namespaces: pkgGenerator.ConfigTypeRequirements{Count: 2},
DeploymentPerNamespace: pkgGenerator.ConfigTypeRequirements{Count: 2},
ReplicaSetPerDeployment: pkgGenerator.ConfigTypeRequirements{Count: 4, Deleted: 3},
PodsPerReplicaSet: pkgGenerator.ConfigTypeRequirements{Count: 1, NumChangesPerConfig: 5, NumInsightsPerConfig: 2},
Tags: map[string]string{
"test": "true",
},
Expand Down
15 changes: 8 additions & 7 deletions tests/config_type_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/tests/generator"
"github.com/flanksource/duty/types"
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -96,13 +97,13 @@ var _ = ginkgo.Describe("Check config_class_summary view", ginkgo.Ordered, func(
})

ginkgo.It("Should query config summary by type", func() {
gen := ConfigGenerator{}
gen.GenerateConfigItem("Test::type-A", "healthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 4, NumInsightsPerConfig: 3})
gen.GenerateConfigItem("Test::type-A", "healthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 2})
gen.GenerateConfigItem("Test::type-A", "unhealthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 2})
gen.GenerateConfigItem("Test::type-B", "healthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 5, NumInsightsPerConfig: 1})
gen.GenerateConfigItem("Test::type-B", "healthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 3})
gen.GenerateConfigItem("Test::type-C", "unhealthy", nil, nil, ConfigTypeRequirements{NumChangesPerConfig: 0, NumInsightsPerConfig: 0})
gen := generator.ConfigGenerator{}
gen.GenerateConfigItem("Test::type-A", "healthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 4, NumInsightsPerConfig: 3})
gen.GenerateConfigItem("Test::type-A", "healthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 2})
gen.GenerateConfigItem("Test::type-A", "unhealthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 2})
gen.GenerateConfigItem("Test::type-B", "healthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 5, NumInsightsPerConfig: 1})
gen.GenerateConfigItem("Test::type-B", "healthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 1, NumInsightsPerConfig: 3})
gen.GenerateConfigItem("Test::type-C", "unhealthy", nil, nil, generator.ConfigTypeRequirements{NumChangesPerConfig: 0, NumInsightsPerConfig: 0})
for _, item := range gen.Generated.Configs {
DefaultContext.DB().Create(&item)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package generator

import (
"fmt"
Expand Down
Loading

0 comments on commit 34a2de0

Please sign in to comment.