-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters