FEATURE: add table below graph

FEATURE: add table below graph

It is easier to reason about exact numbers in a table

diff --git a/plugin.rb b/plugin.rb
index 926737a..9076d42 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -38,15 +38,20 @@ after_initialize do
     return if post.post_number == 1
     return if !SiteSetting.dev_additions_benchmark_topic_ids.split(",").include?(post.topic_id.to_s)
 
-    data = Post.where('topic_id = ? AND post_number > 1', post.topic_id).pluck(:raw, :post_number)
+    rows = DB.query(<<~SQL, topic_id: post.topic_id)
+      SELECT raw, post_number, username
+      FROM posts p
+      LEFT JOIN users u on u.id = p.user_id
+      WHERE p.topic_id = :topic_id AND p.deleted_at IS NULL
+    SQL
 
     benches = {}
 
-    data.each do |(raw, post_number)|
+    rows.each do |row|
       desc = nil
       bench_parsed = nil
 
-      raw.each_line do |line|
+      row.raw.each_line do |line|
         desc = line.strip if !desc && line.present?
 
         if line.include? "on 1 core"
@@ -54,16 +59,18 @@ after_initialize do
         elsif line.match?(/on \d+ core/)
           bench_parsed = :multi
         elsif bench_parsed
-          match = line.match(/(.*): (\S+).*(op\/s|seconds)$/)
+          match = line.match(/(.*): (\S+)(.*)(op\/s|seconds)$/)
           if match
             name = match[1]
             duration = match[2].to_f
             durations = (benches["#{name}#{" (mp)" if bench_parsed == :multi}"] ||= [])
             durations << {
               duration: duration,
-              type: match[3] == "seconds" ? :secs : :ops,
-              post_number: post_number,
-              description: desc
+              type: match[4] == "seconds" ? :secs : :ops,
+              post_number: row.post_number,
+              description: desc,
+              variance: (match[3] || "").strip,
+              username: row.username
             }
           end
         end
@@ -74,7 +81,8 @@ after_initialize do
     markdown = +""
 
     benches.each do |name, metrics|
-      type = metrics[0][:type] == :ops ? "op/s (higher is better)" : "seconds (lower is better)"
+      result_type = metrics[0][:type]
+      type = result_type == :ops ? "op/s (higher is better)" : "seconds (lower is better)"
 
       markdown << <<~MD
         [chart borderColors="#f45,#c83,#d76" title="#{name.gsub('"', "")}" type="horizontalBar" xAxisTitle="#{type}" backgroundColors="#4cc"]
@@ -94,7 +102,24 @@ after_initialize do
         MD
       end
 
-      markdown << "[/chart]\n"
+      markdown << "[/chart]\n\n"
+
+      markdown << <<~MD
+        | desc | user | #{ result_type == :ops ? "op/s" : "seconds" } | variance | percent |
+        |-----|-----|----:|----:|----:|
+      MD
+
+      top_duration = metrics[0][:duration]
+      metrics.each do |row|
+        percent = ((top_duration - (top_duration - row[:duration])) / top_duration) * 100
+        percent = "#{percent.round(2)}%"
+        desc = row[:description].gsub("|", "\\|")
+        user_link = "[#{row[:username]}](#{post.topic.url}/#{row[:post_number]})"
+
+        markdown << <<~MD
+          #{desc}|#{user_link}|#{row[:duration]}|#{row[:variance]}|#{percent}|
+        MD
+      end
     end
 
     op = Post.find_by(topic_id: post.topic_id, post_number: 1)

GitHub sha: 9c6da821

1 Like