Skip to content

Commit 0ab4467

Browse files
committed
started implementing digest basics
1 parent c25ff83 commit 0ab4467

5 files changed

Lines changed: 403 additions & 55 deletions

File tree

cmd/slowql-digest/Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
GOCMD=go
3+
GOTEST=$(GOCMD) test
4+
GOVET=$(GOCMD) vet
5+
BINARY_NAME=slowql-digest
6+
BUILD_DIR=./out/bin/
7+
BUILD_ROOT=./out
8+
9+
GREEN := $(shell tput -Txterm setaf 2)
10+
YELLOW := $(shell tput -Txterm setaf 3)
11+
WHITE := $(shell tput -Txterm setaf 7)
12+
RESET := $(shell tput -Txterm sgr0)
13+
14+
.PHONY: all build clean
15+
16+
all: help
17+
18+
## Build:
19+
build: ## Build the Go project
20+
mkdir -p ./out/bin
21+
GO111MODULE=on $(GOCMD) build -o $(BUILD_DIR)$(BINARY_NAME) .
22+
@echo "${GREEN}[*]${RESET} ${BINARY_NAME} successfully built in ${YELLOW}${BUILD_DIR}${BINARY_NAME}${RESET}"
23+
24+
clean: ## Clean all the files and binaries generated by the Makefile
25+
rm -rf $(BUILD_ROOT)
26+
rm -f ./profile.cov
27+
28+
## Test:
29+
test: ## Run the tests of the project
30+
$(GOTEST) -v -race ./...
31+
32+
coverage: ## Run the tests of the project and export the coverage
33+
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
34+
$(GOCMD) tool cover -func profile.cov
35+
36+
## Help:
37+
help: ## Show this help
38+
@echo ''
39+
@echo 'Usage:'
40+
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
41+
@echo ''
42+
@echo 'Targets:'
43+
@awk 'BEGIN {FS = ":.*?## "} { \
44+
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
45+
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
46+
}' $(MAKEFILE_LIST)
47+

cmd/slowql-digest/app.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"sync"
6+
"time"
7+
8+
"github.com/devops-works/slowql"
9+
"github.com/devops-works/slowql/query"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func newApp(loglevel, kind string) (*app, error) {
14+
var a app
15+
16+
// init res map
17+
a.res = make(map[string]statistics)
18+
19+
// create application logger
20+
a.logger = logrus.New()
21+
switch loglevel {
22+
case "trace":
23+
a.logger.SetLevel(logrus.TraceLevel)
24+
case "debug":
25+
a.logger.SetLevel(logrus.DebugLevel)
26+
case "info":
27+
a.logger.SetLevel(logrus.InfoLevel)
28+
case "warn":
29+
a.logger.SetLevel(logrus.WarnLevel)
30+
case "error", "err":
31+
a.logger.SetLevel(logrus.ErrorLevel)
32+
case "fatal":
33+
a.logger.SetLevel(logrus.FatalLevel)
34+
default:
35+
return nil, errors.New("log level not recognised: " + loglevel)
36+
}
37+
38+
// convert kind from string to slowql.Kind
39+
switch kind {
40+
case "mysql":
41+
a.kind = slowql.MySQL
42+
case "mariadb":
43+
a.kind = slowql.MariaDB
44+
case "pxc":
45+
a.kind = slowql.PXC
46+
default:
47+
return nil, errors.New("kind not recognised: " + kind)
48+
}
49+
50+
return &a, nil
51+
}
52+
53+
func (a *app) digest(q query.Query, wg *sync.WaitGroup) error {
54+
defer wg.Done()
55+
var s statistics
56+
s.fingerprint = fingerprint(q.Query)
57+
s.hash = hash(s.fingerprint)
58+
59+
a.mu.Lock()
60+
if cur, ok := a.res[s.hash]; ok {
61+
// there is already results
62+
cur.calls++
63+
cur.cumBytesSent += q.BytesSent
64+
cur.cumKilled += q.Killed
65+
cur.cumLockTime += time.Duration(q.LockTime)
66+
cur.cumRowsExamined += q.RowsExamined
67+
cur.cumRowsSent += q.RowsSent
68+
69+
// update the entry in the map
70+
a.res[s.hash] = cur
71+
} else {
72+
// it is the first time this hash appears
73+
s.calls++
74+
s.cumBytesSent = q.BytesSent
75+
s.cumKilled = q.Killed
76+
s.cumLockTime = time.Duration(q.LockTime)
77+
s.cumRowsExamined = q.RowsExamined
78+
s.cumRowsSent = q.RowsSent
79+
80+
// getting those values is done only once: same hash == same fingerprint & schema
81+
s.schema = q.Schema
82+
83+
// add the entry to the map
84+
a.res[s.hash] = s
85+
}
86+
a.mu.Unlock()
87+
88+
return nil
89+
}

0 commit comments

Comments
 (0)