Skip to content

Commit

Permalink
Migrate to faster JSON library (#118)
Browse files Browse the repository at this point in the history
Service log scraping is mainly spending time on marshalling and
unmarshalling log entries. Migration to json-iterator doubles the
performance.
  • Loading branch information
medzin authored Jun 29, 2018
1 parent a862a4f commit 8257050
Show file tree
Hide file tree
Showing 212 changed files with 20,395 additions and 6 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@
[[constraint]]
name = "github.com/allegro/go-metrics-graphite"
revision = "d8aec135f2c1e2d0fd86f55e9b85427ba6592490"

[[constraint]]
name = "github.com/json-iterator/go"
version = "1.1.3"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ package: $(BUILD_FOLDER)/executor
chmod 0755 $(BUILD_FOLDER)/executor-$(APPLICATION_VERSION)-linux-amd64.zip

test: test-deps
go test -v -coverprofile=$(BUILD_FOLDER)/coverage.txt -covermode=atomic ./...
go test -coverprofile=$(BUILD_FOLDER)/coverage.txt -covermode=atomic ./...

test-deps: $(BUILD_FOLDER)
./scripts/install-consul.sh
8 changes: 5 additions & 3 deletions servicelog/appender/logstash.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package appender

import (
"encoding/json"
"fmt"
"io"
"net"
"time"

"github.com/allegro/mesos-executor/xio"
"github.com/allegro/mesos-executor/xnet"
"github.com/hashicorp/consul/api"
"github.com/json-iterator/go"
"github.com/kelseyhightower/envconfig"
"github.com/rcrowley/go-metrics"
log "github.com/sirupsen/logrus"

"github.com/allegro/mesos-executor/servicelog"
"github.com/allegro/mesos-executor/xio"
"github.com/allegro/mesos-executor/xnet"
)

const (
logstashVersion = 1
logstashConfigPrefix = "allegro_executor_servicelog_logstash"
)

var json = jsoniter.ConfigFastest

type logstashConfig struct {
Protocol string `default:"tcp"`
Address string
Expand Down
4 changes: 3 additions & 1 deletion servicelog/scraper/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package scraper

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"time"

"github.com/json-iterator/go"
log "github.com/sirupsen/logrus"

"github.com/allegro/mesos-executor/servicelog"
Expand All @@ -18,6 +18,8 @@ const (
megabyte = 1024 * kilobyte
)

var json = jsoniter.ConfigFastest

// JSON is a scraper for logs represented as JSON objects.
type JSON struct {
InvalidLogsWriter io.Writer
Expand Down
20 changes: 20 additions & 0 deletions servicelog/scraper/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"testing"
"time"

Expand Down Expand Up @@ -109,6 +110,25 @@ func TestIfIgnoresEmptyLogLines(t *testing.T) {
assert.NoError(t, err2)
}

func BenchmarkJSONScraping(b *testing.B) {
exampleLog, err := ioutil.ReadFile("testdata/log.json")
if err != nil {
b.Fatal(err)
}
reader, writer := io.Pipe()
scraper := JSON{}

entries := scraper.StartScraping(reader)
go func() {
for {
<-entries
}
}()
for i := 0; i < b.N; i++ {
writer.Write(exampleLog)
}
}

func noEntryWithTimeout(entries <-chan servicelog.Entry, timeout time.Duration) error {
timeoutChan := time.After(timeout)
select {
Expand Down
1 change: 1 addition & 0 deletions servicelog/scraper/testdata/log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level": "INFO", "logger": "logger", "message": "In velit dolore duis dolor aute magna enim ex et excepteur et minim nostrud sint. Velit dolore deserunt Lorem eiusmod est elit nisi in. Sit tempor magna velit culpa eu proident. Ullamco eiusmod laborum eiusmod non laboris nostrud reprehenderit et officia in nulla cupidatat amet Lorem. Magna excepteur excepteur sint sint do aute. Qui eiusmod irure mollit consequat magna dolore minim nostrud exercitation. Proident officia amet dolor aliqua incididunt velit officia sit est aliquip officia."}
3 changes: 3 additions & 0 deletions vendor/github.com/json-iterator/go/.codecov.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/json-iterator/go/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/json-iterator/go/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/json-iterator/go/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/json-iterator/go/Gopkg.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/json-iterator/go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions vendor/github.com/json-iterator/go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8257050

Please sign in to comment.