/projects/jruby-1.7.3/spec/profiler/graph_profile_printer_spec.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Ruby · 192 lines · 148 code · 44 blank · 0 comment · 33 complexity · aa7a23a0b6271880b1e00b02d496e463 MD5 · raw file

  1. require 'spec/profiler/profiler_spec_helpers'
  2. describe JRuby::Profiler, "::GraphProfilePrinter" do
  3. include JRuby::Profiler::SpecHelpers
  4. def find_row(rows, name)
  5. rows.detect {|r| r[:name] == name}
  6. end
  7. context "empty profile data" do
  8. before do
  9. @profile_data = JRuby::Profiler.profile {}
  10. end
  11. it "should print total time 0" do
  12. graph_output.should match(/Total time: 0.00/)
  13. end
  14. describe "the row for the top level" do
  15. before do
  16. @top_line = line_for(graph_output, "(top)")
  17. end
  18. it "should have calls 1" do
  19. @top_line.should_not be_nil
  20. @top_line[:calls].should == 1
  21. end
  22. it "should all times zero" do
  23. @top_line[:total].should == 0
  24. @top_line[:self].should == 0
  25. @top_line[:children].should == 0
  26. end
  27. it "should have all percentages 100%" do
  28. @top_line[:total_pc].should == "100%"
  29. @top_line[:self_pc].should == "100%"
  30. end
  31. end
  32. it "should not have a row for any JRuby::Profiler methods" do
  33. lines_for(graph_output, "JRuby::Profiler").should be_empty
  34. end
  35. end
  36. context "with profiling" do
  37. before do
  38. obj = ProfilerTest.new
  39. @profile_data = JRuby::Profiler.profile do
  40. obj.wait(0.01)
  41. obj.test_instance_method
  42. end
  43. end
  44. it "should have a main row for each method" do
  45. main_names = decode_graph(graph_output).map {|r| r[:name]}
  46. main_names.should include("ProfilerTest#wait")
  47. main_names.should include("ProfilerTest#test_instance_method")
  48. main_names.should include("Kernel.sleep")
  49. end
  50. it "each method should have the correct children" do
  51. graph = decode_graph(graph_output)
  52. wait_children = find_row(graph, "ProfilerTest#wait")[:children]
  53. wait_children.length.should == 1
  54. wait_children.first[:name].should == "Kernel.sleep"
  55. wait_children.first[:calls].should == [1, 1]
  56. find_row(graph, "ProfilerTest#test_instance_method")[:children].length.should == 0
  57. find_row(graph, "Kernel.sleep")[:children].length.should == 0
  58. end
  59. it "each method should have the correct parents" do
  60. graph = decode_graph(graph_output)
  61. wait_parents = find_row(graph, "ProfilerTest#wait")[:parents]
  62. wait_parents.length.should == 1
  63. wait_parents.first[:name].should == "(top)"
  64. parents = find_row(graph, "ProfilerTest#test_instance_method")[:parents]
  65. parents.length.should == 1
  66. parents.first[:name].should == "(top)"
  67. sleep_parents = find_row(graph, "Kernel.sleep")[:parents]
  68. sleep_parents.length.should == 1
  69. sleep_parents.first[:name].should == "ProfilerTest#wait"
  70. sleep_parents.first[:calls].should == [1, 1]
  71. end
  72. end
  73. context "with recursive profiling" do
  74. describe "calls, children and parents" do
  75. before do
  76. obj = ProfilerTest.new
  77. @profile_data = JRuby::Profiler.profile do
  78. obj.recurse(3)
  79. end
  80. end
  81. def recurse_rows
  82. graph = decode_graph(graph_output)
  83. graph.select {|r| r[:name].include?("recurse")}
  84. end
  85. it "should have only one row for the recursive method" do
  86. recurse_rows.length.should == 1
  87. end
  88. it "should have the right number of calls" do
  89. recurse_rows.first[:calls].should == 4
  90. end
  91. describe "the recursive methods children" do
  92. it "should be responsible for all the calls of itself bar one" do
  93. child_row = find_row(recurse_rows.first[:children], "ProfilerTest#recurse")
  94. child_row[:calls].should == [3, 4]
  95. end
  96. end
  97. describe "the recursive methods parent rows" do
  98. it "should be responsible for all the calls of itself bar one" do
  99. parent_row = find_row(recurse_rows.first[:parents], "ProfilerTest#recurse")
  100. parent_row[:calls].should == [3, 4]
  101. end
  102. it "and the other call should come from the top level" do
  103. parent_row = find_row(recurse_rows.first[:parents], "(top)")
  104. parent_row[:calls].should == [1, 4]
  105. end
  106. end
  107. end
  108. describe "durations" do
  109. before do
  110. obj = ProfilerTest.new
  111. @profile_data = JRuby::Profiler.profile do
  112. obj.recurse_wait(3, 0.05)
  113. end
  114. end
  115. it "the time for the main row should not be zero" do
  116. graph = decode_graph(graph_output)
  117. find_row(graph, "ProfilerTest#recurse_wait")[:total].should > 0
  118. end
  119. it "the recursive parent should have zero time" do
  120. graph = decode_graph(graph_output)
  121. main = find_row(graph, "ProfilerTest#recurse_wait")
  122. parent = find_row(main[:parents], "ProfilerTest#recurse_wait")[:total].should == 0
  123. end
  124. it "the recursive child should have zero time" do
  125. graph = decode_graph(graph_output)
  126. main = find_row(graph, "ProfilerTest#recurse_wait")
  127. parent = find_row(main[:children], "ProfilerTest#recurse_wait")[:total].should == 0
  128. end
  129. end
  130. end
  131. context "with recursive methods where the profiling is started inside the recursion" do
  132. before do
  133. obj = ProfilerTest.new
  134. obj.recurse_and_start_profiling(3)
  135. @profile_data = JRuby::Profiler.stop
  136. end
  137. it "should have the correct call info" do
  138. recursive_method = "ProfilerTest#recurse_and_start_profiling"
  139. graph = decode_graph(graph_output)
  140. row = find_row(graph, recursive_method)
  141. row[:calls].should == 4
  142. parent = find_row(row[:parents], recursive_method)
  143. parent[:calls].should == [3, 4]
  144. child = find_row(row[:children], recursive_method)
  145. child[:calls].should == [3, 4]
  146. end
  147. end
  148. end