Skip to content

Commit

Permalink
Merge pull request #1 from Trendyol/doc/benchmark-v2
Browse files Browse the repository at this point in the history
Doc/benchmark v2
  • Loading branch information
GokselKUCUKSAHIN authored Sep 3, 2024
2 parents cb21fda + 12aeda0 commit c44d1fc
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 298 deletions.
162 changes: 150 additions & 12 deletions README.md

Large diffs are not rendered by default.

30 changes: 19 additions & 11 deletions benchmarks/.runbench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

benchmark_test() {
local file=$1
echo "Running benchmark for $file"
go test -bench=. $file -benchtime=5s
echo "$(date +"%Y-%m-%d %H:%M:%S") Running benchmark for $file"
go test -bench=. -benchtime=5s "$file" 2>&1 | tee "$file.log"
if [ $? -ne 0 ]; then
echo "Error running benchmark for $file. Check $file.log for details."
fi
}

test_files=$(find . -type f -name "*_test.go")
test_files=$(find . -name "*_test.go" 2>/dev/null)

for file in $test_files;
do
benchmark_test $file
echo "Waiting for 30 seconds before the next test..."
sleep 30
done

echo "All benchmarks completed."
if [ $? -eq 0 ]; then
start_time=$(date +%s)
for file in $test_files;
do
benchmark_test "$file"
sleep 30
done
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "All benchmarks completed in $elapsed_time seconds."
else
echo "Error finding test files."
fi
105 changes: 51 additions & 54 deletions benchmarks/aggs_example_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package benchmarks_test

import (
"encoding/json"
"testing"

"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/es/enums/sort/order"
"github.com/Trendyol/es-query-builder/test/assert"
)

//// Aggs Example ////

