Skip to content

Commit 6ecb701

Browse files
committed
🎨 优化使用方式
1 parent b8748fd commit 6ecb701

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

frameworks/Java/today/src/main/java/cn/taketoday/benchmark/http/BenchmarkHttpHandler.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5-
import java.util.Objects;
65
import java.util.concurrent.ThreadLocalRandom;
76
import java.util.stream.IntStream;
87

@@ -27,6 +26,7 @@
2726
final class BenchmarkHttpHandler {
2827

2928
private static final int MIN_WORLD_NUMBER = 1;
29+
3030
private static final int MAX_WORLD_NUMBER = 10_000;
3131

3232
private final EntityManager entityManager;
@@ -56,11 +56,9 @@ public World db() {
5656
}
5757

5858
@GET("/queries")
59-
public List<World> queries(@Nullable String queries) {
60-
return randomNumbers()
61-
.mapToObj(this::findWorldById)
62-
.limit(parseQueryCount(queries))
63-
.toList();
59+
public Future<List<World>> queries(@Nullable String queries) {
60+
return Future.combine(randomNumbers().limit(parseQueryCount(queries)).mapToObj(this::findWorldByIdFuture))
61+
.asList();
6462
}
6563

6664
@GET("/cached")
@@ -70,15 +68,14 @@ public List<World> cachedQueries(@Nullable String count) {
7068

7169
@GET("/updates")
7270
public Future<List<World>> updates(@Nullable String queries) {
73-
return Future.run(() -> randomNumbers()
74-
.mapToObj(this::findWorldById)
75-
.filter(Objects::nonNull)
76-
.peek(world -> {
71+
return Future.combine(randomNumbers()
72+
.limit(parseQueryCount(queries))
73+
.mapToObj(this::findWorldByIdFuture)
74+
.map(worldFuture -> worldFuture.map(world -> {
7775
world.setRandomNumber(nextInt());
7876
entityManager.updateById(world);
79-
})
80-
.limit(parseQueryCount(queries))
81-
.toList());
77+
return world;
78+
}))).asList();
8279
}
8380

8481
@GET("/fortunes")
@@ -91,6 +88,10 @@ public ViewRef fortunes(Model model) {
9188
return ViewRef.forViewName("fortunes");
9289
}
9390

91+
private Future<World> findWorldByIdFuture(int id) {
92+
return Future.run(() -> findWorldById(id));
93+
}
94+
9495
@Nullable
9596
private World findWorldById(int id) {
9697
return entityManager.findById(World.class, id);

0 commit comments

Comments
 (0)