Skip to content

Commit 67e7ffe

Browse files
authored
Add benchmarks to tests (#24)
1 parent ab3d812 commit 67e7ffe

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ doc/*.xml
1111

1212
# packages
1313
sshproxy_*.tar.gz
14+
15+
# benchmark results
16+
benchmarks/results

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ TEST = test/fedora-image/sshproxy_test.go
2626
EXE = $(addprefix bin/, sshproxy sshproxy-dumpd sshproxy-replay sshproxyctl)
2727
MANDOC = doc/sshproxy.yaml.5 doc/sshproxy.8 doc/sshproxy-dumpd.8 doc/sshproxy-replay.8 doc/sshproxyctl.8
2828
PACKAGE = sshproxy_$(SSHPROXY_VERSION)_$(shell uname -s)_$(shell uname -p)
29+
COMMIT = $(shell git describe --dirty)
30+
DATE = $(shell date -Iseconds)
2931

3032
all: exe doc
3133

@@ -93,7 +95,11 @@ check:
9395
test:
9496
cd test && bash ./run.sh
9597

98+
benchmark:
99+
mkdir -p benchmarks/results
100+
$(GO) test -failfast -race -count=6 -bench=. -run=^# -benchmem ./... | tee benchmarks/results/$(DATE)-$(COMMIT)
101+
96102
clean:
97103
rm -f $(EXE) $(MANDOC) doc/*.xml sshproxy_*.tar.gz
98104

99-
.PHONY: all exe doc install install-doc-man install-binaries package fmt get-deps check test clean
105+
.PHONY: all exe doc install install-doc-man install-binaries package fmt get-deps check test benchmark clean

benchmarks/README.asciidoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Benchmark Tests
2+
===============
3+
4+
Launching the benchmark tests
5+
-----------------------------
6+
7+
From the root directory of this repository, just run +make benchmark+. It will
8+
create a directory benchmarks/results, and store the results in it.
9+
10+
Consistency
11+
-----------
12+
13+
Benchmarks are only usable if they are consistently run on the same computer,
14+
and with no other resource-consuming program running at the same time. Keep
15+
this in mind in order to have consistent results.
16+
17+
Analyzing the benchmark tests
18+
-----------------------------
19+
20+
You can use +benchstat+ (https://pkg.go.dev/golang.org/x/perf/cmd/benchstat)
21+
for analyzing the results. Just pass the result file name as an argument in
22+
order to see the results. You can even compare different results by passing
23+
more than one file as arguments.
24+
25+
// vim:tw=78:ft=asciidoc:

pkg/utils/utils_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ func TestCalcSessionID(t *testing.T) {
2525
}
2626
}
2727

28+
var calcSessionIDBenchmarks = []struct {
29+
username, hostport string
30+
}{
31+
{"arno", "127.0.0.1:22"},
32+
{"arno", "192.168.100.100:1234"},
33+
{"cyril", "127.0.0.1:22"},
34+
{"cyril", "192.168.100.100:1234"},
35+
}
36+
37+
func BenchmarkCalcSessionID(b *testing.B) {
38+
d := time.Now()
39+
for _, tt := range calcSessionIDBenchmarks {
40+
b.Run(tt.username+"_"+tt.hostport, func(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
CalcSessionID(tt.username, d, tt.hostport)
43+
}
44+
})
45+
}
46+
}
47+
2848
var splithostportTests = []struct {
2949
hostport, host, port string
3050
}{
@@ -67,6 +87,16 @@ func TestInvalidSplitHostPort(t *testing.T) {
6787
}
6888
}
6989

90+
func BenchmarkSplitHostPort(b *testing.B) {
91+
for _, tt := range splithostportTests {
92+
b.Run(tt.hostport, func(b *testing.B) {
93+
for i := 0; i < b.N; i++ {
94+
SplitHostPort(tt.hostport)
95+
}
96+
})
97+
}
98+
}
99+
70100
func mockNetLookupHost(host string) ([]string, error) {
71101
if host == "err" {
72102
return nil, errors.New("LookupHost error")
@@ -190,3 +220,14 @@ func TestInvalidMatchSource(t *testing.T) {
190220
}
191221
}
192222
}
223+
224+
func BenchmarkMatchSource(b *testing.B) {
225+
netLookupHost = mockNetLookupHost
226+
for _, tt := range matchSourceTests {
227+
b.Run(tt.source+"_"+tt.sshdHostport, func(b *testing.B) {
228+
for i := 0; i < b.N; i++ {
229+
MatchSource(tt.source, tt.sshdHostport)
230+
}
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)