Skip to content

Commit

Permalink
Add benchmarks to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilst committed Jan 7, 2025
1 parent 4f88199 commit a6dcfe8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ TEST = test/fedora-image/sshproxy_test.go
EXE = $(addprefix bin/, sshproxy sshproxy-dumpd sshproxy-replay sshproxyctl)
MANDOC = doc/sshproxy.yaml.5 doc/sshproxy.8 doc/sshproxy-dumpd.8 doc/sshproxy-replay.8 doc/sshproxyctl.8
PACKAGE = sshproxy_$(SSHPROXY_VERSION)_$(shell uname -s)_$(shell uname -p)
COMMIT = $(shell git describe --dirty)

This comment has been minimized.

Copy link
@od-cea

od-cea Jan 10, 2025

Member

Why using --dirty instead of --tags --long ?

This comment has been minimized.

Copy link
@cyrilst

cyrilst Jan 14, 2025

Author Contributor

--dirty allows to know if it's a clean commit, or if there are uncommitted changes. It may be useful when you want to benchmark your changes before committing.


all: exe doc

Expand Down Expand Up @@ -93,7 +94,11 @@ check:
test:
cd test && bash ./run.sh

benchmark:
mkdir -p $(GOPATH)/benchmarks/$(PACKAGE)
$(GO) test -failfast -race -count=6 -bench=. -run=^# -benchmem ./... | tee $(GOPATH)/benchmarks/$(PACKAGE)/$(COMMIT)

clean:
rm -f $(EXE) $(MANDOC) doc/*.xml sshproxy_*.tar.gz

.PHONY: all exe doc install install-doc-man install-binaries package fmt get-deps check test clean
.PHONY: all exe doc install install-doc-man install-binaries package fmt get-deps check test benchmark clean
28 changes: 28 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func TestCalcSessionID(t *testing.T) {
}
}

func BenchmarkCalcSessionID(b *testing.B) {
d := time.Unix(1136239445, 0)
for i := 0; i < b.N; i++ {
CalcSessionID("arno", d, "127.0.0.1:22")

This comment has been minimized.

Copy link
@od-cea

od-cea Jan 10, 2025

Member

This will always produce the same output. Is that wanted ? Can it be a bias ?

This comment has been minimized.

Copy link
@cyrilst

cyrilst Jan 14, 2025

Author Contributor

Yes, it will always produce the same output. As we're not testing the return of this function, we could indeed give it a few different inputs, and also use the current time instead of a fixed one.

}
}

var splithostportTests = []struct {
hostport, host, port string
}{
Expand Down Expand Up @@ -67,6 +74,16 @@ func TestInvalidSplitHostPort(t *testing.T) {
}
}

func BenchmarkSplitHostPort(b *testing.B) {
for _, tt := range splithostportTests {
b.Run(tt.hostport, func(b *testing.B) {
for i := 0; i < b.N; i++ {
SplitHostPort(tt.hostport)
}
})
}
}

func mockNetLookupHost(host string) ([]string, error) {
if host == "err" {
return nil, errors.New("LookupHost error")
Expand Down Expand Up @@ -190,3 +207,14 @@ func TestInvalidMatchSource(t *testing.T) {
}
}
}

func BenchmarkMatchSource(b *testing.B) {
netLookupHost = mockNetLookupHost
for _, tt := range matchSourceTests {
b.Run(tt.source + "_" + tt.sshdHostport, func(b *testing.B) {
for i := 0; i < b.N; i++ {
MatchSource(tt.source, tt.sshdHostport)
}
})
}
}

0 comments on commit a6dcfe8

Please sign in to comment.