|
@@ -1,45 +1,37 @@
|
|
|
-package benchmark;
|
|
|
+package benchmark.controller;
|
|
|
|
|
|
import benchmark.model.Fortune;
|
|
|
import benchmark.model.World;
|
|
|
-import org.springframework.data.mongodb.core.FindAndModifyOptions;
|
|
|
-import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
|
|
-import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
-import org.springframework.data.mongodb.core.query.Query;
|
|
|
-import org.springframework.data.mongodb.core.query.Update;
|
|
|
+import benchmark.repository.DbRepository;
|
|
|
+import org.springframework.context.annotation.Profile;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.reactive.result.view.Rendering;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
-import reactor.core.scheduler.Scheduler;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
import static java.util.Comparator.comparing;
|
|
|
-import static org.springframework.data.mongodb.core.FindAndModifyOptions.*;
|
|
|
-import static org.springframework.data.mongodb.core.query.Criteria.*;
|
|
|
-import static org.springframework.data.mongodb.core.query.Query.*;
|
|
|
-import static org.springframework.data.mongodb.core.query.Update.*;
|
|
|
|
|
|
@Controller()
|
|
|
-@RequestMapping("/mongo")
|
|
|
-public final class NoSQLController extends BaseDBController {
|
|
|
+@Profile(value = {"jdbc", "pgclient", "mongo"})
|
|
|
+public final class ReactiveController {
|
|
|
|
|
|
- private final ReactiveMongoTemplate mongoTemplate;
|
|
|
+ private final DbRepository dbRepository;
|
|
|
|
|
|
- public NoSQLController(ReactiveMongoTemplate mongoTemplate) {
|
|
|
- this.mongoTemplate = mongoTemplate;
|
|
|
+ public ReactiveController(DbRepository dbRepository) {
|
|
|
+ this.dbRepository = dbRepository;
|
|
|
}
|
|
|
|
|
|
@GetMapping(value = "/db", produces = "application/json")
|
|
|
@ResponseBody
|
|
|
public Mono<World> db() {
|
|
|
- return mongoTemplate.findById(randomWorldNumber(), World.class);
|
|
|
+ return dbRepository.getWorld(randomWorldNumber());
|
|
|
}
|
|
|
|
|
|
@GetMapping(value = "/queries", produces = "application/json")
|
|
@@ -56,18 +48,14 @@ public final class NoSQLController extends BaseDBController {
|
|
|
public Mono<List<World>> updates(@RequestParam String queries) {
|
|
|
Mono<World>[] worlds = new Mono[parseQueryCount(queries)];
|
|
|
|
|
|
- Arrays.setAll(worlds, i -> mongoTemplate.findAndModify(
|
|
|
- query(where("id").is(randomWorldNumber())),
|
|
|
- update("randomNumber", randomWorldNumber()),
|
|
|
- options().returnNew(true),
|
|
|
- World.class));
|
|
|
+ Arrays.setAll(worlds, i -> dbRepository.findAndUpdateWorld(randomWorldNumber(), randomWorldNumber()));
|
|
|
|
|
|
return Flux.merge(worlds).collectList();
|
|
|
}
|
|
|
|
|
|
@GetMapping(value = "/fortunes")
|
|
|
public Rendering fortunes() {
|
|
|
- Mono<List<Fortune>> result = mongoTemplate.findAll(Fortune.class).collectList().flatMap(fortunes -> {
|
|
|
+ Mono<List<Fortune>> result = dbRepository.fortunes().collectList().flatMap(fortunes -> {
|
|
|
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
fortunes.sort(comparing(fortune -> fortune.message));
|
|
|
return Mono.just(fortunes);
|
|
@@ -75,4 +63,21 @@ public final class NoSQLController extends BaseDBController {
|
|
|
|
|
|
return Rendering.view("fortunes").modelAttribute("fortunes", result).build();
|
|
|
}
|
|
|
+
|
|
|
+ protected int randomWorldNumber() {
|
|
|
+ return 1 + ThreadLocalRandom.current().nextInt(10000);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected int parseQueryCount(String textValue) {
|
|
|
+ if (textValue == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ int parsedValue;
|
|
|
+ try {
|
|
|
+ parsedValue = Integer.parseInt(textValue);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return Math.min(500, Math.max(1, parsedValue));
|
|
|
+ }
|
|
|
}
|