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.

ProfilerSection.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.debug.internal;
  17. import java.util.Collection;
  18. import java.util.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map.Entry;
  21. import java.util.Set;
  22. import com.google.gwt.user.client.ui.FlowPanel;
  23. import com.google.gwt.user.client.ui.HorizontalPanel;
  24. import com.google.gwt.user.client.ui.Label;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.client.ApplicationConnection;
  27. import com.vaadin.client.Profiler;
  28. import com.vaadin.client.Profiler.Node;
  29. import com.vaadin.client.Profiler.ProfilerResultConsumer;
  30. import com.vaadin.client.SimpleTree;
  31. import com.vaadin.client.Util;
  32. import com.vaadin.client.ValueMap;
  33. /**
  34. * Debug window section for investigating {@link Profiler} data. This section is
  35. * only visible if the profiler is enabled ({@link Profiler#isEnabled()}).
  36. *
  37. * @since 7.1
  38. * @author Vaadin Ltd
  39. *
  40. * @see Profiler
  41. */
  42. public class ProfilerSection implements Section {
  43. private static final int MAX_ROWS = 10;
  44. private final DebugButton tabButton = new DebugButton(Icon.RESET_TIMER,
  45. "Profiler");
  46. private final HorizontalPanel controls = new HorizontalPanel();
  47. private final FlowPanel content = new FlowPanel();
  48. public ProfilerSection() {
  49. Profiler.setProfilerResultConsumer(new ProfilerResultConsumer() {
  50. @Override
  51. public void addProfilerData(Node rootNode, List<Node> totals) {
  52. double totalTime = 0;
  53. int eventCount = 0;
  54. for (Node node : totals) {
  55. totalTime += node.getTimeSpent();
  56. eventCount += node.getCount();
  57. }
  58. SimpleTree drillDownTree = (SimpleTree) buildTree(rootNode);
  59. drillDownTree.setText("Drill down");
  60. SimpleTree offendersTree = new SimpleTree("Longest events");
  61. for (int i = 0; i < totals.size() && i < 20; i++) {
  62. Node node = totals.get(i);
  63. offendersTree
  64. .add(new Label(node.getStringRepresentation("")));
  65. }
  66. SimpleTree root = new SimpleTree(
  67. eventCount + " profiler events using "
  68. + Util.round(totalTime, 3) + " ms");
  69. root.add(drillDownTree);
  70. root.add(offendersTree);
  71. root.open(false);
  72. content.add(root);
  73. applyLimit();
  74. }
  75. @Override
  76. public void addBootstrapData(
  77. LinkedHashMap<String, Double> timings) {
  78. SimpleTree tree = new SimpleTree(
  79. "Time since window.performance.timing events");
  80. Set<Entry<String, Double>> entrySet = timings.entrySet();
  81. for (Entry<String, Double> entry : entrySet) {
  82. tree.add(
  83. new Label(entry.getValue() + " " + entry.getKey()));
  84. }
  85. tree.open(false);
  86. content.add(tree);
  87. applyLimit();
  88. }
  89. });
  90. }
  91. private Widget buildTree(Node node) {
  92. String message = node.getStringRepresentation("");
  93. Collection<Node> children = node.getChildren();
  94. if (node.getName() == null || !children.isEmpty()) {
  95. SimpleTree tree = new SimpleTree(message);
  96. for (Node childNode : children) {
  97. Widget child = buildTree(childNode);
  98. tree.add(child);
  99. }
  100. return tree;
  101. } else {
  102. return new Label(message);
  103. }
  104. }
  105. private void applyLimit() {
  106. while (content.getWidgetCount() > MAX_ROWS) {
  107. content.remove(0);
  108. }
  109. }
  110. @Override
  111. public DebugButton getTabButton() {
  112. return tabButton;
  113. }
  114. @Override
  115. public Widget getControls() {
  116. return controls;
  117. }
  118. @Override
  119. public Widget getContent() {
  120. return content;
  121. }
  122. @Override
  123. public void show() {
  124. // Nothing to do
  125. }
  126. @Override
  127. public void hide() {
  128. // Nothing to do
  129. }
  130. @Override
  131. public void meta(ApplicationConnection ac, ValueMap meta) {
  132. // Nothing to do
  133. }
  134. @Override
  135. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  136. // Nothing to do
  137. }
  138. }