-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathnginx_config_parser_benchmark_test.go
More file actions
125 lines (108 loc) · 2.94 KB
/
nginx_config_parser_benchmark_test.go
File metadata and controls
125 lines (108 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package config
import (
"context"
"fmt"
"log/slog"
"path/filepath"
"testing"
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/test/helpers"
"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/require"
)
var configFilePaths = []string{
"../../../test/config/nginx/nginx.conf",
"../../../test/config/nginx/nginx-with-1k-lines.conf",
"../../../test/config/nginx/nginx-with-2k-lines.conf",
"../../../test/config/nginx/nginx-with-3k-lines.conf",
"../../../test/config/nginx/nginx-with-10k-lines.conf",
}
func BenchmarkNginxConfigParser_Parse(b *testing.B) {
// Discard log messages
slog.SetDefault(slog.New(slog.DiscardHandler))
ctx := context.Background()
agentConfig := types.AgentConfig()
for _, configFilePath := range configFilePaths {
func(configFilePath string) {
b.Run(configFilePath, func(bb *testing.B) {
agentConfig.AllowedDirectories = []string{
filepath.Dir(configFilePath),
}
nginxConfigParser := NewNginxConfigParser(
agentConfig,
)
bb.ResetTimer()
for i := 0; i < bb.N; i++ {
_, err := nginxConfigParser.Parse(
ctx,
&mpi.Instance{
InstanceMeta: &mpi.InstanceMeta{
InstanceType: mpi.InstanceMeta_INSTANCE_TYPE_NGINX,
},
InstanceRuntime: &mpi.InstanceRuntime{
ConfigPath: configFilePath,
},
},
)
require.NoError(bb, err)
}
})
}(configFilePath)
}
}
// These tests don't exercise the traversal very well, they are more to track the growth of configs in size
func BenchmarkNginxConfigParserGeneratedConfig_Parse(b *testing.B) {
slog.SetDefault(slog.New(slog.DiscardHandler))
ctx := context.Background()
agentConfig := types.AgentConfig()
tests := []struct {
name string
fileSize int64
}{
{
name: "100 KB",
fileSize: int64(1 * 1024 * 1024 / 10), // 100 KB
},
{
name: "1 MB",
fileSize: int64(1 * 1024 * 1024), // 1 MB
},
{
name: "10 MB",
fileSize: int64(10 * 1024 * 1024), // 10 MB
},
}
for _, test := range tests {
b.Run(test.name, func(bb *testing.B) {
location := bb.TempDir()
fileName := fmt.Sprintf("%s/%d_%s", location, test.fileSize, "nginx.conf")
_, err := helpers.GenerateConfig(bb, fileName, test.fileSize)
require.NoError(b, err)
agentConfig.AllowedDirectories = []string{
location,
}
nginxConfigParser := NewNginxConfigParser(
agentConfig,
)
bb.ResetTimer()
for i := 0; i < bb.N; i++ {
_, parseErr := nginxConfigParser.Parse(
ctx,
&mpi.Instance{
InstanceMeta: &mpi.InstanceMeta{
InstanceType: mpi.InstanceMeta_INSTANCE_TYPE_NGINX,
},
InstanceRuntime: &mpi.InstanceRuntime{
ConfigPath: location,
},
},
)
require.NoError(bb, parseErr)
}
})
}
}