github.com/weaviate/weaviate@v1.24.6/test/acceptance/stress_tests/concurrent_batches_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package stress_tests
    13  
    14  import (
    15  	"encoding/json"
    16  	"fmt"
    17  	"sync"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/weaviate/weaviate/entities/models"
    22  )
    23  
    24  type batch struct {
    25  	Objects []*models.Object
    26  }
    27  
    28  const class = "TestClass"
    29  
    30  func Test_AddConcurrentSchemas_sameObject(t *testing.T) {
    31  	url := "http://localhost:8080/v1/"
    32  	batch := batch{createObject(class)}
    33  	parallelReqs := 10
    34  	wg := sync.WaitGroup{}
    35  	wg.Add(parallelReqs)
    36  
    37  	// Add schema and object
    38  	c := createHttpClient()
    39  	clearExistingObjects(c, url)
    40  	requestSchema := createSchemaRequest(url, class, false)
    41  	performRequest(c, requestSchema)
    42  
    43  	for i := 0; i < parallelReqs; i++ {
    44  		go func(j int) {
    45  			defer wg.Done()
    46  			c := createHttpClient()
    47  			performRequest(c, createRequest(url+"batch/objects", "POST", batch))
    48  		}(i)
    49  	}
    50  	wg.Wait()
    51  
    52  	requestRead := createRequest(url+"objects?limit="+fmt.Sprint(10)+"&class="+class, "GET", nil)
    53  	_, body, _ := performRequest(c, requestRead)
    54  	var result map[string]interface{}
    55  	json.Unmarshal(body, &result)
    56  	assert.Equal(t, 1, int(result["totalResults"].(float64)))
    57  }
    58  
    59  func Test_AddConcurrentBatches_differentObjects(t *testing.T) {
    60  	url := "http://localhost:8080/v1/"
    61  
    62  	parallelReqs := 150
    63  	wg := sync.WaitGroup{}
    64  	wg.Add(parallelReqs)
    65  
    66  	// Add schema and object
    67  	c := createHttpClient()
    68  	clearExistingObjects(c, url)
    69  	requestSchema := createSchemaRequest(url, class, false)
    70  	performRequest(c, requestSchema)
    71  	batch1 := batch{createObject(class)}
    72  	batch2 := batch{createObject(class)}
    73  
    74  	for i := 0; i < parallelReqs; i++ {
    75  		go func(j int) {
    76  			defer wg.Done()
    77  
    78  			c := createHttpClient()
    79  			if j%2 == 0 {
    80  				performRequest(c, createRequest(url+"batch/objects", "POST", batch1))
    81  			} else {
    82  				performRequest(c, createRequest(url+"batch/objects", "POST", batch2))
    83  			}
    84  		}(i)
    85  	}
    86  	wg.Wait()
    87  
    88  	requestRead := createRequest(url+"objects?limit="+fmt.Sprint(10)+"&class="+class, "GET", nil)
    89  	_, body, _ := performRequest(c, requestRead)
    90  	var result map[string]interface{}
    91  	json.Unmarshal(body, &result)
    92  	assert.Equal(t, 2, int(result["totalResults"].(float64)))
    93  	clearExistingObjects(c, url)
    94  }