BenchmarkService.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import org.redkale.util.AnyValue;
  15. /**
  16. * 测试redkale-jdbc, 需要覆盖到原BenchmarkService
  17. *
  18. * @author zhangjx
  19. */
  20. @RestService(name = " ", repair = false)
  21. public class BenchmarkService extends AbstractService {
  22. private static final byte[] helloBytes = "Hello, world!".getBytes();
  23. @Resource
  24. private DataSource source;
  25. public void init(AnyValue conf) {
  26. source.finds(CachedWorld.class, 1);
  27. }
  28. @NonBlocking
  29. @RestMapping(auth = false)
  30. public byte[] plaintext() {
  31. return helloBytes;
  32. }
  33. @NonBlocking
  34. @RestMapping(auth = false)
  35. public Message json() {
  36. return new Message("Hello, World!");
  37. }
  38. @RestMapping(auth = false)
  39. public World db() {
  40. return source.find(World.class, ThreadLocalRandom.current().nextInt(10000) + 1);
  41. }
  42. @RestMapping(auth = false)
  43. public List<World> queries(int q) {
  44. return source.findsList(World.class, random(q));
  45. }
  46. @RestMapping(auth = false)
  47. public List<World> updates(int q) {
  48. int size = Math.min(500, Math.max(1, q));
  49. int[] newNumbers = ThreadLocalRandom.current().ints(size, 1, 10001).toArray();
  50. List<World> words = source.findsList(World.class, random(q));
  51. source.update(World.updateNewNumbers(words, newNumbers));
  52. return words;
  53. }
  54. @RestMapping(auth = false)
  55. public HttpScope fortunes() {
  56. List<Fortune> fortunes = source.queryList(Fortune.class);
  57. fortunes.add(new Fortune(0, "Additional fortune added at request time."));
  58. Collections.sort(fortunes);
  59. return HttpScope.refer("").referObj(fortunes);
  60. }
  61. @NonBlocking
  62. @RestMapping(name = "cached-worlds", auth = false)
  63. public CachedWorld[] cachedWorlds(int q) {
  64. return source.finds(CachedWorld.class, random(q));
  65. }
  66. private Stream<Integer> random(int q) {
  67. int size = Math.min(500, Math.max(1, q));
  68. return ThreadLocalRandom.current().ints(size, 1, 10001).boxed();
  69. }
  70. }