You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PurgeProfilerTest.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.purge;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.log.Logger;
  24. import static org.mockito.ArgumentMatchers.contains;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.verify;
  27. public class PurgeProfilerTest {
  28. private MockedClock clock;
  29. private PurgeProfiler profiler;
  30. private Logger logger;
  31. @Before
  32. public void prepare() {
  33. clock = new MockedClock();
  34. profiler = new PurgeProfiler(clock);
  35. logger = mock(Logger.class);
  36. }
  37. @Test
  38. public void shouldProfilePurge() {
  39. profiler.start("foo");
  40. clock.sleep(10);
  41. profiler.stop();
  42. profiler.start("bar");
  43. clock.sleep(5);
  44. profiler.stop();
  45. profiler.start("foo");
  46. clock.sleep(8);
  47. profiler.stop();
  48. profiler.dump(50, logger);
  49. verify(logger).info(contains("foo: 18ms"));
  50. verify(logger).info(contains("bar: 5ms"));
  51. }
  52. @Test
  53. public void shouldResetPurgeProfiling() {
  54. profiler.start("foo");
  55. clock.sleep(10);
  56. profiler.stop();
  57. profiler.reset();
  58. profiler.start("bar");
  59. clock.sleep(5);
  60. profiler.stop();
  61. profiler.start("foo");
  62. clock.sleep(8);
  63. profiler.stop();
  64. profiler.dump(50, logger);
  65. verify(logger).info(contains("foo: 8ms"));
  66. verify(logger).info(contains("bar: 5ms"));
  67. }
  68. private static class MockedClock extends PurgeProfiler.Clock {
  69. private long now = 0;
  70. @Override
  71. public long now() {
  72. return now;
  73. }
  74. public void sleep(long duration) {
  75. now += duration;
  76. }
  77. }
  78. }