Skip to content

Commit

Permalink
[Chore] Add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Feb 22, 2024
1 parent e5c4956 commit 056ac3d
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 49 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint

on:
push:
branches-ignore:
- release

jobs:
lint:
strategy:
matrix:
go-version: [1.21.x]
platform: [ubuntu-latest]

runs-on: ${{ matrix.platform }}

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: lint
uses: golangci/golangci-lint-action@v4
with:
version: latest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ go.work

/.idea/
.DS_Store
lint.txt
106 changes: 106 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
run:
concurrency: 8
timeout: 5m
build-tags:
- integration
modules-download-mode: readonly
go: '1.17'
output:
format: tab:lint.txt
print-issued-lines: false
uniq-by-line: false
sort-results: true
linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
- durationcheck
- errcheck
- errname
- errorlint
- exportloopref
- gocheckcompilerdirectives
- gocritic
- godot
- gofumpt
- gci
- gomoddirectives
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- nilerr
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
disable:
- unused
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
- path: _test\.go
linters:
- gosec
- path: simulator/internal/trace/generator/zipf.go
linters:
- gosec
linters-settings:
gci:
sections:
- standard # Standard lib
- default # External dependencies
- prefix(github.com/maypok86/benchmarks) # Internal packages
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- hugeParam
- rangeExprCopy
- rangeValCopy
errcheck:
check-type-assertions: true
check-blank: true
exclude-functions:
- io/ioutil.ReadFile
- io.Copy(*bytes.Buffer)
- io.Copy(os.Stdout)
nakedret:
max-func-lines: 1
revive:
rules:
- name: empty-block
disabled: true
tagliatelle:
case:
rules:
json: snake
yaml: snake
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
.PHONY: setup
setup: deps ## Setup development environment
cp ./scripts/pre-push.sh .git/hooks/pre-push
chmod +x .git/hooks/pre-push

.PHONY: deps
deps: ## Install all the build and lint dependencies
bash scripts/deps.sh

.PHONY: lint
lint: ## Run all the linters
golangci-lint run -v ./...

.PHONY: ci
ci: lint ## Run all the tests and code checks

.PHONY: fmt
fmt: ## Run format tools on all go files
gci write --skip-vendor --skip-generated \
Expand Down
4 changes: 2 additions & 2 deletions client/ccache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Ccache[V any] struct {
client *ccache.Cache[V]
}

