vlsi 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_r409385740
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java
 ##########
 @@ -453,4 +467,63 @@ protected MulticastRelOptListener getListener() {
         : Collections.singletonList(relType);
     return Pair.of(digest, v);
   }
+
+  /** Listener for counting the attempts of each rule. Only enabled under 
DEBUG level.*/
+  private class RuleAttemptsListener implements RelOptListener {
+    private long beforeTimestamp;
+    private Map<String, Pair<Long, Long>> ruleAttempts;
+
+    RuleAttemptsListener() {
+      ruleAttempts = new HashMap<>();
+    }
+
+    @Override public void relEquivalenceFound(RelEquivalenceEvent event) {
+    }
+
+    @Override public void ruleAttempted(RuleAttemptedEvent event) {
+      if (event.isBefore()) {
+        this.beforeTimestamp = System.nanoTime();
+      } else {
+        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)"));
+      NumberFormat usFormat = NumberFormat.getNumberInstance(Locale.US);
+      for (Map.Entry<String, Pair<Long, Long>> entry : list) {
+        sb.append(
+            String.format(Locale.ROOT, "%-60s%20s%20s%n",
+                entry.getKey(),
+                usFormat.format(entry.getValue().left),
+                
usFormat.getNumberInstance(Locale.US).format(entry.getValue().right)));
 
 Review comment:
   ```suggestion
                   usFormat.format(entry.getValue().right)));
   ```
   
   Is there a reason you did not move the second `getNumberInstance` out of the 
loop?

----------------------------------------------------------------
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