22
33import java .util .Comparator ;
44import java .util .List ;
5- import java .util .Map ;
65import java .util .Objects ;
76import java .util .concurrent .ThreadLocalRandom ;
87import java .util .stream .IntStream ;
98
109import cn .taketoday .benchmark .model .Fortune ;
10+ import cn .taketoday .benchmark .model .Message ;
1111import cn .taketoday .benchmark .model .World ;
12- import cn .taketoday .http .MediaType ;
13- import cn .taketoday .http .ResponseEntity ;
14- import cn .taketoday .jdbc .persistence .EntityManager ;
15- import cn .taketoday .lang .Nullable ;
16- import cn .taketoday .ui .Model ;
17- import cn .taketoday .web .annotation .GET ;
18- import cn .taketoday .web .annotation .RestController ;
19- import cn .taketoday .web .view .ViewRef ;
12+ import infra .http .MediaType ;
13+ import infra .http .ResponseEntity ;
14+ import infra .lang .Nullable ;
15+ import infra .persistence .EntityManager ;
16+ import infra .ui .Model ;
17+ import infra .util .concurrent .Future ;
18+ import infra .web .annotation .GET ;
19+ import infra .web .annotation .RestController ;
20+ import infra .web .view .ViewRef ;
2021
2122/**
2223 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
@@ -30,15 +31,18 @@ final class BenchmarkHttpHandler {
3031
3132 private final EntityManager entityManager ;
3233
34+ private final WorldCache worldCache ;
35+
3336 BenchmarkHttpHandler (EntityManager entityManager ) {
3437 this .entityManager = entityManager ;
38+ this .worldCache = new WorldCache (entityManager .find (World .class ));
3539 }
3640
3741 @ GET ("/json" )
38- public ResponseEntity <Map < String , String > > json () {
42+ public ResponseEntity <Message > json () {
3943 return ResponseEntity .ok ()
4044 .contentType (MediaType .APPLICATION_JSON )
41- .body (Map . of ( "message" , "Hello, World!" ));
45+ .body (new Message ( "Hello, World!" ));
4246 }
4347
4448 @ GET ("/plaintext" )
@@ -59,17 +63,22 @@ public List<World> queries(@Nullable String queries) {
5963 .toList ();
6064 }
6165
66+ @ GET ("/cached" )
67+ public List <World > cachedQueries (@ Nullable String count ) {
68+ return worldCache .getCachedWorld (parseQueryCount (count ));
69+ }
70+
6271 @ GET ("/updates" )
63- public List <World > updates (@ Nullable String queries ) {
64- return randomNumbers ()
72+ public Future < List <World > > updates (@ Nullable String queries ) {
73+ return Future . run (() -> randomNumbers ()
6574 .mapToObj (this ::findWorldById )
6675 .filter (Objects ::nonNull )
6776 .peek (world -> {
6877 world .setRandomNumber (nextInt ());
6978 entityManager .updateById (world );
7079 })
7180 .limit (parseQueryCount (queries ))
72- .toList ();
81+ .toList ()) ;
7382 }
7483
7584 @ GET ("/fortunes" )
@@ -84,7 +93,7 @@ public ViewRef fortunes(Model model) {
8493
8594 @ Nullable
8695 private World findWorldById (int id ) {
87- return entityManager .findById (World .class , boxed [ id ] );
96+ return entityManager .findById (World .class , id );
8897 }
8998
9099 //
@@ -95,12 +104,8 @@ private static IntStream randomNumbers() {
95104 .distinct ();
96105 }
97106
98- private static final Integer [] boxed = IntStream .range (MIN_WORLD_NUMBER , MAX_WORLD_NUMBER + 1 )
99- .boxed ()
100- .toArray (Integer []::new );
101-
102- private static Integer nextInt () {
103- return boxed [ThreadLocalRandom .current ().nextInt (MIN_WORLD_NUMBER , MAX_WORLD_NUMBER )];
107+ private static int nextInt () {
108+ return ThreadLocalRandom .current ().nextInt (MIN_WORLD_NUMBER , MAX_WORLD_NUMBER );
104109 }
105110
106111 private static int parseQueryCount (@ Nullable String textValue ) {
0 commit comments