|
@@ -7,13 +7,11 @@ package org.redkalex.benchmark;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.*;
|
|
|
-import java.util.function.IntFunction;
|
|
|
import java.util.stream.IntStream;
|
|
|
import org.redkale.annotation.*;
|
|
|
import org.redkale.net.http.*;
|
|
|
import org.redkale.service.AbstractService;
|
|
|
import org.redkale.source.DataSource;
|
|
|
-import org.redkale.util.AnyValue;
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -28,37 +26,30 @@ public class BenchmarkService extends AbstractService {
|
|
|
@Resource
|
|
|
private DataSource source;
|
|
|
|
|
|
- private WorldCache cache;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void init(AnyValue conf) {
|
|
|
- this.cache = new WorldCache(source);
|
|
|
- }
|
|
|
-
|
|
|
- @RestMapping(name = "plaintext")
|
|
|
- public byte[] getHelloBytes() {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public byte[] plaintext() {
|
|
|
return helloBytes;
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "json")
|
|
|
- public Message getHelloMessage() {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public Message json() {
|
|
|
return new Message("Hello, World!");
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "db")
|
|
|
- public CompletableFuture<World> findWorldAsync() {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public CompletableFuture<World> db() {
|
|
|
return source.findAsync(World.class, ThreadLocalRandom.current().nextInt(10000) + 1);
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "queries")
|
|
|
- public CompletableFuture<List<World>> queryWorldAsync(int q) {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public CompletableFuture<List<World>> queries(int q) {
|
|
|
int size = Math.min(500, Math.max(1, q));
|
|
|
IntStream ids = ThreadLocalRandom.current().ints(size, 1, 10001);
|
|
|
return source.findsListAsync(World.class, ids.boxed());
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "updates")
|
|
|
- public CompletableFuture<List<World>> updateWorldAsync(int q) {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public CompletableFuture<List<World>> updates(int q) {
|
|
|
int size = Math.min(500, Math.max(1, q));
|
|
|
IntStream ids = ThreadLocalRandom.current().ints(size, 1, 10001);
|
|
|
int[] newNumbers = ThreadLocalRandom.current().ints(size, 1, 10001).toArray();
|
|
@@ -67,8 +58,8 @@ public class BenchmarkService extends AbstractService {
|
|
|
.thenApply(v -> words));
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "fortunes")
|
|
|
- public CompletableFuture<HttpScope> queryFortunes() {
|
|
|
+ @RestMapping(auth = false)
|
|
|
+ public CompletableFuture<HttpScope> fortunes() {
|
|
|
return source.queryListAsync(Fortune.class).thenApply(fortunes -> {
|
|
|
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
Collections.sort(fortunes);
|
|
@@ -76,29 +67,10 @@ public class BenchmarkService extends AbstractService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "cached-worlds")
|
|
|
- public World[] cachedWorlds(int q) {
|
|
|
+ @RestMapping(name = "cached-worlds", auth = false)
|
|
|
+ public CachedWorld[] cachedWorlds(int q) {
|
|
|
int size = Math.min(500, Math.max(1, q));
|
|
|
- return cache.random(size);
|
|
|
- }
|
|
|
-
|
|
|
- static class WorldCache {
|
|
|
-
|
|
|
- private final IntFunction<World[]> arrayFunc = c -> new World[c];
|
|
|
-
|
|
|
- private final World[] array;
|
|
|
-
|
|
|
- private final IntFunction<World> mapFunc;
|
|
|
-
|
|
|
- public WorldCache(DataSource source) {
|
|
|
- List<World> list = source.queryList(World.class);
|
|
|
- this.array = list.toArray(new World[list.size()]);
|
|
|
- this.mapFunc = c -> array[c];
|
|
|
- }
|
|
|
-
|
|
|
- public World[] random(int size) {
|
|
|
- IntStream ids = ThreadLocalRandom.current().ints(size, 0, 10000);
|
|
|
- return ids.mapToObj(mapFunc).toArray(arrayFunc);
|
|
|
- }
|
|
|
+ IntStream ids = ThreadLocalRandom.current().ints(size, 1, 10001);
|
|
|
+ return source.finds(CachedWorld.class, ids.boxed());
|
|
|
}
|
|
|
}
|