|
@@ -7,8 +7,9 @@ package org.redkalex.benchmark;
|
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
import java.util.concurrent.*;
|
|
|
|
+import java.util.function.IntFunction;
|
|
import java.util.stream.IntStream;
|
|
import java.util.stream.IntStream;
|
|
-import org.redkale.annotation.Resource;
|
|
|
|
|
|
+import org.redkale.annotation.*;
|
|
import org.redkale.net.http.*;
|
|
import org.redkale.net.http.*;
|
|
import org.redkale.service.AbstractService;
|
|
import org.redkale.service.AbstractService;
|
|
import org.redkale.source.DataSource;
|
|
import org.redkale.source.DataSource;
|
|
@@ -18,6 +19,7 @@ import org.redkale.util.AnyValue;
|
|
*
|
|
*
|
|
* @author zhangjx
|
|
* @author zhangjx
|
|
*/
|
|
*/
|
|
|
|
+@NonBlocking
|
|
@RestService(name = " ", repair = false)
|
|
@RestService(name = " ", repair = false)
|
|
public class BenchmarkService extends AbstractService {
|
|
public class BenchmarkService extends AbstractService {
|
|
|
|
|
|
@@ -26,9 +28,11 @@ public class BenchmarkService extends AbstractService {
|
|
@Resource
|
|
@Resource
|
|
private DataSource source;
|
|
private DataSource source;
|
|
|
|
|
|
|
|
+ private WorldCache cache;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void init(AnyValue conf) {
|
|
public void init(AnyValue conf) {
|
|
- CachedWorld.Cache.getInstance(source);
|
|
|
|
|
|
+ this.cache = new WorldCache(source);
|
|
}
|
|
}
|
|
|
|
|
|
@RestMapping(name = "plaintext")
|
|
@RestMapping(name = "plaintext")
|
|
@@ -59,7 +63,7 @@ public class BenchmarkService extends AbstractService {
|
|
IntStream ids = ThreadLocalRandom.current().ints(size, 1, 10001);
|
|
IntStream ids = ThreadLocalRandom.current().ints(size, 1, 10001);
|
|
int[] newNumbers = ThreadLocalRandom.current().ints(size, 1, 10001).toArray();
|
|
int[] newNumbers = ThreadLocalRandom.current().ints(size, 1, 10001).toArray();
|
|
return source.findsListAsync(World.class, ids.boxed())
|
|
return source.findsListAsync(World.class, ids.boxed())
|
|
- .thenCompose(words -> source.updateAsync(World.setNewNumbers(words.toArray(new World[words.size()]), newNumbers))
|
|
|
|
|
|
+ .thenCompose(words -> source.updateAsync(World.updateNewNumbers(words, newNumbers))
|
|
.thenApply(v -> words));
|
|
.thenApply(v -> words));
|
|
}
|
|
}
|
|
|
|
|
|
@@ -73,8 +77,28 @@ public class BenchmarkService extends AbstractService {
|
|
}
|
|
}
|
|
|
|
|
|
@RestMapping(name = "cached-worlds")
|
|
@RestMapping(name = "cached-worlds")
|
|
- public CachedWorld[] cachedWorlds(int q) {
|
|
|
|
|
|
+ public World[] cachedWorlds(int q) {
|
|
int size = Math.min(500, Math.max(1, q));
|
|
int size = Math.min(500, Math.max(1, q));
|
|
- return CachedWorld.Cache.getInstance(source).random(ThreadLocalRandom.current(), size);
|
|
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|