PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/util/profiling/UtilTimerStack.java

https://bitbucket.org/atlassian/atlassian-profiling
Java | 99 lines | 61 code | 10 blank | 28 comment | 9 complexity | 3c19aea44ff1e7b4d9b2eaed8e1c70f2 MD5 | raw file
  1. /**
  2. * Atlassian Source Code Template.
  3. * User: Scott Farquhar
  4. * Date: Feb 19, 2003
  5. * Time: 6:56:26 PM
  6. * CVS Revision: $Revision$
  7. * Last CVS Commit: $Date$
  8. * Author of last CVS Commit: $Author$
  9. *
  10. * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
  11. */
  12. package com.atlassian.util.profiling;
  13. import com.atlassian.util.profiling.strategy.ProfilingStrategy;
  14. import com.atlassian.util.profiling.strategy.impl.StackProfilingStrategy;
  15. import java.util.concurrent.ConcurrentLinkedQueue;
  16. /**
  17. * <p>A timer stack.</p>
  18. * <p></p>
  19. * Usage:
  20. * <pre>
  21. * String logMessage = "Log message";
  22. * UtilTimerStack.push(logMessage);
  23. * try
  24. * {
  25. * //do some code
  26. * }
  27. * finally
  28. * {
  29. * UtilTimerStack.pop(logMessage); //this needs to be the same text as above
  30. * }
  31. * </pre>
  32. */
  33. public class UtilTimerStack
  34. {
  35. private static StackProfilingStrategy defaultProfilingStrategy = new StackProfilingStrategy();
  36. private static final ConcurrentLinkedQueue<ProfilingStrategy> strategies = new ConcurrentLinkedQueue<ProfilingStrategy>();
  37. public static void push(String name)
  38. {
  39. if (defaultProfilingStrategy.isEnabled())
  40. {
  41. defaultProfilingStrategy.start(name);
  42. }
  43. for (ProfilingStrategy strategy : strategies)
  44. {
  45. if (strategy.isEnabled())
  46. {
  47. strategy.start(name);
  48. }
  49. }
  50. }
  51. public static void pop(String name)
  52. {
  53. defaultProfilingStrategy.stop(name);
  54. for (ProfilingStrategy strategy : strategies)
  55. {
  56. strategy.stop(name);
  57. }
  58. }
  59. public static void add(ProfilingStrategy strategy)
  60. {
  61. if (!defaultProfilingStrategy.equals(strategy) && !strategies.contains(strategy))
  62. {
  63. strategies.add(strategy);
  64. }
  65. }
  66. public static void remove(ProfilingStrategy strategy)
  67. {
  68. strategies.remove(strategy);
  69. }
  70. public static StackProfilingStrategy getDefaultStrategy()
  71. {
  72. return defaultProfilingStrategy;
  73. }
  74. public static boolean isActive()
  75. {
  76. if (defaultProfilingStrategy.isEnabled())
  77. {
  78. return true;
  79. }
  80. for (ProfilingStrategy strategy : strategies)
  81. {
  82. if (strategy.isEnabled())
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. }