Skip to content

Commit

Permalink
fix: updates user query speed test (#892)
Browse files Browse the repository at this point in the history
* fix: update concurrent test

* fix: parallel get user

* fix: test

* fix: test

* fix: version update

* fix: changelog
  • Loading branch information
sattvikc authored Nov 28, 2023
1 parent ebaa349 commit ec8e355
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 20 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [7.0.14] - 2023-11-21

- Updates test user query speed

### Migration

If using MySQL plugin, run the following SQL script:

```sql
CREATE INDEX app_id_to_user_id_primary_user_id_index ON app_id_to_user_id (primary_or_recipe_user_id);
CREATE INDEX app_id_to_user_id_user_id_index ON app_id_to_user_id (user_id);
```

## [7.0.13] - 2023-11-21

- Adds test to user query speed
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "7.0.13"
version = "7.0.14"


repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static org.junit.Assert.assertNotNull;

Expand All @@ -57,6 +63,9 @@ public void beforeEach() {
public void testUserCreationLinkingAndGetByIdSpeeds() throws Exception {
String[] args = {"../"};
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
Utils.setValueInConfig("postgresql_connection_pool_size", "100");
Utils.setValueInConfig("mysql_connection_pool_size", "100");

FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{
EE_FEATURES.ACCOUNT_LINKING, EE_FEATURES.MULTI_TENANCY});
Expand All @@ -67,35 +76,49 @@ public void testUserCreationLinkingAndGetByIdSpeeds() throws Exception {
return;
}

if (!Version.getVersion(process.getProcess()).getPluginName().equals("postgresql")) {
return;
}

if (StorageLayer.isInMemDb(process.getProcess())) {
return;
}

int numberOfUsers = 10000;
List<String> userIds = new ArrayList<>();
List<String> userIds2 = new ArrayList<>();
Lock lock = new ReentrantLock();
{
ExecutorService es = Executors.newFixedThreadPool(32);
long start = System.currentTimeMillis();
for (int i = 0; i < numberOfUsers; i++) {
String email = "user" + i + "@example.com";
AuthRecipeUserInfo user = ThirdParty.signInUp(
process.getProcess(), "google", "googleid" + i, email).user;
userIds.add(user.getSupertokensUserId());
userIds2.add(user.getSupertokensUserId());
int finalI = i;
es.execute(() -> {
try {
String email = "user" + finalI + "@example.com";
AuthRecipeUserInfo user = ThirdParty.signInUp(
process.getProcess(), "google", "googleid" + finalI, email).user;
lock.lock();
userIds.add(user.getSupertokensUserId());
userIds2.add(user.getSupertokensUserId());
lock.unlock();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
es.shutdown();
es.awaitTermination(5, TimeUnit.MINUTES);

long end = System.currentTimeMillis();
assert end - start < 100000; // 100 sec
System.out.println("Created users " + numberOfUsers + " in " + (end - start) + "ms");
assert end - start < 25000; // 25 sec
}

Thread.sleep(20000); // wait for indexing to finish
Thread.sleep(10000); // wait for index

{
// Randomly link accounts
long start = System.currentTimeMillis();
ExecutorService es = Executors.newFixedThreadPool(32);
AtomicInteger numberOflinks = new AtomicInteger(0);

while (userIds.size() > 0) {
int numUsersToLink = new Random().nextInt(3) + 1;
if (numUsersToLink > userIds.size()) {
Expand All @@ -107,27 +130,46 @@ public void testUserCreationLinkingAndGetByIdSpeeds() throws Exception {
usersToLink.add(userIds.get(index));
userIds.remove(index);
}
numberOflinks.incrementAndGet();

es.execute(() -> {
try {
AuthRecipe.createPrimaryUser(process.getProcess(), usersToLink.get(0));
for (int i = 1; i < usersToLink.size(); i++) {
AuthRecipe.linkAccounts(process.getProcess(), usersToLink.get(i), usersToLink.get(0));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});

AuthRecipe.createPrimaryUser(process.getProcess(), usersToLink.get(0));
for (int i = 1; i < usersToLink.size(); i++) {
AuthRecipe.linkAccounts(process.getProcess(), usersToLink.get(i), usersToLink.get(0));
}
}
es.shutdown();
es.awaitTermination(5, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println("Accounts linked in " + (end - start) + "ms");
assert end - start < 50000; // 50 sec
}

Thread.sleep(20000); // wait for indexing to finish
Thread.sleep(10000); // wait for index

{
ExecutorService es = Executors.newFixedThreadPool(32);
long start = System.currentTimeMillis();
for (String userId : userIds2) {
AuthRecipe.getUserById(process.getProcess(), userId);
es.execute(() -> {
try {
AuthRecipe.getUserById(process.getProcess(), userId);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
es.shutdown();
es.awaitTermination(5, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println("Time taken for " + numberOfUsers + " users: " + (end - start) + "ms");

assert end - start < 20000;
assert end - start < 20000; // 20 sec
}

process.kill();
Expand Down

0 comments on commit ec8e355

Please sign in to comment.