discovery-service/pkg/server/version_test.go
Andrey Smirnov b8da986b5a fix: reduce memory allocations (logger)
Rework the gRPC logger by using hand-rolled simple version, rework
version parsing to remove regexp matching.

The baseline (via benchmark):

```
BenchmarkViaClientSimulator-32    	    2934	    387398 ns/op	  101921 B/op	     832 allocs/op
```

The baseline + removed logging middleware:

```
BenchmarkViaClientSimulator-32    	    3543	    331166 ns/op	   73581 B/op	     543 allocs/op
```

Reworked logging middleware:

```
BenchmarkViaClientSimulator-32    	    3394	    334066 ns/op	   77985 B/op	     568 allocs/op
```

Plus reworked version parsing:

```
BenchmarkViaClientSimulator-32    	    3510	    325714 ns/op	   66215 B/op	     561 allocs/op
```

So overall, baseline to this PR:

* allocs 101921 -> 66215 B/op
* alloc ops 832 -> 561 allocs/op

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
2024-10-22 13:30:21 +04:00

36 lines
954 B
Go

// Copyright (c) 2024 Sidero Labs, Inc.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
package server //nolint:testpackage
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseVersion(t *testing.T) {
t.Parallel()
for v, expected := range map[string]string{
"": "unknown",
"unknown": "unknown",
"v0.13.0": "v0.13",
"v0.13.0-beta.0": "v0.13-pre",
"v0.14.0-alpha.0-7-gf7d9f211": "v0.14-pre",
"v0.14.0-alpha.0-7-gf7d9f211-dirty": "v0.14-pre",
"v1.8.3": "v1.8",
"v1.8.3-7-gf7d9f211": "v1.8-pre",
"v1.8.0-beta.1": "v1.8-pre",
"v1.2": "unknown",
} {
t.Run(v, func(t *testing.T) {
t.Parallel()
assert.Equal(t, expected, parseVersion(v))
})
}
}