Skip to content

Commit

Permalink
Merge #594
Browse files Browse the repository at this point in the history
594: Enhance encoding algorithm and improve naming convention r=curquiza a=Ja7ad

## What does this PR do?
- Fix return buffers to the pool after use
- Add encoding and decoding benchmark
- Improve naming convention

## PR checklist
Please check if your PR fulfills the following requirements:
- [ ] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Javad <[email protected]>
  • Loading branch information
meili-bors[bot] and Ja7ad authored Dec 30, 2024
2 parents 6f83ef3 + 12e4658 commit c167439
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 15 deletions.
2 changes: 2 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (g *gzipEncoder) Encode(rc io.Reader) (*bytes.Buffer, error) {
}()

buf := g.bufferPool.Get().(*bytes.Buffer)
defer g.bufferPool.Put(buf)

buf.Reset()
w.writer.Reset(buf)

Expand Down
148 changes: 148 additions & 0 deletions encoding_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package meilisearch

import (
"bytes"
"encoding/json"
"testing"
)

func BenchmarkGzipEncoder(b *testing.B) {
encoder := newEncoding(GzipEncoding, DefaultCompression)
data := bytes.NewReader(make([]byte, 1024*1024)) // 1 MB of data
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
buf, err := encoder.Encode(data)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}
_ = buf
}
}

func BenchmarkDeflateEncoder(b *testing.B) {
encoder := newEncoding(DeflateEncoding, DefaultCompression)
data := bytes.NewReader(make([]byte, 1024*1024)) // 1 MB of data
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
buf, err := encoder.Encode(data)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}
_ = buf
}
}

func BenchmarkBrotliEncoder(b *testing.B) {
encoder := newEncoding(BrotliEncoding, DefaultCompression)
data := bytes.NewReader(make([]byte, 1024*1024)) // 1 MB of data
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
buf, err := encoder.Encode(data)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}
_ = buf
}
}

func BenchmarkGzipDecoder(b *testing.B) {
encoder := newEncoding(GzipEncoding, DefaultCompression)

// Prepare a valid JSON input
data := map[string]interface{}{
"key1": "value1",
"key2": 12345,
"key3": []string{"item1", "item2", "item3"},
}
jsonData, err := json.Marshal(data)
if err != nil {
b.Fatalf("JSON marshal failed: %v", err)
}

// Encode the valid JSON data
input := bytes.NewReader(jsonData)
encoded, err := encoder.Encode(input)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
var result map[string]interface{}
if err := encoder.Decode(encoded.Bytes(), &result); err != nil {
b.Fatalf("Decode failed: %v", err)
}
}
}

func BenchmarkFlateDecoder(b *testing.B) {
encoder := newEncoding(DeflateEncoding, DefaultCompression)

// Prepare valid JSON input
data := map[string]interface{}{
"key1": "value1",
"key2": 12345,
"key3": []string{"item1", "item2", "item3"},
}
jsonData, err := json.Marshal(data)
if err != nil {
b.Fatalf("JSON marshal failed: %v", err)
}

// Encode the valid JSON data
input := bytes.NewReader(jsonData)
encoded, err := encoder.Encode(input)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
var result map[string]interface{}
if err := encoder.Decode(encoded.Bytes(), &result); err != nil {
b.Fatalf("Decode failed: %v", err)
}
}
}

func BenchmarkBrotliDecoder(b *testing.B) {
encoder := newEncoding(BrotliEncoding, DefaultCompression)

// Prepare valid JSON input
data := map[string]interface{}{
"key1": "value1",
"key2": 12345,
"key3": []string{"item1", "item2", "item3"},
}
jsonData, err := json.Marshal(data)
if err != nil {
b.Fatalf("JSON marshal failed: %v", err)
}

// Encode the valid JSON data
input := bytes.NewReader(jsonData)
encoded, err := encoder.Encode(input)
if err != nil {
b.Fatalf("Encode failed: %v", err)
}

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
var result map[string]interface{}
if err := encoder.Decode(encoded.Bytes(), &result); err != nil {
b.Fatalf("Decode failed: %v", err)
}
}
}
10 changes: 5 additions & 5 deletions meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ func Connect(host string, options ...Option) (ServiceManager, error) {
return meili, nil
}

func (m *meilisearch) GetServiceReader() ServiceReader {
func (m *meilisearch) ServiceReader() ServiceReader {
return m
}

func (m *meilisearch) GetTaskManager() TaskManager {
func (m *meilisearch) TaskManager() TaskManager {
return m
}

func (m *meilisearch) GetTaskReader() TaskReader {
func (m *meilisearch) TaskReader() TaskReader {
return m
}

func (m *meilisearch) GetKeyManager() KeyManager {
func (m *meilisearch) KeyManager() KeyManager {
return m
}

func (m *meilisearch) GetKeyReader() KeyReader {
func (m *meilisearch) KeyReader() KeyReader {
return m
}

Expand Down
12 changes: 7 additions & 5 deletions meilisearch_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ type ServiceManager interface {
KeyManager
TaskManager

GetServiceReader() ServiceReader
GetTaskManager() TaskManager
GetTaskReader() TaskReader
GetKeyManager() KeyManager
GetKeyReader() KeyReader
ServiceReader() ServiceReader

TaskManager() TaskManager
TaskReader() TaskReader

KeyManager() KeyManager
KeyReader() KeyReader

// CreateIndex creates a new index.
CreateIndex(config *IndexConfig) (*TaskInfo, error)
Expand Down
10 changes: 5 additions & 5 deletions meilisearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2335,9 +2335,9 @@ func Test_CreateSnapshot(t *testing.T) {

func TestGetServiceManagerAndReaders(t *testing.T) {
c := setup(t, "")
require.NotNil(t, c.GetServiceReader())
require.NotNil(t, c.GetTaskManager())
require.NotNil(t, c.GetTaskReader())
require.NotNil(t, c.GetKeyManager())
require.NotNil(t, c.GetKeyReader())
require.NotNil(t, c.ServiceReader())
require.NotNil(t, c.TaskManager())
require.NotNil(t, c.TaskReader())
require.NotNil(t, c.KeyManager())
require.NotNil(t, c.KeyReader())
}

0 comments on commit c167439

Please sign in to comment.