Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance encoding algorithm and improve naming convention #594

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -13 to -17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change breaking for users?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

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())
}
Loading