Skip to content

Commit 27a1c57

Browse files
authored
Merge pull request #2304 from simonbaird/EC-968
Simple macro benchmark
2 parents ae653ef + f0635a3 commit 27a1c57

25 files changed

+1427
-19
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,6 @@ node_modules
177177

178178
# kustomize expanded Helm charts
179179
hack/**/charts
180+
181+
# Pregenerated benchmark data
182+
benchmark/*/data.tar.gz

.gitleaks.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[allowlist]
2+
description = "Project allowlist"
3+
4+
paths = [
5+
'''benchmark\/internal\/registry\/fake_quay.key$''',
6+
]

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ feature_%: ## Run acceptance tests for a single feature file, e.g. make feature_
139139
scenario_%: build ## Run acceptance tests for a single scenario, e.g. make scenario_inline_policy
140140
@cd acceptance && go test -test.run 'TestFeatures/$*'
141141

142+
benchmark/%/data.tar.gz:
143+
@cd benchmark/$*
144+
@./prepare_data.sh
145+
146+
.PHONY: benchmark_%
147+
benchmark_%: benchmark/%/data.tar.gz
148+
@cd benchmark/$*
149+
@go run .
150+
151+
.PHONY: benchmark_data
152+
benchmark_data: benchmark/simple/data.tar.gz ## Prepare data for benchmark
153+
154+
.PHONY: benchmark
155+
benchmark: benchmark_simple ## Run benchmarks
156+
142157
.PHONY: ci
143158
ci: test lint-fix acceptance ## Run the usual required CI tasks
144159

benchmark/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Benchmarks of ec CLI
2+
3+
Benchmarks within this directory use the [golang
4+
benchmarking](golang.org/x/benchmarks/) package and output in the [standard
5+
benchmark
6+
format](https://go.googlesource.com/proposal/+/master/design/14313-benchmark-format.md).
7+
8+
Each benchmark is built as a standalone executable with no external dependency
9+
other than any data that is contained within it. Benchmarks are run from within
10+
the directory they're defined in, simply by running `go run .`, additional
11+
arguments can be passed in, for example `-benchnum 10` to run the benchmark 10
12+
times.
+20
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sLn7jGZOUYaSrjG3
17+
OLf5AArhHzs0rRRcaCjbybuah8nQMMqG7Q6bz7jF4szCeIL7ndWro2U5kgVVMrXS
18+
5zbEklvW5OXFO/UREiY+5YBAu6gCr0APFLAQi5qfAhKpbQkjZ28unuB/3aQlT0kR
19+
wHo3oDNZDNaIwBoP/54=
20+
-----END CERTIFICATE-----
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCdUi9QYcDZcVJx
3+
pFw3y/E2K4NKE3hI24JZx0JxvO8JsDvK7i/YskGqxsMiznNZyv2RvXm+DQKDd0If
4+
O2ewkKYUjCnqUpzeXrR4Pj5CR7ZicPdxQH8/gDuMxQLaQSOOWp2egvDogjC3sPol
5+
l5pmEOk98bJ/eWXGhV1Pn4l18G2fpThadiqpODu9xyh7RFNeSi0sBQzKyF3TRBoY
6+
og5nkd3MMFcdRWzNNT5WJ1/3HLGd0NWXGcTvTNa/+N5kuGCeTExHOiLSraGrblN6
7+
6BD7KsYpUPhQEHoJoZaAAg1LDSJMWZh37S4FWlNgXwos7aNz9s2oUYmfa3/4/zxe
8+
p8dMJlV/AgMBAAECggEASM1Xh+M002tVs7FT255NKbxJLWaFK3IpiFJltSyxkdI0
9+
WVyQV79mFBmXA+x2BP+jCGaeiAyNNTljKADdox/NO4UBgKZqYZ2B9oMXXKTDChss
10+
5wPp3wEAjcw93bJ3OdFFT61YvsBAu0DzAaJ+mO0Kgtfuun77Ujs1SzuQ6Tsx/8ZD
11+
STr8q7DcMAd6afHFFk5NZDqyFPgzmEMUWRFy7BH/vLLEgdRi7I7UbTYhjpP7uyjF
12+
/0HLSjBQJr58554CyL11OcVOwqktKZYKMVsCNWg6l4uhYxg0ChyJgO+PuUhPqh1i
13+
eIybcaDUVQ0LG/gBYzDlnw8yNXPO4+HX+MdIXafBAQKBgQDZx6m2rsI3L4M0ow89
14+
T6L8jMrFF6bUp2PBVPOYvobk4de3x7ma4WwK212l869A2u536+8BlBaiZN0yRSmI
15+
XXX0oL8JfqTO7d5efa9zHdkEN+t7iwVFWsYe74jjVR/oOyW7ZJ07gP/kZudbm8uk
16+
DnGzEGsjTYo7K6dEcEiXXmZiDwKBgQC47j8iR7v6a0E7EzEcM6FIpAV8tlpj0wUt
17+
pXKHPRdMQaOi39YyNWCGGake7bRCGz2ni1zqSVqVIcm6yvzlS/o3lLMTUYlUxtqt
18+
0cGrTJXkyhwkyoAJg0vwU43BJkbUNsX0y5revv/T4TaVTYK4dv2RTv8Jbdp6sT7i
19+
B6I7tFmFkQKBgQDQShe24rRchbPOrzoPINC5DYuOMA3fC+3r5e1KJMftt1dTrdGG
20+
IZ3tUFvkMgpnVD2KMvyYHOP9E80/nEiZ5RHBE9FDKn5Eb9sjssAQsPZ0A2vU8GTt
21+
LWGaCu14yFzJB3ESJqv/UmTsNcOHqZ1+XlY+tjBSRoI0D49edKnpJF913wKBgQCn
22+
WSHUuamIIYr0FJf6d1ZaT+OPSc1eTFWBSxjq/QOaREP6XiiNYvQoJgx/KohW0iPm
23+
/Bxm/15zWhIdcReNwEV8CppKvxxRlWnKpehmRXXXhnYVbRKUiTYtEs9SnHq4C6Dz
24+
t1Q8ggcsC7/DOFM07qjj1+K++6QcJ01KabIL6VahoQKBgGNCgw4+0291OA8nwjXl
25+
HNu9WBzJVHsAL9RDvrL6c4wcUWmhJTUcKN/TnndYN5SdvrOzZ2RjysS8KZoLnHgI
26+
c41uBNLvGtE9RPuzFe7U3zt9t80d4gcjy92tBru6xszPIgmKsUe8Hrl2EWbJvCET
27+
/BbfoyzzqH3f+vYm25GFSIy5
28+
-----END PRIVATE KEY-----
+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright The Enterprise Contract Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//type Closer func()
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
package registry
18+
19+
import (
20+
"context"
21+
"crypto/tls"
22+
"crypto/x509"
23+
_ "embed"
24+
"fmt"
25+
"log"
26+
"net"
27+
"net/http"
28+
"net/http/httptest"
29+
"net/url"
30+
"os"
31+
"path"
32+
"time"
33+
34+
"github.com/docker/docker/api/types/container"
35+
"github.com/smarty/cproxy/v2"
36+
"github.com/testcontainers/testcontainers-go"
37+
"github.com/testcontainers/testcontainers-go/modules/registry"
38+
"github.com/testcontainers/testcontainers-go/wait"
39+
40+
"github.com/enterprise-contract/ec-cli/benchmark/internal/suite"
41+
)
42+
43+
//go:embed fake_quay.cer
44+
var certificate []byte
45+
46+
//go:embed fake_quay.key
47+
var key []byte
48+
49+
type registryCloser struct {
50+
tempDir string
51+
container *registry.RegistryContainer
52+
proxy *httptest.Server
53+
}
54+
55+
func (r *registryCloser) Close() {
56+
if r == nil {
57+
return
58+
}
59+
60+
if r.tempDir != "" {
61+
_ = os.RemoveAll(r.tempDir)
62+
}
63+
64+
if r.container != nil {
65+
_ = r.container.Terminate(context.Background())
66+
}
67+
68+
if r.proxy != nil {
69+
r.proxy.Close()
70+
}
71+
}
72+
73+
type registryProxy struct {
74+
registry string
75+
}
76+
77+
func (f *registryProxy) IsAuthorized(_ http.ResponseWriter, req *http.Request) bool {
78+
req.RequestURI = fmt.Sprintf("https://%s/", f.registry)
79+
req.URL, _ = url.Parse(req.RequestURI)
80+
req.Host = f.registry
81+
82+
return true
83+
}
84+
85+
func Launch(data string) (suite.Closer, error) {
86+
ctx := context.Background()
87+
88+
env := testcontainers.WithEnv(map[string]string{
89+
"REGISTRY_HTTP_TLS_CERTIFICATE": "/tls/fake_quay.cer",
90+
"REGISTRY_HTTP_TLS_KEY": "/tls/fake_quay.key",
91+
})
92+
93+
dir, err := os.MkdirTemp("", "ec-benchmark-tls-*")
94+
if err != nil {
95+
return nil, err
96+
}
97+
closer := &registryCloser{dir, nil, nil}
98+
99+
if err := os.Setenv("https_proxy", "http://localhost:3128"); err != nil {
100+
return nil, err
101+
}
102+
103+
if err := os.Chmod(dir, 0755); err != nil {
104+
return closer.Close, err
105+
}
106+
107+
certPath := path.Join(dir, "fake_quay.cer")
108+
if err := os.WriteFile(certPath, certificate, 0600); err != nil {
109+
return closer.Close, err
110+
}
111+
112+
if err := os.Setenv("SSL_CERT_FILE", certPath); err != nil {
113+
return closer.Close, err
114+
}
115+
116+
if err := os.WriteFile(path.Join(dir, "fake_quay.key"), key, 0600); err != nil {
117+
return closer.Close, err
118+
}
119+
120+
rp := registryProxy{}
121+
proxy := cproxy.New(cproxy.Options.Filter(&rp), cproxy.Options.Logger(log.Default()))
122+
proxyServer := httptest.NewUnstartedServer(proxy)
123+
proxyServer.Listener, err = net.Listen("tcp", "127.0.0.1:3128")
124+
if err != nil {
125+
return closer.Close, err
126+
}
127+
proxyServer.Config.ErrorLog = log.Default()
128+
proxyServer.Start()
129+
closer.proxy = proxyServer
130+
131+
tlsMount := testcontainers.WithHostConfigModifier(func(hostConfig *container.HostConfig) {
132+
hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:/tls:ro,Z", dir))
133+
})
134+
135+
roots := x509.NewCertPool()
136+
roots.AppendCertsFromPEM(certificate)
137+
waitStrategy := testcontainers.WithWaitStrategy(wait.ForHTTP("/").
138+
WithPort("5000/tcp").
139+
WithTLS(true, &tls.Config{
140+
RootCAs: roots,
141+
MinVersion: tls.VersionTLS13,
142+
}).
143+
WithStartupTimeout(10 * time.Second))
144+
145+
opts := []testcontainers.ContainerCustomizer{
146+
registry.WithData(data),
147+
env,
148+
tlsMount,
149+
waitStrategy,
150+
}
151+
152+
if false {
153+
opts = append(opts,
154+
testcontainers.WithConfigModifier(func(config *container.Config) {
155+
config.AttachStdout = true
156+
}),
157+
testcontainers.WithLogger(log.Default()))
158+
}
159+
160+
r, err := registry.Run(ctx, "registry:2.8.3", opts...)
161+
if err != nil {
162+
return closer.Close, err
163+
}
164+
closer.container = r
165+
166+
rp.registry = r.RegistryName
167+
168+
return closer.Close, nil
169+
}

benchmark/internal/suite/closer.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright The Enterprise Contract Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
package suite
18+
19+
type Closer func()

benchmark/internal/suite/suite.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The Enterprise Contract Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
package suite
18+
19+
import (
20+
"io"
21+
22+
"github.com/enterprise-contract/ec-cli/cmd"
23+
"github.com/enterprise-contract/ec-cli/cmd/root"
24+
)
25+
26+
func Execute(args []string) error {
27+
c := root.NewRootCmd()
28+
cmd.AddCommandsTo(c)
29+
c.SetArgs(args)
30+
c.SetOutput(io.Discard)
31+
return c.Execute()
32+
}

0 commit comments

Comments
 (0)