Add k6 tests
This commit is contained in:
@@ -8,7 +8,7 @@ import com.zeenea.experiments.virtualthreads.virtualthreadsapp.domain.marketplac
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Service
|
||||
public class MarketplaceService {
|
||||
@@ -21,9 +21,12 @@ public class MarketplaceService {
|
||||
}
|
||||
|
||||
public Marketplace getMarketplace() {
|
||||
return getMarketplaceInParallel();
|
||||
return getMarketplaceInParallel2();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method uses classic threads while it is using `CompletableFuture`s. This way should be avoided.
|
||||
*/
|
||||
private Marketplace getMarketplaceInParallel() {
|
||||
CompletableFuture<List<Catalog>> getCatalogsFuture = CompletableFuture.supplyAsync(catalogService::getAll);
|
||||
CompletableFuture<List<Item>> getAllItemsFuture = CompletableFuture.supplyAsync(itemService::getAllItems);
|
||||
@@ -38,6 +41,23 @@ public class MarketplaceService {
|
||||
return new Marketplace(catalogs, sharedItems);
|
||||
}
|
||||
|
||||
private Marketplace getMarketplaceInParallel2() {
|
||||
try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
Future<List<Catalog>> getCatalogsFuture = executorService.submit(catalogService::getAll);
|
||||
Future<List<Item>> getItemsFuture = executorService.submit(itemService::getAllItems);
|
||||
|
||||
List<Catalog> catalogs = getCatalogsFuture.get();
|
||||
List<Item> items = getItemsFuture.get();
|
||||
|
||||
var sharedItems = items.stream()
|
||||
.filter(Item::isShared)
|
||||
.toList();
|
||||
return new Marketplace(catalogs, sharedItems);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Marketplace getMarketplaceSequentially() {
|
||||
return new Marketplace(
|
||||
catalogService.getAll(),
|
||||
|
||||
Reference in New Issue
Block a user