Skip to content

Commit

Permalink
update code based on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Apr 22, 2021
1 parent fa6e6b2 commit 69609f8
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 40 deletions.
3 changes: 1 addition & 2 deletions Dockerfile.digest
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Build stage
FROM devopsworks/golang-upx:1.16 AS builder
ENV GO111MODULE=on \
CGO_ENABLED=0 \
ENV CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
COPY . /app
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.replayer
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Build stage
FROM devopsworks/golang-upx:1.15 AS builder
ENV GO111MODULE=on \
CGO_ENABLED=0 \
ENV CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
COPY . /app
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)

.PHONY: all build clean
.PHONY: all digest replayer

all: help

## Build:
digest: ## Build slowql-digest
$(GOCMD) mod tidy
GO111MODULE=on $(GOCMD) build -o $(BUILD_ROOT)digest ./cmd/slowql-digest/
@GO111MODULE=on $(GOCMD) build -o $(BUILD_ROOT)digest ./cmd/slowql-digest/
@echo "${GREEN}[*]${RESET} digest successfully built in ${YELLOW}${BUILD_ROOT}digest${RESET}"

replayer: ## Build slowql-replayer
$(GOCMD) mod tidy
GO111MODULE=on $(GOCMD) build -o $(BUILD_ROOT)replayer ./cmd/slowql-replayer/
@GO111MODULE=on $(GOCMD) build -o $(BUILD_ROOT)replayer ./cmd/slowql-replayer/
@echo "${GREEN}[*]${RESET} replayer successfully built in ${YELLOW}${BUILD_ROOT}replayer${RESET}"

clean: ## Clean all the files and binaries generated by the Makefile
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ go get github.com/devops-works/slowql

## Basic usage

To put in a nutshell, you can use it as follows:
In a nutshell, you can use it as follows:

```go
package main
Expand Down Expand Up @@ -57,7 +57,7 @@ func main() {
}
```

## Performances
## Performance

Running the example given in cmd/ without any `fmt.Printf` against a 292MB slow query logs from a MySQL database provides the following output:

Expand All @@ -84,7 +84,6 @@ Not all kind of slow query logs have been tested yet:
- [X] MariaDB
- [ ] Percona-db
- [X] Percona-cluster (pxc)
- [ ] PostgreSQL

## Contributing

Expand Down
4 changes: 2 additions & 2 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func main() {
break
}

// showQueries(q)
// showQuery(q)

count++
}
elapsed := time.Since(start)
fmt.Printf("\nparsed %d queries in %s\n", count, elapsed)
}

