7
7
8
8
import java .util .*;
9
9
import java .util .concurrent .*;
10
- import java .util .function .IntFunction ;
11
10
import java .util .stream .IntStream ;
12
11
import org .redkale .annotation .*;
13
12
import org .redkale .net .http .*;
14
13
import org .redkale .service .AbstractService ;
15
14
import org .redkale .source .DataSource ;
16
- import org .redkale .util .AnyValue ;
17
15
18
16
/**
19
17
*
@@ -28,37 +26,30 @@ public class BenchmarkService extends AbstractService {
28
26
@ Resource
29
27
private DataSource source ;
30
28
31
- private WorldCache cache ;
32
-
33
- @ Override
34
- public void init (AnyValue conf ) {
35
- this .cache = new WorldCache (source );
36
- }
37
-
38
- @ RestMapping (name = "plaintext" )
39
- public byte [] getHelloBytes () {
29
+ @ RestMapping (auth = false )
30
+ public byte [] plaintext () {
40
31
return helloBytes ;
41
32
}
42
33
43
- @ RestMapping (name = "json" )
44
- public Message getHelloMessage () {
34
+ @ RestMapping (auth = false )
35
+ public Message json () {
45
36
return new Message ("Hello, World!" );
46
37
}
47
38
48
- @ RestMapping (name = "db" )
49
- public CompletableFuture <World > findWorldAsync () {
39
+ @ RestMapping (auth = false )
40
+ public CompletableFuture <World > db () {
50
41
return source .findAsync (World .class , ThreadLocalRandom .current ().nextInt (10000 ) + 1 );
51
42
}
52
43
53
- @ RestMapping (name = "queries" )
54
- public CompletableFuture <List <World >> queryWorldAsync (int q ) {
44
+ @ RestMapping (auth = false )
45
+ public CompletableFuture <List <World >> queries (int q ) {
55
46
int size = Math .min (500 , Math .max (1 , q ));
56
47
IntStream ids = ThreadLocalRandom .current ().ints (size , 1 , 10001 );
57
48
return source .findsListAsync (World .class , ids .boxed ());
58
49
}
59
50
60
- @ RestMapping (name = "updates" )
61
- public CompletableFuture <List <World >> updateWorldAsync (int q ) {
51
+ @ RestMapping (auth = false )
52
+ public CompletableFuture <List <World >> updates (int q ) {
62
53
int size = Math .min (500 , Math .max (1 , q ));
63
54
IntStream ids = ThreadLocalRandom .current ().ints (size , 1 , 10001 );
64
55
int [] newNumbers = ThreadLocalRandom .current ().ints (size , 1 , 10001 ).toArray ();
@@ -67,38 +58,19 @@ public CompletableFuture<List<World>> updateWorldAsync(int q) {
67
58
.thenApply (v -> words ));
68
59
}
69
60
70
- @ RestMapping (name = "fortunes" )
71
- public CompletableFuture <HttpScope > queryFortunes () {
61
+ @ RestMapping (auth = false )
62
+ public CompletableFuture <HttpScope > fortunes () {
72
63
return source .queryListAsync (Fortune .class ).thenApply (fortunes -> {
73
64
fortunes .add (new Fortune (0 , "Additional fortune added at request time." ));
74
65
Collections .sort (fortunes );
75
66
return HttpScope .refer ("" ).referObj (fortunes );
76
67
});
77
68
}
78
69
79
- @ RestMapping (name = "cached-worlds" )
80
- public World [] cachedWorlds (int q ) {
70
+ @ RestMapping (name = "cached-worlds" , auth = false )
71
+ public CachedWorld [] cachedWorlds (int q ) {
81
72
int size = Math .min (500 , Math .max (1 , q ));
82
- return cache .random (size );
83
- }
84
-
85
- static class WorldCache {
86
-
87
- private final IntFunction <World []> arrayFunc = c -> new World [c ];
88
-
89
- private final World [] array ;
90
-
91
- private final IntFunction <World > mapFunc ;
92
-
93
- public WorldCache (DataSource source ) {
94
- List <World > list = source .queryList (World .class );
95
- this .array = list .toArray (new World [list .size ()]);
96
- this .mapFunc = c -> array [c ];
97
- }
98
-
99
- public World [] random (int size ) {
100
- IntStream ids = ThreadLocalRandom .current ().ints (size , 0 , 10000 );
101
- return ids .mapToObj (mapFunc ).toArray (arrayFunc );
102
- }
73
+ IntStream ids = ThreadLocalRandom .current ().ints (size , 1 , 10001 );
74
+ return source .finds (CachedWorld .class , ids .boxed ());
103
75
}
104
76
}
0 commit comments