/** * Starts a task timing with the specified task title. * * @param taskTitle the specified task title */ public static void start(final String taskTitle) { Stopwatch root = STOPWATCH.get(); if (null == root) { root = new Stopwatch(taskTitle); // Creates the root stopwatch STOPWATCH.set(root); return; } final Stopwatch recent = getRecentRunning(STOPWATCH.get()); if (null == recent) { return; } recent.addLeaf(new Stopwatch(taskTitle)); // Adds sub-stopwatch }
/** * Appends the timing statistics with the specified string builder. * * @param level the current level of the task tree * @param stringBuilder the specified string builder */ private void appendTimingStat(final int level, final StringBuilder stringBuilder) { stringBuilder.append(toString()); for (int i = 0; i < leaves.size(); i++) { final Stopwatch leaf = leaves.get(i); stringBuilder.append(getIndentBlanks(level * 2)); leaf.appendTimingStat(level + 1, stringBuilder); } }
if (stopwatch.isEnded()) { return stopwatch.getElapsedTime(); return currentTimeMillis - stopwatch.getStartTime();
if (stopwatch.isEnded()) { return stopwatch.getElapsedTime(); return currentTimeMillis - stopwatch.getStartTime();
/** * Gets the recent running stopwatch with the specified parent stopwatch. * * @param parent the specified parent stopwatch * @return the recent stopwatch, returns {@code null} if not found */ private static Stopwatch getRecentRunning(final Stopwatch parent) { if (null == parent) { return null; } final List<Stopwatch> leaves = parent.getLeaves(); if (leaves.isEmpty()) { if (parent.isRunning()) { return parent; } else { return null; } } for (int i = leaves.size() - 1; i > -1; i--) { final Stopwatch leaf = leaves.get(i); if (leaf.isRunning()) { return getRecentRunning(leaf); } else { continue; } } return parent; }
/** * Gets stopwatch from the specified parent stopwatch with the specified task title. * * @param parent the specified parent * @param taskTitle the specified task title * @return stopwatch, returns {@code null} if not found */ private static Stopwatch get(final Stopwatch parent, final String taskTitle) { if (taskTitle.equals(parent.getTaskTitle())) { return parent; } for (final Stopwatch leaf : parent.getLeaves()) { final Stopwatch ret = get(leaf, taskTitle); if (null != ret) { return ret; } } return null; }
/** * Starts a task timing with the specified task title. * * @param taskTitle the specified task title */ public static void start(final String taskTitle) { Stopwatch root = STOPWATCH.get(); if (null == root) { root = new Stopwatch(taskTitle); // Creates the root stopwatch STOPWATCH.set(root); return; } final Stopwatch recent = getRecentRunning(STOPWATCH.get()); if (null == recent) { return; } recent.addLeaf(new Stopwatch(taskTitle)); // Adds sub-stopwatch }
/** * Gets the elapsed time percent of root. * * @return percent of root */ public float getPercentOfRoot() { final Stopwatch root = STOPWATCH.get(); if (null == root) { return 0; } final float rootElapsedTime = (float) root.getElapsedTime(); if (0 == rootElapsedTime) { // Denominator is equals to zero return 0; } return getElapsedTime() / rootElapsedTime * HUNDRED; }
/** * Appends the timing statistics with the specified string builder. * * @param level the current level of the task tree * @param stringBuilder the specified string builder */ private void appendTimingStat(final int level, final StringBuilder stringBuilder) { stringBuilder.append(toString()); for (int i = 0; i < leaves.size(); i++) { final Stopwatch leaf = leaves.get(i); stringBuilder.append(getIndentBlanks(level * 2)); leaf.appendTimingStat(level + 1, stringBuilder); } }
@Override public String toString() { float percentOfRoot = getPercentOfRoot(); if (0 > percentOfRoot) { percentOfRoot = 0F; } final BigDecimal percenOfRoot = new BigDecimal(percentOfRoot, MATH_CONTEXT); final StringBuilder stringBuilder = new StringBuilder("[").append(percenOfRoot).append("]%, [").append(getElapsedTime()).append("]ms [").append(getTaskTitle()).append("]").append( Strings.LINE_SEPARATOR); return stringBuilder.toString(); } }
/** * Gets the recent running stopwatch with the specified parent stopwatch. * * @param parent the specified parent stopwatch * @return the recent stopwatch, returns {@code null} if not found */ private static Stopwatch getRecentRunning(final Stopwatch parent) { if (null == parent) { return null; } final List<Stopwatch> leaves = parent.getLeaves(); if (leaves.isEmpty()) { if (parent.isRunning()) { return parent; } else { return null; } } for (int i = leaves.size() - 1; i > -1; i--) { final Stopwatch leaf = leaves.get(i); if (leaf.isRunning()) { return getRecentRunning(leaf); } else { continue; } } return parent; }
/** * Gets stopwatch from the specified parent stopwatch with the specified task title. * * @param parent the specified parent * @param taskTitle the specified task title * @return stopwatch, returns {@code null} if not found */ private static Stopwatch get(final Stopwatch parent, final String taskTitle) { if (taskTitle.equals(parent.getTaskTitle())) { return parent; } for (final Stopwatch leaf : parent.getLeaves()) { final Stopwatch ret = get(leaf, taskTitle); if (null != ret) { return ret; } } return null; }
/** * Gets the elapsed time percent of root. * * @return percent of root */ public float getPercentOfRoot() { final Stopwatch root = STOPWATCH.get(); if (null == root) { return 0; } final float rootElapsedTime = (float) root.getElapsedTime(); if (0 == rootElapsedTime) { // Denominator is equals to zero return 0; } return getElapsedTime() / rootElapsedTime * HUNDRED; }
@Override public String toString() { float percentOfRoot = getPercentOfRoot(); if (0 > percentOfRoot) { percentOfRoot = 0F; } final BigDecimal percenOfRoot = new BigDecimal(percentOfRoot, MATH_CONTEXT); final StringBuilder stringBuilder = new StringBuilder("[").append(percenOfRoot).append("]%, [").append(getElapsedTime()).append("]ms [").append(getTaskTitle()).append("]").append( Strings.LINE_SEPARATOR); return stringBuilder.toString(); } }