Skip to content

Commit

Permalink
tools: Limit line length to 100 characters (linter check) (sourcenetw…
Browse files Browse the repository at this point in the history
…ork#224)

Enables line limit linter and resolve all the linter errors.

Resolves sourcenetwork#214 .

Notes:

- For now line limits won't be enforced on *_test.go files.
- Tabs are counted as 1 character.
  • Loading branch information
shahzadlone authored Apr 1, 2022
1 parent 8f56758 commit 497547e
Show file tree
Hide file tree
Showing 83 changed files with 1,923 additions and 647 deletions.
71 changes: 37 additions & 34 deletions .golangci.sourceinc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ output:
sort-results: true


linters:
disable-all: true
enable:
- deadcode
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- errcheck
- forbidigo
- lll
# - wrapcheck
# - errorlint
# - gci
# - exportloopref
# - megacheck
# - govet
# - wsl
# - nilerr
# - nilnil
enable-all: false
disable:
# - maligned
# - prealloc
presets:
# - unused
# - bugs
fast: false


# all available settings of specific linters
linters-settings:
bidichk:
Expand Down Expand Up @@ -523,9 +557,9 @@ linters-settings:
- github.com\/user\/package\/v4\.Type

lll:
# max line length, lines longer will be reported. Default is 120.
# max line length, lines longer will be reported.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 120
line-length: 100
# tab width in spaces. Default to 1.
tab-width: 1

Expand Down Expand Up @@ -776,38 +810,6 @@ linters-settings:
# Intended to point to the repo location of the linter. Optional, just for documentation purposes.
# original-url: github.com/golangci/example-linter

linters:
disable-all: true
enable:
- deadcode
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- errcheck
- forbidigo
# - wrapcheck
# - errorlint
# - gci
# - exportloopref
# - megacheck
# - govet
# - wsl
# - nilerr
# - nilnil
enable-all: false
disable:
# - maligned
# - prealloc
presets:
# - unused
# - bugs
fast: false


issues:
# List of regexps of issue texts to exclude, empty list by default.
Expand All @@ -826,6 +828,7 @@ issues:
- errcheck
- dupl
- gosec
- lll

# Exclude known linters from partially hard-vendored code,
# which is impossible to exclude via "nolint" comments.
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ deps\:lint:
deps\:go-acc:
go install github.com/ory/go-acc@latest

.PHONY: deps\:go-lines
deps\:go-lines:
go install github.com/segmentio/golines@latest

.PHONY: deps\:bench
deps\:bench:
go install golang.org/x/perf/cmd/benchstat@latest
Expand Down
4 changes: 3 additions & 1 deletion api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func NewServer(db client.DB) *Server {
// todo - we should log via our own log, not middleware.logger
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"))
_, err := w.Write(
[]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"),
)
if err != nil {
log.ErrorE(context.Background(), "DefraDB HTTP API Welcome message writing failed", err)
}
Expand Down
35 changes: 29 additions & 6 deletions bench/bench_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func hashToInt64(s string) int64 {
return int64(h.Sum64())
}

func SetupCollections(b *testing.B, ctx context.Context, db client.DB, fixture fixtures.Generator) ([]client.Collection, error) {
func SetupCollections(
b *testing.B,
ctx context.Context,
db client.DB,
fixture fixtures.Generator,
) ([]client.Collection, error) {
numTypes := len(fixture.Types())
collections := make([]client.Collection, numTypes)
schema, err := ConstructSchema(fixture)
Expand Down Expand Up @@ -107,7 +112,11 @@ func ConstructSchema(fixture fixtures.Generator) (string, error) {
return schema, nil
}

func SetupDBAndCollections(b *testing.B, ctx context.Context, fixture fixtures.Generator) (client.DB, []client.Collection, error) {
func SetupDBAndCollections(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
) (client.DB, []client.Collection, error) {
db, err := NewTestDB(ctx, b)
if err != nil {
return nil, nil, err
Expand All @@ -126,7 +135,14 @@ func SetupDBAndCollections(b *testing.B, ctx context.Context, fixture fixtures.G
// Loads the given test database using the provided fixture context.
// It loads docCount number of documents asynchronously in batches of *up to*
// writeBatchGroup.
func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collection, fixture fixtures.Generator, docCount, opCount int, doSync bool) ([][]client.DocKey, error) {
func BackfillBenchmarkDB(
b *testing.B,
ctx context.Context,
cols []client.Collection,
fixture fixtures.Generator,
docCount, opCount int,
doSync bool,
) ([][]client.DocKey, error) {
numTypes := len(fixture.Types())

// load fixtures
Expand All @@ -141,7 +157,9 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec
// Note weird math because the last batch will likely be smaller then
// writeBatchGroup ~cus math~.
for bid := 0; float64(bid) < math.Ceil(float64(docCount)/writeBatchGroup); bid++ {
currentBatchSize := int(math.Min(float64((docCount - (bid * writeBatchGroup))), writeBatchGroup))
currentBatchSize := int(
math.Min(float64((docCount - (bid * writeBatchGroup))), writeBatchGroup),
)
var batchWg sync.WaitGroup
batchWg.Add(currentBatchSize)

Expand Down Expand Up @@ -171,8 +189,13 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec
// in place. The error check could prob use a wrap system
// but its fine :).
for {
if err := cols[j].Create(ctx, doc); err != nil && err.Error() == badger.ErrConflict.Error() {
log.Info(ctx, "Failed to commit TX for doc %s, retrying...\n", logging.NewKV("DocKey", doc.Key()))
if err := cols[j].Create(ctx, doc); err != nil &&
err.Error() == badger.ErrConflict.Error() {
log.Info(
ctx,
"Failed to commit TX for doc %s, retrying...\n",
logging.NewKV("DocKey", doc.Key()),
)
continue
} else if err != nil {
errCh <- fmt.Errorf("Failed to create document: %w", err)
Expand Down
38 changes: 33 additions & 5 deletions bench/collection/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@ const (
writeBatchGroup = 100
)

func runCollectionBenchGet(b *testing.B, ctx context.Context, fixture fixtures.Generator, docCount, opCount int, doSync bool) error {
func runCollectionBenchGet(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
docCount, opCount int,
doSync bool,
) error {
db, collections, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
if err != nil {
return err
}
defer db.Close(ctx)

dockeys, err := benchutils.BackfillBenchmarkDB(b, ctx, collections, fixture, docCount, opCount, doSync)
dockeys, err := benchutils.BackfillBenchmarkDB(
b,
ctx,
collections,
fixture,
docCount,
opCount,
doSync,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -96,7 +110,13 @@ func runCollectionBenchGetAsync(b *testing.B,
return nil
}

func runCollectionBenchCreate(b *testing.B, ctx context.Context, fixture fixtures.Generator, docCount, opCount int, doSync bool) error {
func runCollectionBenchCreate(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
docCount, opCount int,
doSync bool,
) error {
b.StopTimer()
db, collections, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
if err != nil {
Expand All @@ -117,7 +137,13 @@ func runCollectionBenchCreate(b *testing.B, ctx context.Context, fixture fixture
return runCollectionBenchCreateAsync(b, ctx, collections, fixture, docCount, opCount)
}

func runCollectionBenchCreateMany(b *testing.B, ctx context.Context, fixture fixtures.Generator, docCount, opCount int, doSync bool) error {
func runCollectionBenchCreateMany(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
docCount, opCount int,
doSync bool,
) error {
db, collections, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
if err != nil {
return err
Expand Down Expand Up @@ -194,7 +220,9 @@ func runCollectionBenchCreateAsync(b *testing.B,
wg.Add(opCount)

for bid := 0; float64(bid) < math.Ceil(float64(opCount)/writeBatchGroup); bid++ {
currentBatchSize := int(math.Min(float64((opCount - (bid * writeBatchGroup))), writeBatchGroup))
currentBatchSize := int(
math.Min(float64((opCount - (bid * writeBatchGroup))), writeBatchGroup),
)
var batchWg sync.WaitGroup
batchWg.Add(currentBatchSize)

Expand Down
19 changes: 16 additions & 3 deletions bench/query/planner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import (
"github.com/sourcenetwork/defradb/query/graphql/schema"
)

func runQueryParserBench(b *testing.B, ctx context.Context, fixture fixtures.Generator, query string) error {
func runQueryParserBench(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
query string,
) error {
exec, err := buildExecutor(ctx, fixture)
if err != nil {
return err
Expand All @@ -39,7 +44,12 @@ func runQueryParserBench(b *testing.B, ctx context.Context, fixture fixtures.Gen
return nil
}

func runMakePlanBench(b *testing.B, ctx context.Context, fixture fixtures.Generator, query string) error {
func runMakePlanBench(
b *testing.B,
ctx context.Context,
fixture fixtures.Generator,
query string,
) error {
db, _, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
if err != nil {
return err
Expand Down Expand Up @@ -71,7 +81,10 @@ func runMakePlanBench(b *testing.B, ctx context.Context, fixture fixtures.Genera
return nil
}

func buildExecutor(ctx context.Context, fixture fixtures.Generator) (*planner.QueryExecutor, error) {
func buildExecutor(
ctx context.Context,
fixture fixtures.Generator,
) (*planner.QueryExecutor, error) {
sm, err := schema.NewSchemaManager()
if err != nil {
return nil, err
Expand Down
36 changes: 32 additions & 4 deletions bench/query/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,59 @@ var (

func Benchmark_Query_UserSimple_Query_Sync_1(b *testing.B) {
ctx := context.Background()
err := runQueryBenchGet(b, ctx, fixtures.ForSchema(ctx, "user_simple"), 1, userSimpleQuery, false)
err := runQueryBenchGet(
b,
ctx,
fixtures.ForSchema(ctx, "user_simple"),
1,
userSimpleQuery,
false,
)
if err != nil {
b.Fatal(err)
}
}

func Benchmark_Query_UserSimple_Query_Sync_10(b *testing.B) {
ctx := context.Background()
err := runQueryBenchGet(b, ctx, fixtures.ForSchema(ctx, "user_simple"), 10, userSimpleQuery, false)
err := runQueryBenchGet(
b,
ctx,
fixtures.ForSchema(ctx, "user_simple"),
10,
userSimpleQuery,
false,
)
if err != nil {
b.Fatal(err)
}
}

func Benchmark_Query_UserSimple_Query_Sync_100(b *testing.B) {
ctx := context.Background()
err := runQueryBenchGet(b, ctx, fixtures.ForSchema(ctx, "user_simple"), 100, userSimpleQuery, false)
err := runQueryBenchGet(
b,
ctx,
fixtures.ForSchema(ctx, "user_simple"),
100,
userSimpleQuery,
false,
)
if err != nil {
b.Fatal(err)
}
}

func Benchmark_Query_UserSimple_Query_Sync_1000(b *testing.B) {
ctx := context.Background()
err := runQueryBenchGet(b, ctx, fixtures.ForSchema(ctx, "user_simple"), 1000, userSimpleQuery, false)
err := runQueryBenchGet(
b,
ctx,
fixtures.ForSchema(ctx, "user_simple"),
1000,
userSimpleQuery,
false,
)
if err != nil {
b.Fatal(err)
}
Expand Down
Loading

0 comments on commit 497547e

Please sign in to comment.