-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
403 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
GOCMD=go | ||
GOTEST=$(GOCMD) test | ||
GOVET=$(GOCMD) vet | ||
BINARY_NAME=slowql-digest | ||
BUILD_DIR=./out/bin/ | ||
BUILD_ROOT=./out | ||
|
||
GREEN := $(shell tput -Txterm setaf 2) | ||
YELLOW := $(shell tput -Txterm setaf 3) | ||
WHITE := $(shell tput -Txterm setaf 7) | ||
RESET := $(shell tput -Txterm sgr0) | ||
|
||
.PHONY: all build clean | ||
|
||
all: help | ||
|
||
## Build: | ||
build: ## Build the Go project | ||
mkdir -p ./out/bin | ||
GO111MODULE=on $(GOCMD) build -o $(BUILD_DIR)$(BINARY_NAME) . | ||
@echo "${GREEN}[*]${RESET} ${BINARY_NAME} successfully built in ${YELLOW}${BUILD_DIR}${BINARY_NAME}${RESET}" | ||
|
||
clean: ## Clean all the files and binaries generated by the Makefile | ||
rm -rf $(BUILD_ROOT) | ||
rm -f ./profile.cov | ||
|
||
## Test: | ||
test: ## Run the tests of the project | ||
$(GOTEST) -v -race ./... | ||
|
||
coverage: ## Run the tests of the project and export the coverage | ||
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./... | ||
$(GOCMD) tool cover -func profile.cov | ||
|
||
## Help: | ||
help: ## Show this help | ||
@echo '' | ||
@echo 'Usage:' | ||
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' | ||
@echo '' | ||
@echo 'Targets:' | ||
@awk 'BEGIN {FS = ":.*?## "} { \ | ||
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \ | ||
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ | ||
}' $(MAKEFILE_LIST) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/devops-works/slowql" | ||
"github.com/devops-works/slowql/query" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func newApp(loglevel, kind string) (*app, error) { | ||
var a app | ||
|
||
// init res map | ||
a.res = make(map[string]statistics) | ||
|
||
// create application logger | ||
a.logger = logrus.New() | ||
switch loglevel { | ||
case "trace": | ||
a.logger.SetLevel(logrus.TraceLevel) | ||
case "debug": | ||
a.logger.SetLevel(logrus.DebugLevel) | ||
case "info": | ||
a.logger.SetLevel(logrus.InfoLevel) | ||
case "warn": | ||
a.logger.SetLevel(logrus.WarnLevel) | ||
case "error", "err": | ||
a.logger.SetLevel(logrus.ErrorLevel) | ||
case "fatal": | ||
a.logger.SetLevel(logrus.FatalLevel) | ||
default: | ||
return nil, errors.New("log level not recognised: " + loglevel) | ||
} | ||
|
||
// convert kind from string to slowql.Kind | ||
switch kind { | ||
case "mysql": | ||
a.kind = slowql.MySQL | ||
case "mariadb": | ||
a.kind = slowql.MariaDB | ||
case "pxc": | ||
a.kind = slowql.PXC | ||
default: | ||
return nil, errors.New("kind not recognised: " + kind) | ||
} | ||
|
||
return &a, nil | ||
} | ||
|
||
func (a *app) digest(q query.Query, wg *sync.WaitGroup) error { | ||
defer wg.Done() | ||
var s statistics | ||
s.fingerprint = fingerprint(q.Query) | ||
s.hash = hash(s.fingerprint) | ||
|
||
a.mu.Lock() | ||
if cur, ok := a.res[s.hash]; ok { | ||
// there is already results | ||
cur.calls++ | ||
cur.cumBytesSent += q.BytesSent | ||
cur.cumKilled += q.Killed | ||
cur.cumLockTime += time.Duration(q.LockTime) | ||
cur.cumRowsExamined += q.RowsExamined | ||
cur.cumRowsSent += q.RowsSent | ||
|
||
// update the entry in the map | ||
a.res[s.hash] = cur | ||
} else { | ||
// it is the first time this hash appears | ||
s.calls++ | ||
s.cumBytesSent = q.BytesSent | ||
s.cumKilled = q.Killed | ||
s.cumLockTime = time.Duration(q.LockTime) | ||
s.cumRowsExamined = q.RowsExamined | ||
s.cumRowsSent = q.RowsSent | ||
|
||
// getting those values is done only once: same hash == same fingerprint & schema | ||
s.schema = q.Schema | ||
|
||
// add the entry to the map | ||
a.res[s.hash] = s | ||
} | ||
a.mu.Unlock() | ||
|
||
return nil | ||
} |
Oops, something went wrong.