func showQueries(q query.Query) {
func showQuery(q query.Query) {
fmt.Printf("Time: %s\nUser: %s\nHost: %s\nID: %d\nSchema: %s\nLast_errno: %d\nKilled: %d\nQuery_time: %f\nLock_time: %f\nRows_sent: %d\nRows_examined: %d\nRows_affected: %d\nBytes_sent: %d\nQuery: %s\n",
q.Time,
q.User,
Expand Down
16 changes: 15 additions & 1 deletion cmd/slowql-digest/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ package main

import (
"errors"
"io"
"sync"
"time"

"github.com/devops-works/slowql"
"github.com/devops-works/slowql/query"
"github.com/sirupsen/logrus"
)

type app struct {
mu sync.Mutex
logger *logrus.Logger
kind slowql.Kind
fd io.Reader
p slowql.Parser
res map[string]statistics
digestDuration time.Duration
queriesNumber int
}

func newApp(loglevel, kind string) (*app, error) {
var a app

Expand Down Expand Up @@ -56,6 +69,8 @@ func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
s.Hash = hash(s.Fingerprint)

a.mu.Lock()
defer a.mu.Unlock()

if cur, ok := a.res[s.Hash]; ok {
// there is already results
cur.Calls++
Expand Down Expand Up @@ -99,7 +114,6 @@ func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
// add the entry to the map
a.res[s.Hash] = s
}
a.mu.Unlock()

return nil
}
14 changes: 2 additions & 12 deletions cmd/slowql-digest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ import (
"github.com/sirupsen/logrus"
)

type app struct {
mu sync.Mutex
logger *logrus.Logger
kind slowql.Kind
fd io.Reader
p slowql.Parser
res map[string]statistics
digestDuration time.Duration
queriesNumber int
}

type options struct {
logfile string
loglevel string
Expand Down Expand Up @@ -170,12 +159,13 @@ func main() {
if err != nil {
a.logger.Errorf("cannot open log file to count lines: %s", err)
}
defer fd.Close()

lines, err := lineCounter(fd)
if err != nil {
a.logger.Errorf("cannot count lines in log file: %s", err)
}
a.logger.Infof("log file has %d lines", lines)
fd.Close()
}

var q query.Query
Expand Down
2 changes: 1 addition & 1 deletion database/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(qc chan query.Query) *Database {
return &p
}

func (db *Database) ParseBlocs(rawBlocs chan []string) {
func (db *Database) ParseBlocks(rawBlocs chan []string) {
for {
select {
case bloc := <-rawBlocs:
Expand Down
2 changes: 1 addition & 1 deletion database/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestDatabase_ParseBlocs(t *testing.T) {
db := New(qc)
t.Run(tt.name, func(t *testing.T) {
rawBlocs <- tt.bloc
go db.ParseBlocs(rawBlocs)
go db.ParseBlocks(rawBlocs)
q := <-db.WaitingList
if q != tt.refQuery {
t.Errorf("got = %v, want = %v", q, tt.refQuery)
Expand Down
4 changes: 2 additions & 2 deletions database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func New(qc chan query.Query) *Database {
return &p
}

// ParseBlocs parses query blocks
func (db *Database) ParseBlocs(rawBlocs chan []string) {
// ParseBlocks parses query blocks
func (db *Database) ParseBlocks(rawBlocs chan []string) {
for {
select {
case bloc := <-rawBlocs:
Expand Down
4 changes: 2 additions & 2 deletions database/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestDatabase_ParseServerMeta(t *testing.T) {
}
}

func TestDatabase_ParseBlocs(t *testing.T) {
func TestDatabase_ParseBlocks(t *testing.T) {
tests := []struct {
name string
bloc []string
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestDatabase_ParseBlocs(t *testing.T) {
db := New(qc)
t.Run(tt.name, func(t *testing.T) {
rawBlocs <- tt.bloc
go db.ParseBlocs(rawBlocs)
go db.ParseBlocks(rawBlocs)
q := <-db.WaitingList
if q != tt.refQuery {
t.Errorf("got = %v, want = %v", q, tt.refQuery)
Expand Down
18 changes: 9 additions & 9 deletions slowql.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Database interface {
// GetNext() Query
// // GetServerMeta returns informations about the SQL server in usage
// GetServerMeta() Server
ParseBlocs(rawBlocs chan []string)
ParseBlocks(rawBlocks chan []string)
ParseServerMeta(chan []string)
GetServerMeta() server.Server
}
Expand All @@ -46,19 +46,19 @@ type Database interface {
type Parser struct {
db Database
waitingList chan query.Query
rawBlocs chan []string
rawBlocks chan []string
servermeta chan []string
}

// NewParser returns a new parser depending on the desired kind
func NewParser(k Kind, r io.Reader) Parser {
var p Parser

p.rawBlocs = make(chan []string, 4096)
p.rawBlocks = make(chan []string, 4096)
p.servermeta = make(chan []string)
p.waitingList = make(chan query.Query, 4096)

go scan(*bufio.NewScanner(r), p.rawBlocs, p.servermeta)
go scan(*bufio.NewScanner(r), p.rawBlocks, p.servermeta)

switch k {
case MySQL, PXC:
Expand All @@ -68,7 +68,7 @@ func NewParser(k Kind, r io.Reader) Parser {
}

p.db.ParseServerMeta(p.servermeta)
go p.db.ParseBlocs(p.rawBlocs)
go p.db.ParseBlocks(p.rawBlocks)

// This is gross but we are sure that some queries will be already parsed at
// when the user will call the package's functions
Expand All @@ -93,7 +93,7 @@ func (p *Parser) GetServerMeta() server.Server {
return p.db.GetServerMeta()
}

func scan(s bufio.Scanner, rawBlocs, servermeta chan []string) {
func scan(s bufio.Scanner, rawBlocks, servermeta chan []string) {
var bloc []string
inHeader, inQuery := false, false

Expand Down Expand Up @@ -122,7 +122,7 @@ func scan(s bufio.Scanner, rawBlocs, servermeta chan []string) {
// the first one
inQuery = false
if len(bloc) > 0 {
rawBlocs <- bloc
rawBlocks <- bloc
bloc = nil
}
}
Expand All @@ -143,7 +143,7 @@ func scan(s bufio.Scanner, rawBlocs, servermeta chan []string) {
}

// Send the last bloc
rawBlocs <- bloc
rawBlocks <- bloc

close(rawBlocs)
close(rawBlocks)
}

0 comments on commit 69609f8

Please sign in to comment.