From c975e9ef52f3c3ccac71d306d338a83508bb7692 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 5 May 2022 19:08:49 +0300 Subject: [PATCH 1/5] [vegz] Add simple sync.Pool task. --- vegz/README.md | 3 +++ vegz/encode.go | 17 ++++++++++++ vegz/encode_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ vegz/solution.go | 31 ++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 vegz/README.md create mode 100644 vegz/encode.go create mode 100644 vegz/encode_test.go create mode 100644 vegz/solution.go diff --git a/vegz/README.md b/vegz/README.md new file mode 100644 index 0000000..a23f8d5 --- /dev/null +++ b/vegz/README.md @@ -0,0 +1,3 @@ +## vegz [runtime] + +В этой задаче нужно победить бенчмарк, переписав функцию сериализации в `gzip`. diff --git a/vegz/encode.go b/vegz/encode.go new file mode 100644 index 0000000..37b8cd0 --- /dev/null +++ b/vegz/encode.go @@ -0,0 +1,17 @@ +//go:build !solution + +package vegz + +import ( + "compress/gzip" + "io" +) + +func Encode(data []byte, w io.Writer) error { + ww := gzip.NewWriter(w) + defer func() { _ = ww.Close() }() + if _, err := ww.Write(data); err != nil { + return err + } + return ww.Flush() +} diff --git a/vegz/encode_test.go b/vegz/encode_test.go new file mode 100644 index 0000000..6bd1e58 --- /dev/null +++ b/vegz/encode_test.go @@ -0,0 +1,63 @@ +package vegz + +import ( + "bytes" + "compress/gzip" + "io" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/slon/shad-go/tools/testtool" +) + +func BenchmarkEncode(b *testing.B) { + data := []byte( + "New function should generally only return pointer types, " + + "since a pointer can be put into the return interface " + + "value without an allocation. " + + testtool.RandomName(), + ) + + b.ResetTimer() + b.ReportAllocs() + for n := 0; n < b.N; n++ { + require.NoError(b, Encode(data, io.Discard)) + } +} + +func TestRoundTrip(t *testing.T) { + testCases := []struct { + name string + in string + }{ + {name: "empty", in: ""}, + {name: "simple", in: "A long time ago in a galaxy far, far away..."}, + {name: "random", in: testtool.RandomName()}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + buf := new(bytes.Buffer) + require.NoError(t, Encode([]byte(tc.in), buf)) + + out, err := decode(buf.Bytes()) + require.NoError(t, err, tc.in) + require.Equal(t, tc.in, string(out)) + }) + + } +} + +func decode(data []byte) ([]byte, error) { + r, err := gzip.NewReader(bytes.NewReader(data)) + if err != nil { + return nil, err + } + + buf := &bytes.Buffer{} + if _, err := io.Copy(buf, r); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} diff --git a/vegz/solution.go b/vegz/solution.go new file mode 100644 index 0000000..2f6eb2c --- /dev/null +++ b/vegz/solution.go @@ -0,0 +1,31 @@ +//go:build solution + +package vegz + +import ( + "compress/gzip" + "io" + "sync" +) + +var writerPool = sync.Pool{ + New: func() interface{} { + return new(gzip.Writer) + }, +} + +func Encode(data []byte, w io.Writer) error { + ww := writerPool.Get().(*gzip.Writer) + defer func() { + _ = ww.Close() + writerPool.Put(ww) + }() + + ww.Reset(w) + + if _, err := ww.Write(data); err != nil { + return err + } + + return ww.Close() +} From a5e79ba175970f2207387e54623ec41ea150d8de Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 5 May 2022 19:10:19 +0300 Subject: [PATCH 2/5] [vegz] Update readme. --- vegz/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/vegz/README.md b/vegz/README.md index a23f8d5..30ae47a 100644 --- a/vegz/README.md +++ b/vegz/README.md @@ -1,3 +1,21 @@ ## vegz [runtime] В этой задаче нужно победить бенчмарк, переписав функцию сериализации в `gzip`. + +Пример запуска бенчмарка для бейзлайна и авторского решения: +``` +goos: linux +goarch: amd64 +pkg: gitlab.com/slon/shad-go/vegz +cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz +BenchmarkEncodeSimple +BenchmarkEncodeSimple-8 8307 124841 ns/op 813872 B/op 17 allocs/op +BenchmarkEncode +BenchmarkEncode-8 2094512 620.0 ns/op 0 B/op 0 allocs/op +PASS +ok gitlab.com/slon/shad-go/vegz 3.756s +``` + +### С чего начать? + +Запустите бенчмарк локально. Найдите в коде `compress/gzip` откуда берутся эти 800 килобайт на операцию? From 19030efe9ed9927f30918cff853097084fce5ba2 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 5 May 2022 19:20:16 +0300 Subject: [PATCH 3/5] [vegz] Rename to gzep. --- {vegz => gzep}/README.md | 6 +++--- {vegz => gzep}/encode.go | 2 +- {vegz => gzep}/encode_test.go | 2 +- vegz/solution.go | 31 ------------------------------- 4 files changed, 5 insertions(+), 36 deletions(-) rename {vegz => gzep}/README.md (89%) rename {vegz => gzep}/encode.go (94%) rename {vegz => gzep}/encode_test.go (98%) delete mode 100644 vegz/solution.go diff --git a/vegz/README.md b/gzep/README.md similarity index 89% rename from vegz/README.md rename to gzep/README.md index 30ae47a..cec4851 100644 --- a/vegz/README.md +++ b/gzep/README.md @@ -1,4 +1,4 @@ -## vegz [runtime] +## gzep [runtime] В этой задаче нужно победить бенчмарк, переписав функцию сериализации в `gzip`. @@ -6,14 +6,14 @@ ``` goos: linux goarch: amd64 -pkg: gitlab.com/slon/shad-go/vegz +pkg: gitlab.com/slon/shad-go/gzep cpu: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz BenchmarkEncodeSimple BenchmarkEncodeSimple-8 8307 124841 ns/op 813872 B/op 17 allocs/op BenchmarkEncode BenchmarkEncode-8 2094512 620.0 ns/op 0 B/op 0 allocs/op PASS -ok gitlab.com/slon/shad-go/vegz 3.756s +ok gitlab.com/slon/shad-go/gzep 3.756s ``` ### С чего начать? diff --git a/vegz/encode.go b/gzep/encode.go similarity index 94% rename from vegz/encode.go rename to gzep/encode.go index 37b8cd0..53feff2 100644 --- a/vegz/encode.go +++ b/gzep/encode.go @@ -1,6 +1,6 @@ //go:build !solution -package vegz +package gzep import ( "compress/gzip" diff --git a/vegz/encode_test.go b/gzep/encode_test.go similarity index 98% rename from vegz/encode_test.go rename to gzep/encode_test.go index 6bd1e58..2abcf87 100644 --- a/vegz/encode_test.go +++ b/gzep/encode_test.go @@ -1,4 +1,4 @@ -package vegz +package gzep import ( "bytes" diff --git a/vegz/solution.go b/vegz/solution.go deleted file mode 100644 index 2f6eb2c..0000000 --- a/vegz/solution.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build solution - -package vegz - -import ( - "compress/gzip" - "io" - "sync" -) - -var writerPool = sync.Pool{ - New: func() interface{} { - return new(gzip.Writer) - }, -} - -func Encode(data []byte, w io.Writer) error { - ww := writerPool.Get().(*gzip.Writer) - defer func() { - _ = ww.Close() - writerPool.Put(ww) - }() - - ww.Reset(w) - - if _, err := ww.Write(data); err != nil { - return err - } - - return ww.Close() -} From e62a9241057c2fbe095bb1eb1aeafce26344b913 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 5 May 2022 22:14:33 +0300 Subject: [PATCH 4/5] [gzep] Fix goimports & readme. --- gzep/README.md | 2 +- gzep/encode_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gzep/README.md b/gzep/README.md index cec4851..7d63498 100644 --- a/gzep/README.md +++ b/gzep/README.md @@ -1,6 +1,6 @@ ## gzep [runtime] -В этой задаче нужно победить бенчмарк, переписав функцию сериализации в `gzip`. +В этой задаче нужно победить бенчмарк, "улучшив" функцию сериализации в `gzip`. Пример запуска бенчмарка для бейзлайна и авторского решения: ``` diff --git a/gzep/encode_test.go b/gzep/encode_test.go index 2abcf87..8b9ad78 100644 --- a/gzep/encode_test.go +++ b/gzep/encode_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/slon/shad-go/tools/testtool" ) From 4d49e65fc997b719c22c779f995295cf855c686b Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 5 May 2022 22:18:25 +0300 Subject: [PATCH 5/5] Release analysis. --- .deadlines.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.deadlines.yml b/.deadlines.yml index 466fb46..555c372 100644 --- a/.deadlines.yml +++ b/.deadlines.yml @@ -1,3 +1,12 @@ +- group: Analysis + start: 05-05-2022 18:00 + deadline: 15-05-2022 23:59 + tasks: + - task: testifycheck + score: 200 + - task: gzep + score: 100 + - group: Bonus start: 14-04-2022 17:59 deadline: 22-05-2022 23:59