Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rearrange struct fields to save memory #32589

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions comp/core/workloadmeta/def/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func TestIsNodeMetadata(t *testing.T) {

tests := []struct {
name string
metadataEntity KubernetesMetadata
name string
shouldBeNodeMetadata bool
}{
{
Expand Down Expand Up @@ -61,9 +61,9 @@ func TestFilterBuilder_Build(t *testing.T) {
}

tests := []struct {
name string
builderFunc func() *FilterBuilder
expectedFilter *Filter
name string
}{
{
name: "filter with default options",
Expand Down Expand Up @@ -158,8 +158,8 @@ func TestFilter_MatchSource(t *testing.T) {

func TestFilter_MatchEventType(t *testing.T) {
tests := []struct {
name string
filter *Filter
name string
eventType EventType
expectMatchEventType bool
}{
Expand Down Expand Up @@ -254,9 +254,9 @@ func TestFilter_MatchKind(t *testing.T) {

func TestFilter_MatchEntity(t *testing.T) {
tests := []struct {
name string
filter *Filter
entity Entity
filter *Filter
name string
expectMatch bool
}{
{
Expand Down
2 changes: 1 addition & 1 deletion comp/core/workloadmeta/def/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package workloadmeta

// Params provides the kind of agent we're instantiating workloadmeta for
type Params struct {
AgentType AgentType
InitHelper InitHelper
AgentType AgentType
}

// NewParams creates a Params struct with the default NodeAgent configuration
Expand Down
168 changes: 168 additions & 0 deletions comp/core/workloadmeta/def/type_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package workloadmeta

import (
"testing"
"time"

"github.com/DataDog/datadog-agent/pkg/util/pointer"
)

func getAContainerEntity(id, image string) Entity {
return &Container{
EntityID: EntityID{
ID: id,
Kind: KindContainer,
},
EntityMeta: EntityMeta{
Labels: map[string]string{
"com.datadoghq.ad.check_names": "[\"apache\"]",
"com.datadoghq.ad.init_configs": "[{}]",
"com.datadoghq.ad.instances": "[{\"apache_status_url\": \"http://%%host%%/server-status?auto\"}]",
},
},
Runtime: ContainerRuntimeDocker,
Image: ContainerImage{
Name: image,
},
}
}

func getAContainerState() ContainerState {
return ContainerState{
CreatedAt: time.Time{},
StartedAt: time.Time{},
FinishedAt: time.Time{},
ExitCode: pointer.Ptr(int64(100)),
Health: ContainerHealthHealthy,
}
}

func getAPodEntity(id string) Entity {
return &KubernetesPod{
EntityMeta: EntityMeta{
Annotations: map[string]string{
"ad.datadoghq.com/apache.checks": `{
"http_check": {
"instances": [
{
"name": "My service",
"url": "http://%%host%%",
"timeout": 1
}
]
}
}`,
"ad.datadoghq.com/apache.check_names": "[\"invalid\"]",
"ad.datadoghq.com/apache.init_configs": "[{}]",
"ad.datadoghq.com/apache.instances": "[{}]",
},
},
EntityID: EntityID{
ID: id,
},

Containers: []OrchestratorContainer{
{
Name: "abc",
ID: "3b8efe0c50e1",
},
},
}
}

func getAProcessEntity() Entity {
return &Process{
EntityID: EntityID{
ID: "123",
Kind: KindProcess,
},
}
}

func BenchmarkProcessCollectorEvent(b *testing.B) {
containerEntity := getAContainerEntity("3b8efe0c50e1", "cassandra")
podEntity := getAPodEntity("3b8efe0c50e1")
processEntity := getAProcessEntity()
events := []CollectorEvent{
{
Type: EventType(0),
Source: "container",
Entity: containerEntity,
},
{
Type: EventType(1),
Source: "kubernetes",
Entity: podEntity,
},
{
Type: EventType(2),
Source: "processes",
Entity: processEntity,
},
}

for _, event := range events {
b.Run("Run collector event bench test on "+string(event.Source), func(b *testing.B) {
b.ReportAllocs() // Report memory allocations
b.ResetTimer()
// process events
for i := 0; i < b.N; i++ {
size := 1000
events := make([]CollectorEvent, size)
for i := 0; i < size; i++ {
events[i] = event
}
// Simulate some processing
for i := 0; i < size; i++ {
_ = events[i]
}
}
})
}
}

func BenchmarkProcessEntity(b *testing.B) {
containerEntity := getAContainerEntity("3b8efe0c50e1", "cassandra")
podEntity := getAPodEntity("3b8efe0c50e1")
procEntity := getAProcessEntity()
entities := []Entity{containerEntity, podEntity, procEntity}

for _, entity := range entities {
b.Run("Run process entity bench test on "+string(entity.GetID().Kind), func(b *testing.B) {
b.ReportAllocs() // Report memory allocations
b.ResetTimer()
// process entities
for i := 0; i < b.N; i++ {
size := 10
entities := make([]Entity, size)
for i := 0; i < size; i++ {
entities[i] = entity.DeepCopy()
}
// Simulate some processing
for i := 0; i < size; i++ {
_ = entities[i]
}
}
})
}
}

func BenchmarkProcessContainerState(b *testing.B) {
b.Run("Run process container state bench test", func(b *testing.B) {
b.ReportAllocs() // Report memory allocations
b.ResetTimer()
// process container states
for i := 0; i < b.N; i++ {
size := 1000
states := make([]ContainerState, size)
for i := 0; i < size; i++ {
states[i] = getAContainerState()
}
// Simulate some processing
var sum int64 = 0
for i := 0; i < size; i++ {
sum += *(states[i].ExitCode)
}
}
})
}
Loading
Loading