func (c *Ccache[V]) Init(cap int) {
func (c *Ccache[V]) Init(capacity int) {
client := ccache.New(
ccache.Configure[V]().
MaxSize(int64(cap)).
MaxSize(int64(capacity)).
Buckets(uint32(16 * runtime.GOMAXPROCS(0))),
)
c.client = client
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

type Client[K comparable, V any] interface {
Init(cap int)
Init(capacity int)
Get(key K) (V, bool)
Set(key K, value V)
Name() string
Expand Down
4 changes: 2 additions & 2 deletions client/golangfifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type FIFO[K comparable, V any] struct {
client fifo.Cache[K, V]
}

func (c *FIFO[K, V]) Init(cap int) {
client := fifo.NewS3FIFO[K, V](cap)
func (c *FIFO[K, V]) Init(capacity int) {
client := fifo.NewS3FIFO[K, V](capacity)
c.client = client
}

Expand Down
8 changes: 4 additions & 4 deletions client/golanglru.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type LRU[K comparable, V any] struct {
client *lru.Cache[K, V]
}

func (c *LRU[K, V]) Init(cap int) {
client, err := lru.New[K, V](cap)
func (c *LRU[K, V]) Init(capacity int) {
client, err := lru.New[K, V](capacity)
if err != nil {
panic(err)
}
Expand All @@ -37,8 +37,8 @@ type ARC[K comparable, V any] struct {
client *arc.ARCCache[K, V]
}

func (c *ARC[K, V]) Init(cap int) {
client, err := arc.NewARC[K, V](cap)
func (c *ARC[K, V]) Init(capacity int) {
client, err := arc.NewARC[K, V](capacity)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/otter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type Otter[K comparable, V any] struct {
client otter.Cache[K, V]
}

func (c *Otter[K, V]) Init(cap int) {
client, err := otter.MustBuilder[K, V](cap).Build()
func (c *Otter[K, V]) Init(capacity int) {
client, err := otter.MustBuilder[K, V](capacity).Build()
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions client/ristretto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ type Ristretto[K comparable, V any] struct {
client *ristretto.Cache
}

func (c *Ristretto[K, V]) Init(cap int) {
func (c *Ristretto[K, V]) Init(capacity int) {
client, err := ristretto.NewCache(&ristretto.Config{
NumCounters: int64(cap * 10),
MaxCost: int64(cap),
NumCounters: int64(capacity * 10),
MaxCost: int64(capacity),
BufferItems: 64,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/theine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type Theine[K comparable, V any] struct {
client *theine.Cache[K, V]
}

func (c *Theine[K, V]) Init(cap int) {
client, err := theine.NewBuilder[K, V](int64(cap)).Build()
func (c *Theine[K, V]) Init(capacity int) {
client, err := theine.NewBuilder[K, V](int64(capacity)).Build()
if err != nil {
panic(err)
}
Expand Down
9 changes: 5 additions & 4 deletions client/tinylfu.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package client

import (
"github.com/dgryski/go-tinylfu"
"strconv"

"github.com/dgryski/go-tinylfu"
)

type TinyLFU struct {
client *tinylfu.T[uint64]
}

func (c *TinyLFU) Init(cap int) {
client := tinylfu.New[uint64](cap, 10*cap)
func (c *TinyLFU) Init(capacity int) {
client := tinylfu.New[uint64](capacity, 10*capacity)
c.client = client
}

Expand All @@ -22,7 +23,7 @@ func (c *TinyLFU) Get(key uint64) (uint64, bool) {
return c.client.Get(strconv.FormatUint(key, 10))
}

func (c *TinyLFU) Set(key uint64, value uint64) {
func (c *TinyLFU) Set(key, value uint64) {
c.client.Add(strconv.FormatUint(key, 10), value)
}

Expand Down
14 changes: 9 additions & 5 deletions eviction/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ type benchCase struct {
}

var benchCases = []benchCase{
//{"capacity=1", 1},
//{"capacity=100", 100},
// {"capacity=1", 1},
// {"capacity=100", 100},
{"capacity=10000", 10000},
//{"capacity=1000000", 1000000},
//{"capacity=10000000", 10000000},
// {"capacity=1000000", 1000000},
// {"capacity=10000000", 10000000},
}

func runParallelBenchmark(b *testing.B, benchFunc func(pb *testing.PB)) {
b.Helper()

b.ResetTimer()
start := time.Now()
b.RunParallel(benchFunc)
opsPerSec := float64(b.N) / float64(time.Since(start).Seconds())
opsPerSec := float64(b.N) / time.Since(start).Seconds()
b.ReportMetric(opsPerSec, "ops/s")
}

Expand All @@ -49,6 +51,8 @@ func runCacheBenchmark(
benchCase benchCase,
c client.Client[int, struct{}],
) {
b.Helper()

c.Init(benchCase.cacheSize)

for i := 0; i < benchCase.cacheSize; i++ {
Expand Down
16 changes: 12 additions & 4 deletions memory/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ func main() {
path := os.Args[1]
dir := filepath.Dir(path)

if err := run(path, dir); err != nil {
log.Fatal(err)
}
}

func run(path, dir string) error {
memoryFile, err := os.Open(path)
if err != nil {
log.Fatal(err)
return err
}
defer memoryFile.Close()

Expand All @@ -35,7 +41,7 @@ func main() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return err
}

capacityToResults := make(map[int][]memoryResult)
Expand All @@ -44,11 +50,11 @@ func main() {
cacheName := fields[0]
capacity, err := strconv.Atoi(fields[1])
if err != nil {
log.Fatal("can not parse benchmark output", err)
return fmt.Errorf("can not parse benchmark output: %w", err)
}
alloc, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
log.Fatal("can not parse benchmark output", err)
return fmt.Errorf("can not parse benchmark output: %w", err)
}

capacityToResults[capacity] = append(capacityToResults[capacity], memoryResult{
Expand Down Expand Up @@ -93,4 +99,6 @@ func main() {
imagePath := filepath.Join(dir, fmt.Sprintf("%s.png", outputName))
render.MakeChartSnapshot(bar.RenderContent(), imagePath)
}

return nil
}
9 changes: 9 additions & 0 deletions scripts/deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

(which golangci-lint > /dev/null) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
bash -s -- -b "$(go env GOPATH)"/bin v1.55.2

go install github.com/daixiang0/gci@latest
GO111MODULE=on go install mvdan.cc/gofumpt@latest
11 changes: 11 additions & 0 deletions scripts/pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Run CI tests
readonly ci_cmd="make ci"
eval "$ci_cmd"

readonly result=$?
if [ $result -ne 0 ]; then
echo -e "CI tests Failed!\n CMD: $ci_cmd"
exit 1
fi
3 changes: 0 additions & 3 deletions simulator/internal/parser/oracleGeneral.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func (og *OracleGeneral) Parse(send func(event event.AccessEvent) bool) (bool, e
return false, WrapError(err)
}

// clockTime := bin.Uint32(og.buffer)
id := bin.Uint64(og.buffer[4:])
// weight := bin.Uint32(buffer[12:])
// nextAccessTime := bin.Int64(buffer[16:]) ?????????

return send(event.NewAccessEvent(id)), nil
}
Loading

0 comments on commit 056ac3d

Please sign in to comment.