|
@@ -11,7 +11,7 @@ import javax.annotation.Resource;
|
|
|
import org.redkale.net.http.*;
|
|
|
import org.redkale.service.AbstractService;
|
|
|
import org.redkale.source.*;
|
|
|
-import org.redkale.util.AnyValue;
|
|
|
+import org.redkalex.benchmark.CachedWorld.WorldEntityCache;
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -20,28 +20,21 @@ import org.redkale.util.AnyValue;
|
|
|
@RestService(name = " ", repair = false)
|
|
|
public class Service extends AbstractService {
|
|
|
|
|
|
- private static final byte[] helloBytes = "Hello, world!".intern().getBytes();
|
|
|
+ private static final byte[] helloBytes = "Hello, world!".getBytes();
|
|
|
|
|
|
private final Random random = new Random();
|
|
|
|
|
|
@Resource
|
|
|
private DataSource source;
|
|
|
|
|
|
- private EntityCache<CachedWorld> cache;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void init(AnyValue conf) {
|
|
|
- if (Boolean.getBoolean("benchmarks.cache")) {
|
|
|
- this.cache = ((DataSqlSource) source).loadCache(CachedWorld.class).array();
|
|
|
- }
|
|
|
- }
|
|
|
+ private WorldEntityCache cache;
|
|
|
|
|
|
@RestMapping(name = "plaintext")
|
|
|
public byte[] getHelloBytes() {
|
|
|
return helloBytes;
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "json", length = 27)
|
|
|
+ @RestMapping(name = "json")
|
|
|
public Message getHelloMessage() {
|
|
|
return Message.create("Hello, World!");
|
|
|
}
|
|
@@ -72,28 +65,29 @@ public class Service extends AbstractService {
|
|
|
final int index = i;
|
|
|
futures[index] = source.findAsync(World.class, randomId()).thenAccept(v -> worlds[index] = v.randomNumber(randomId()));
|
|
|
}
|
|
|
- return CompletableFuture.allOf(futures).thenCompose(v -> {
|
|
|
- Arrays.sort(worlds);
|
|
|
- return source.updateAsync(worlds);
|
|
|
- }).thenApply(v -> worlds);
|
|
|
+ return CompletableFuture.allOf(futures).thenCompose(v -> source.updateAsync(World.sort(worlds))).thenApply(v -> worlds);
|
|
|
}
|
|
|
|
|
|
@RestMapping(name = "fortunes")
|
|
|
public CompletableFuture<HttpResult<String>> queryFortunes() {
|
|
|
return source.queryListAsync(Fortune.class).thenApply((fortunes) -> {
|
|
|
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
- Collections.sort(fortunes);
|
|
|
- String html = FortunesTemplate.template(fortunes).render().toString();
|
|
|
+ String html = FortunesTemplate.template(Fortune.sort(fortunes)).render().toString();
|
|
|
return new HttpResult("text/html; charset=utf-8", html);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@RestMapping(name = "cached-worlds")
|
|
|
public CachedWorld[] cachedWorlds(int q) {
|
|
|
+ if (cache == null) {
|
|
|
+ synchronized (this) {
|
|
|
+ if (cache == null) cache = new WorldEntityCache(source);
|
|
|
+ }
|
|
|
+ }
|
|
|
final int size = Math.min(500, Math.max(1, q));
|
|
|
final CachedWorld[] worlds = new CachedWorld[size];
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
- worlds[i] = cache.findAt(randomId());
|
|
|
+ worlds[i] = cache.findAt(random.nextInt(10000));
|
|
|
}
|
|
|
return worlds;
|
|
|
}
|