github.com/weaviate/weaviate@v1.24.6/test/acceptance/stress_tests/tenants_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 func TestConcurrentTenantsAddAndRemove(t *testing.T) { 25 url := "http://localhost:8080/v1/" 26 27 c := createHttpClient() 28 clearExistingObjects(c, url) 29 requestSchema := createSchemaRequest(url, class, true) 30 31 performRequest(c, requestSchema) 32 33 // Add schema and object 34 parallelReqs := 50 35 wg := sync.WaitGroup{} 36 wgStartReqests := sync.WaitGroup{} 37 wgStartReqests.Add(parallelReqs) 38 wg.Add(parallelReqs) 39 40 for i := 0; i < parallelReqs; i++ { 41 go func(j int) { 42 c := createHttpClient() 43 44 requestAddTenant := createRequest(url+"schema/"+class+"/tenants", "POST", []models.Tenant{{Name: "tenant" + fmt.Sprint(j)}}) 45 requestDeleteTenant := createRequest(url+"schema/"+class+"/tenants", "DELETE", []string{"tenant" + fmt.Sprint(j)}) 46 47 wgStartReqests.Done() 48 wgStartReqests.Wait() 49 performRequest(c, requestAddTenant) 50 performRequest(c, requestDeleteTenant) 51 52 wg.Done() 53 }(i) 54 } 55 wg.Wait() 56 57 requestGetTenants := createRequest(url+"schema/"+class+"/tenants", "GET", nil) 58 59 _, body, _ := performRequest(c, requestGetTenants) 60 var result []*models.Tenant 61 json.Unmarshal(body, &result) 62 assert.Equal(t, 0, len(result)) 63 } 64 65 func TestConcurrentTenantBatchesWithTenantAdd(t *testing.T) { 66 url := "http://localhost:8080/v1/" 67 68 c := createHttpClient() 69 clearExistingObjects(c, url) 70 71 tenants := make([]models.Tenant, 10) 72 for i := 0; i < len(tenants); i++ { 73 tenants[i] = models.Tenant{Name: "tenant" + fmt.Sprint(i)} 74 } 75 76 nrClasses := 10 77 for i := 0; i < nrClasses; i++ { 78 performRequest(c, createSchemaRequest(url, class+fmt.Sprint(i), true)) 79 performRequest(c, createRequest(url+"schema/"+class+fmt.Sprint(i)+"/tenants", "POST", tenants)) 80 } 81 82 // Add schema and object 83 parallelReqs := 20 84 wg := sync.WaitGroup{} 85 wgStartReqests := sync.WaitGroup{} 86 wgStartReqests.Add(parallelReqs) 87 wg.Add(parallelReqs) 88 89 batchSize := 100 90 91 for i := 0; i < parallelReqs; i++ { 92 go func(j int) { 93 c := createHttpClient() 94 95 requestBatch := createRequest(url+"batch/objects", "POST", batch{createBatch(class+fmt.Sprint(j%nrClasses), batchSize, tenants)}) 96 97 wgStartReqests.Done() 98 wgStartReqests.Wait() 99 performRequest(c, requestBatch) 100 wg.Done() 101 }(i) 102 } 103 wg.Wait() 104 105 // check for one class that we have the expected nr of objects per tenant 106 requestRead := createRequest(url+fmt.Sprintf("objects?limit=%v&class="+class+"%s&tenant=%s", 1000, "4", fmt.Sprint(tenants[0].Name)), "GET", nil) 107 _, body, _ := performRequest(c, requestRead) 108 var result map[string]interface{} 109 json.Unmarshal(body, &result) 110 assert.Equal(t, parallelReqs/nrClasses*batchSize/len(tenants), int(result["totalResults"].(float64))) // batches per class, * objects per batch/nr_tenants 111 clearExistingObjects(c, url) 112 }