func createAggsQuery() string {
func createAggsQuery() map[string]any {
query := es.NewQuery(
es.Bool().
Must(
Expand All @@ -22,38 +19,32 @@ func createAggsQuery() string {
).
MustNot(
es.Exists("file.name"),
),
)

query.
)).
Size(5_000).
Sort(
es.Sort("modifiedDate").Order(order.Desc),
es.Sort("indexedAt").Order(order.Desc),
)

query.Aggs("DocumentIds",
es.AggTerms().
Field("document.id").
es.Sort("indexedAt").Order(order.Asc),
).
Aggs("by_category", es.AggTerms().
Field("category.keyword").
Size(250).
Aggs("OrderCounts",
es.AggMultiTerms().
Terms(
es.AggTerm("document.orders.count"),
es.AggTerm("files.order.count").
Missing("book.meta.author"),
Aggs("nested_reviews", es.AggNested().
Path("reviews").
Aggs("average_rating", es.AggAvg().
Field("reviews.rating"),
).
Aggs("by_reviewer", es.AggTerms().
Field("reviews.reviewer.keyword").
Aggs("max_reviewer_rating", es.AggMax().
Field("reviews.rating"),
),
),
),
)

marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
)
return query
}

func createAggsQueryVanillaGo() string {
func createAggsQueryVanilla() map[string]any {
query := map[string]interface{}{
"size": 5000,
"sort": []map[string]interface{}{
Expand All @@ -64,7 +55,7 @@ func createAggsQueryVanillaGo() string {
},
{
"indexedAt": map[string]interface{}{
"order": "desc",
"order": "asc",
},
},
},
Expand Down Expand Up @@ -95,21 +86,32 @@ func createAggsQueryVanillaGo() string {
},
},
"aggs": map[string]interface{}{
"DocumentIds": map[string]interface{}{
"by_category": map[string]interface{}{
"terms": map[string]interface{}{
"field": "document.id",
"field": "category.keyword",
"size": 250,
},
"aggs": map[string]interface{}{
"OrderCounts": map[string]interface{}{
"multi_terms": map[string]interface{}{
"terms": []map[string]interface{}{
{
"field": "document.orders.count",
"nested_reviews": map[string]interface{}{
"nested": map[string]interface{}{
"path": "reviews",
},
"aggs": map[string]interface{}{
"average_rating": map[string]interface{}{
"avg": map[string]interface{}{
"field": "reviews.rating",
},
{
"field": "files.order.count",
"missing": "book.meta.author",
},
"by_reviewer": map[string]interface{}{
"terms": map[string]interface{}{
"field": "reviews.reviewer.keyword",
},
"aggs": map[string]interface{}{
"max_reviewer_rating": map[string]interface{}{
"max": map[string]interface{}{
"field": "reviews.rating",
},
},
},
},
},
Expand All @@ -118,18 +120,7 @@ func createAggsQueryVanillaGo() string {
},
},
}

marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
}

func Test_Aggs_Queries_are_equal(t *testing.T) {
build := createAggsQuery()
vanilla := createAggsQueryVanillaGo()
assert.Equal(t, vanilla, build)
return query
}

func Benchmark_Aggs_Example_Builder(b *testing.B) {
Expand All @@ -140,10 +131,16 @@ func Benchmark_Aggs_Example_Builder(b *testing.B) {
}
}

func Benchmark_Aggs_Example_VanillaGo(b *testing.B) {
createAggsQueryVanillaGo()
func Benchmark_Aggs_Example_Vanilla(b *testing.B) {
createAggsQueryVanilla()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createAggsQueryVanillaGo()
createAggsQueryVanilla()
}
}

func Test_Aggs_Queries_are_equal(t *testing.T) {
builder := marshalString(t, createAggsQuery())
vanilla := marshalString(t, createAggsQueryVanilla())
assert.Equal(t, vanilla, builder)
}
72 changes: 28 additions & 44 deletions benchmarks/complex_example_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package benchmarks_test

import (
"encoding/json"
"testing"

"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/es/enums/sort/mode"
"github.com/Trendyol/es-query-builder/es/enums/sort/order"
"github.com/Trendyol/es-query-builder/test/assert"
)

//// Complex Example ////

func createComplexQuery(id int) string {
func createComplexQuery(id int) map[string]any {
query := es.NewQuery(
es.Bool().
Must(
es.Range("partition").
GreaterThan(25).
LesserThanOrEqual(30),
es.Bool().
Should(
es.Term("doc.id", id),
Expand All @@ -26,44 +25,35 @@ func createComplexQuery(id int) string {
Filter(
es.Term("type", "File"),
es.Terms("sector", 1, 2, 3),
es.Range("partition").
GreaterThan(25).
LesserThanOrEqual(30),
).
MustNot(
es.Exists("blocks.reason.id"),
).
MinimumShouldMatch(1).
Boost(3.14),
)
query.TrackTotalHits(true)
query.Size(100)
query.From(5000)
query.Sort(
es.Sort("modifiedDate").Order(order.Desc),
es.Sort("name").Order(order.Asc).Mode(mode.Median),
es.Sort("name").Order(order.Asc),
es.Sort("indexedAt").Order(order.Asc),
)
query.Source().
Includes("id", "type", "indexedAt", "chapters").
Excludes("private.key", "cipher")

marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
return query
}

func createComplexQueryVanillaGo(id int) string {
func createComplexQueryVanilla(id int) map[string]any {
query := map[string]interface{}{
"_source": map[string]interface{}{
"includes": []interface{}{"id", "type", "indexedAt", "chapters"},
"excludes": []interface{}{"private.key", "cipher"},
},
"size": 100,
"from": 5000,
"track_total_hits": true,
"size": 100,
"from": 5000,
"sort": []map[string]interface{}{
{
"modifiedDate": map[string]interface{}{
Expand All @@ -73,7 +63,6 @@ func createComplexQueryVanillaGo(id int) string {
{
"name": map[string]interface{}{
"order": "asc",
"mode": "median",
},
},
{
Expand All @@ -87,6 +76,14 @@ func createComplexQueryVanillaGo(id int) string {
"minimum_should_match": 1,
"boost": 3.14,
"must": []map[string]interface{}{
{
"range": map[string]interface{}{
"partition": map[string]interface{}{
"gt": 25,
"lte": 30,
},
},
},
{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
Expand Down Expand Up @@ -122,14 +119,6 @@ func createComplexQueryVanillaGo(id int) string {
},
},
},
{
"range": map[string]interface{}{
"partition": map[string]interface{}{
"gt": 25,
"lte": 30,
},
},
},
},
"must_not": []map[string]interface{}{
{
Expand All @@ -141,19 +130,7 @@ func createComplexQueryVanillaGo(id int) string {
},
},
}

marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
}

func Test_Complex_Queries_are_equal(t *testing.T) {
id := 76
build := createComplexQuery(id)
vanilla := createComplexQueryVanillaGo(id)
assert.Equal(t, vanilla, build)
return query
}

func Benchmark_Complex_Builder(b *testing.B) {
Expand All @@ -165,11 +142,18 @@ func Benchmark_Complex_Builder(b *testing.B) {
}
}

func Benchmark_Complex_VanillaGo(b *testing.B) {
func Benchmark_Complex_Vanilla(b *testing.B) {
id := 76
createComplexQueryVanillaGo(id)
createComplexQueryVanilla(id)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createComplexQueryVanillaGo(id)
createComplexQueryVanilla(id)
}
}

func Test_Complex_Queries_are_equal(t *testing.T) {
id := 76
build := marshalString(t, createComplexQuery(id))
vanilla := marshalString(t, createComplexQueryVanilla(id))
assert.Equal(t, vanilla, build)
}
Loading

0 comments on commit c44d1fc

Please sign in to comment.