Skip to content

Commit

Permalink
feat: created linter and gosec rules
Browse files Browse the repository at this point in the history
  • Loading branch information
DenzelPenzel committed Jul 11, 2024
1 parent ec8f66c commit 5a8ddab
Show file tree
Hide file tree
Showing 36 changed files with 516 additions and 494 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
21 changes: 21 additions & 0 deletions .github/workflows/hygeine.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: hygeiene

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
golangci:
# Linting job
# https://github.com/golangci/golangci-lint-action
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52.1
22 changes: 22 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: security

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
# Static security scan using gosec
# https://github.com/securego/gosec
gosec:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- name: Checkout Source
uses: actions/checkout@v3
- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
args: ./...
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Go test workflow
name: test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.22

- name: Install project dependencies
run: |
go mod download
- name: Build App
run: make build-app

go-test:
outputs:
COVERAGE: ${{ steps.unit.outputs.coverage }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.22

- name: Install project dependencies
run: |
go mod download
- name: Run Tests
id: unit
run: |
make test
12 changes: 6 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ linters:
disable-all: true
enable:
## enabled by default
- errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
#- errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
- gosimple # specializes in simplifying a code
- ineffassign # detects when assignments to existing variables are not used
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
Expand All @@ -133,27 +133,27 @@ linters:
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- execinquery # checks query string in Query function which reads your Go src files and warning it finds
- exhaustive # checks exhaustiveness of enum switch statements
#- exhaustive # checks exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- forbidigo # forbids identifiers
- funlen # tool for detection of long functions
#- funlen # tool for detection of long functions
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
- gochecknoinits # checks that no init functions are present in Go code
- gocognit # computes and checks the cognitive complexity of functions
- goconst # finds repeated strings that could be replaced by a constant
- gocritic # provides diagnostics that check for bugs, performance and style issues
- gocyclo # computes and checks the cyclomatic complexity of functions
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
# - gomnd # detects magic numbers
#- gomnd # detects magic numbers
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
- goprintffuncname # checks that printf-like functions are named with f at the end
- gosec # inspects source code for security problems
- lll # reports long lines
#- lll # reports long lines
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
- makezero # finds slice declarations with non-zero initial length
- misspell # Ensures real english is used within strings
- nakedret # finds naked returns in functions greater than a specified function length
- nestif # reports deeply nested if statements
#- nestif # reports deeply nested if statements
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
- noctx # finds sending http request without context.Context
Expand Down
Empty file removed bench
Empty file.
58 changes: 30 additions & 28 deletions cmd/client/main.go → cmd/client/rand/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"bufio"
"context"
"crypto/rand"
"fmt"
"github.com/denzelpenzel/nyx/cmd/client/textprot"
"github.com/denzelpenzel/nyx/internal/common"
"github.com/denzelpenzel/nyx/internal/logging"
"github.com/denzelpenzel/nyx/internal/utils"
"github.com/urfave/cli"
"go.uber.org/zap"
"errors"
"io"
"log"
"math/big"
"net"
"os"
"runtime"
"runtime/pprof"
"sync"
"time"

"github.com/DenzelPenzel/nyx/cmd/client/textprot"
"github.com/DenzelPenzel/nyx/internal/common"
"github.com/DenzelPenzel/nyx/internal/logging"
"github.com/DenzelPenzel/nyx/internal/utils"
"github.com/urfave/cli"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -104,7 +106,7 @@ func run(c *cli.Context) {

opsPerTask := numOps / numCmds / numWorkers

fmt.Printf("Running %v ops total with:\n"+
log.Printf("Running %v ops total with:\n"+
"\t%v workers\n"+
"\ttotal commands %v\n"+
"\tusing the %v protocol\n"+
Expand All @@ -131,7 +133,7 @@ func run(c *cli.Context) {
connWg.Add(1)
conn, err := utils.Connect(httpAddr)
if err != nil {
fmt.Printf("Failed connect to %s error: %s\n", httpAddr.String(), err)
log.Printf("Failed connect to %s error: %s\n", httpAddr.String(), err)
i--
connWg.Add(-1)
continue
Expand All @@ -154,31 +156,31 @@ func run(c *cli.Context) {
hits[m.op] = append(hits[m.op], int(m.duration))
}

metricPool.Put(m)
metricPool.Put(m) //nolint:staticcheck //check pointer-like issue
}

for i, op := range allOps {
if i == 0 {
fmt.Println("===========Metrics===========")
log.Println("===========Metrics===========")
}
renderStats("hits", op, hits[op])
renderStats("misses", op, misses[op])
fmt.Println("=============================")
log.Println("=============================")
}

stats.Done()
}()

fmt.Println("Generate testing tasks...")
log.Println("Generate testing tasks...")
tasksWg.Wait()

fmt.Println("Tasks generation done")
log.Println("Tasks generation done")
close(tasks)

fmt.Println("Start tasks execution...")
log.Println("Start tasks execution...")
connWg.Wait()

fmt.Println("Execution done")
log.Println("Execution done")
close(metrics)

stats.Wait()
Expand Down Expand Up @@ -223,8 +225,8 @@ func execute(conn net.Conn, connWg *sync.WaitGroup, tasks <-chan *Task, metrics
if err != nil {
if !common.IsMiss(err) {
// socket is closed
if err == io.EOF {
fmt.Printf("Failed to execute request: %s, key: %s, error: %v\n", item.Cmd, item.Key, err)
if errors.Is(err, io.EOF) {
log.Printf("Failed to execute request: %s, key: %s, error: %v\n", item.Cmd, item.Key, err)
return
}
}
Expand All @@ -249,17 +251,17 @@ func genData(cmd Op) []byte {

func renderStats(t string, op Op, data []int) {
if len(data) == 0 {
fmt.Printf("\nNo %s %s\n", op.String(), t)
log.Printf("\nNo %s %s\n", op.String(), t)
return
}
s := GetStats(data)
fmt.Printf("%s %s (n = %d)\n", op.String(), t, len(data))
fmt.Printf("Min: %fms\n", s.Min)
fmt.Printf("Max: %fms\n", s.Max)
fmt.Printf("Avg: %fms\n", s.Avg)
fmt.Printf("p50: %fms\n", s.P50)
fmt.Printf("p75: %fms\n", s.P75)
fmt.Printf("p90: %fms\n", s.P90)
fmt.Printf("p95: %fms\n", s.P95)
fmt.Printf("p99: %fms\n", s.P99)
log.Printf("%s %s (n = %d)\n", op.String(), t, len(data))
log.Printf("Min: %fms\n", s.Min)
log.Printf("Max: %fms\n", s.Max)
log.Printf("Avg: %fms\n", s.Avg)
log.Printf("p50: %fms\n", s.P50)
log.Printf("p75: %fms\n", s.P75)
log.Printf("p90: %fms\n", s.P90)
log.Printf("p95: %fms\n", s.P95)
log.Printf("p99: %fms\n", s.P99)
}
60 changes: 60 additions & 0 deletions cmd/client/rand/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"math"
"slices"
"sort"
)

const (
msFactor = 1000000
)

type Stats struct {
Avg float64
Min float64
Max float64
P50 float64
P75 float64
P90 float64
P95 float64
P99 float64
}

func GetStats(data []int) Stats {
if len(data) == 0 {
return Stats{}
}

sort.Ints(data)
minv, maxv := slices.Min(data), slices.Max(data)

return Stats{
Avg: avg(data) / msFactor,
Min: float64(minv / msFactor),
Max: float64(maxv / msFactor),
P50: percent(data, 0.5) / msFactor,
P75: percent(data, 0.75) / msFactor,
P90: percent(data, 0.9) / msFactor,
P95: percent(data, 0.95) / msFactor,
P99: percent(data, 0.99) / msFactor,
}
}

func avg(data []int) float64 {
r := float64(0)
for _, d := range data {
r += float64(d)
}
return r / float64(len(data))
}

func percent(data []int, p float64) float64 {
idx := pIdx(len(data), p)
return float64(data[idx])
}

func pIdx(datalen int, p float64) int {
w := math.Ceil(float64(datalen) * p)
return int(min(w, float64(datalen-1)))
}
Loading

0 comments on commit 5a8ddab

Please sign in to comment.