BenchmarkService.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package org.redkalex.benchmark;
  7. import java.util.*;
  8. import java.util.concurrent.*;
  9. import java.util.stream.Stream;
  10. import org.redkale.annotation.*;
  11. import org.redkale.net.http.*;
  12. import org.redkale.service.AbstractService;
  13. import org.redkale.source.DataSource;
  14. /**
  15. * 测试redkale-jdbc, 需要覆盖到原BenchmarkService
  16. *
  17. * @author zhangjx
  18. */
  19. @RestService(name = " ", repair = false)
  20. public class BenchmarkService extends AbstractService {
  21. private static final byte[] helloBytes = "Hello, world!".getBytes();
  22. @Resource
  23. private DataSource source;
  24. @NonBlocking
  25. @RestMapping(auth = false)
  26. public byte[] plaintext() {
  27. return helloBytes;
  28. }
  29. @NonBlocking
  30. @RestMapping(auth = false)
  31. public Message json() {
  32. return new Message("Hello, World!");
  33. }
  34. @RestMapping(auth = false)
  35. public World db() {
  36. return source.find(World.class, ThreadLocalRandom.current().nextInt(10000) + 1);
  37. }
  38. @RestMapping(auth = false)
  39. public List<World> queries(int q) {
  40. return source.findsList(World.class, random(q));
  41. }
  42. @RestMapping(auth = false)
  43. public List<World> updates(int q) {
  44. int size = Math.min(500, Math.max(1, q));
  45. int[] newNumbers = ThreadLocalRandom.current().ints(size, 1, 10001).toArray();
  46. List<World> words = source.findsList(World.class, random(q));
  47. source.update(World.updateNewNumbers(words, newNumbers));
  48. return words;
  49. }
  50. @RestMapping(auth = false)
  51. public HttpScope fortunes() {
  52. List<Fortune> fortunes = source.queryList(Fortune.class);
  53. fortunes.add(new Fortune(0, "Additional fortune added at request time."));
  54. Collections.sort(fortunes);
  55. return HttpScope.refer("").referObj(fortunes);
  56. }
  57. @NonBlocking
  58. @RestMapping(name = "cached-worlds", auth = false)
  59. public CachedWorld[] cachedWorlds(int q) {
  60. return source.finds(CachedWorld.class, random(q));
  61. }
  62. private Stream<Integer> random(int q) {
  63. int size = Math.min(500, Math.max(1, q));
  64. return ThreadLocalRandom.current().ints(size, 1, 10001).boxed();
  65. }
  66. }