2
2
3
3
import java .util .Comparator ;
4
4
import java .util .List ;
5
- import java .util .Map ;
6
- import java .util .Objects ;
7
5
import java .util .concurrent .ThreadLocalRandom ;
8
6
import java .util .stream .IntStream ;
9
7
10
8
import cn .taketoday .benchmark .model .Fortune ;
9
+ import cn .taketoday .benchmark .model .Message ;
11
10
import 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 ;
11
+ import infra .http .MediaType ;
12
+ import infra .http .ResponseEntity ;
13
+ import infra .lang .Nullable ;
14
+ import infra .persistence .EntityManager ;
15
+ import infra .ui .Model ;
16
+ import infra .util .concurrent .Future ;
17
+ import infra .web .annotation .GET ;
18
+ import infra .web .annotation .RestController ;
19
+ import infra .web .view .ViewRef ;
20
20
21
21
/**
22
22
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
26
26
final class BenchmarkHttpHandler {
27
27
28
28
private static final int MIN_WORLD_NUMBER = 1 ;
29
+
29
30
private static final int MAX_WORLD_NUMBER = 10_000 ;
30
31
31
32
private final EntityManager entityManager ;
32
33
34
+ private final WorldCache worldCache ;
35
+
33
36
BenchmarkHttpHandler (EntityManager entityManager ) {
34
37
this .entityManager = entityManager ;
38
+ this .worldCache = new WorldCache (entityManager .find (World .class ));
35
39
}
36
40
37
41
@ GET ("/json" )
38
- public ResponseEntity <Map < String , String > > json () {
42
+ public ResponseEntity <Message > json () {
39
43
return ResponseEntity .ok ()
40
44
.contentType (MediaType .APPLICATION_JSON )
41
- .body (Map . of ( "message" , "Hello, World!" ));
45
+ .body (new Message ( "Hello, World!" ));
42
46
}
43
47
44
48
@ GET ("/plaintext" )
45
49
public String plaintext () {
46
50
return "Hello, World!" ;
47
51
}
48
52
53
+ @ Nullable
49
54
@ GET ("/db" )
50
55
public World db () {
51
56
return entityManager .findById (World .class , nextInt ());
52
57
}
53
58
54
59
@ GET ("/queries" )
55
- public List <World > queries (@ Nullable String queries ) {
56
- return randomNumbers ()
57
- .mapToObj (this ::findWorldById )
58
- .limit (parseQueryCount (queries ))
59
- .toList ();
60
+ public Future <List <World >> queries (@ Nullable String queries ) {
61
+ return Future .combine (randomNumbers ().limit (parseQueryCount (queries )).mapToObj (this ::findWorldByIdFuture ))
62
+ .asList ();
63
+ }
64
+
65
+ @ GET ("/cached-queries" )
66
+ public List <World > cachedQueries (@ Nullable String count ) {
67
+ return worldCache .getCachedWorld (parseQueryCount (count ));
60
68
}
61
69
62
70
@ GET ("/updates" )
63
- public List <World > updates (@ Nullable String queries ) {
64
- return randomNumbers ()
65
- .mapToObj ( this :: findWorldById )
66
- .filter ( Objects :: nonNull )
67
- .peek (world -> {
71
+ public Future < List <World > > updates (@ Nullable String queries ) {
72
+ return Future . combine ( randomNumbers ()
73
+ .limit ( parseQueryCount ( queries ) )
74
+ .mapToObj ( this :: findWorldByIdFuture )
75
+ .map ( worldFuture -> worldFuture . map (world -> {
68
76
world .setRandomNumber (nextInt ());
69
77
entityManager .updateById (world );
70
- })
71
- .limit (parseQueryCount (queries ))
72
- .toList ();
78
+ return world ;
79
+ }))).asList ();
73
80
}
74
81
75
82
@ GET ("/fortunes" )
@@ -82,9 +89,13 @@ public ViewRef fortunes(Model model) {
82
89
return ViewRef .forViewName ("fortunes" );
83
90
}
84
91
92
+ private Future <World > findWorldByIdFuture (int id ) {
93
+ return Future .run (() -> findWorldById (id ));
94
+ }
95
+
85
96
@ Nullable
86
97
private World findWorldById (int id ) {
87
- return entityManager .findById (World .class , boxed [ id ] );
98
+ return entityManager .findById (World .class , id );
88
99
}
89
100
90
101
//
@@ -95,12 +106,8 @@ private static IntStream randomNumbers() {
95
106
.distinct ();
96
107
}
97
108
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 )];
109
+ private static int nextInt () {
110
+ return ThreadLocalRandom .current ().nextInt (MIN_WORLD_NUMBER , MAX_WORLD_NUMBER );
104
111
}
105
112
106
113
private static int parseQueryCount (@ Nullable String textValue ) {
0 commit comments