xndai commented on a change in pull request #1910: [CALCITE-3915] Add rule 
listener to report rule attempts and time at …
URL: https://github.com/apache/calcite/pull/1910#discussion_r407310226
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/plan/volcano/VolcanoPlanner.java
 ##########
 @@ -1757,4 +1738,66 @@ protected void onMatch() {
       this.callId = callId;
     }
   }
+
+  /** Listener for counting the attempts of each rule. Only enabled under 
DEBUG level.*/
+  private class RuleAttemptsListener implements RelOptListener {
+    private long totalAttempts;
+    private long beforeTimestamp;
+    private Map<String, Pair<Long, Long>> ruleAttempts;
+
+
+    RuleAttemptsListener() {
+      this.totalAttempts = 0;
+      ruleAttempts = new HashMap<>();
+    }
+
+    @Override public void relEquivalenceFound(RelEquivalenceEvent event) {
+    }
+
+    @Override public void ruleAttempted(RuleAttemptedEvent event) {
+      if (event.isBefore()) {
+        this.beforeTimestamp = System.nanoTime();
+      } else {
+        ++this.totalAttempts;
+        long elapsed = (System.nanoTime() - this.beforeTimestamp) / 1000;
+        String rule = event.getRuleCall().getRule().toString();
+        if (ruleAttempts.containsKey(rule)) {
+          Pair<Long, Long> p = ruleAttempts.get(rule);
+          ruleAttempts.put(rule, Pair.of(p.left + 1, p.right + elapsed));
+        } else {
+          ruleAttempts.put(rule, Pair.of(1L,  elapsed));
+        }
+      }
+    }
+
+    @Override public void ruleProductionSucceeded(RuleProductionEvent event) {
+    }
+
+    @Override public void relDiscarded(RelDiscardedEvent event) {
+    }
+
+    @Override public void relChosen(RelChosenEvent event) {
+    }
+
+    public String dump() {
+      // Sort rules by number of attempts
+      List<Map.Entry<String, Pair<Long, Long>>> list =
+          new ArrayList<>(this.ruleAttempts.entrySet());
+      Collections.sort(list,
+          (left, right) -> 
right.getValue().left.compareTo(left.getValue().left));
+
+      // Print out rule attempts and time
+      StringBuilder sb = new StringBuilder();
+      sb.append(String
+          .format(Locale.ROOT, "%n%-60s%20s%20s%n", "Rules", "Attempts", "Time 
(us)"));
+      for (Map.Entry<String, Pair<Long, Long>> entry : list) {
+        sb.append(
+            String.format(Locale.ROOT, "%-60s%20s%20s%n",
 
 Review comment:
   It's only referenced at two places. Not very necessary.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to