xiangfu0 commented on code in PR #15192: URL: https://github.com/apache/pinot/pull/15192#discussion_r1983127288
########## pinot-query-planner/src/main/java/org/apache/pinot/query/context/RuleTimingPlannerListener.java: ########## @@ -0,0 +1,89 @@ +package org.apache.pinot.query.context; + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.HashMap; +import java.util.Map; +import org.apache.calcite.plan.RelOptListener; +import org.apache.calcite.plan.RelOptRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class RuleTimingPlannerListener implements RelOptListener { + private static final Logger LOGGER = LoggerFactory.getLogger(RuleTimingPlannerListener.class); + public static final String RULE_TIMINGS = "RULE_TIMINGS"; + + private final PlannerContext _plannerContext; + private final Map<RelOptRule, Long> _ruleStartTimes = new HashMap<>(); + private final Map<RelOptRule, Long> _ruleDurations = new HashMap<>(); + + public RuleTimingPlannerListener(PlannerContext plannerContext) { + _plannerContext = plannerContext; + } + + @Override + public void ruleAttempted(RuleAttemptedEvent event) { + // Capture start time when a rule is attempted + if (event.isBefore()) { + _ruleStartTimes.put(event.getRuleCall().getRule(), System.nanoTime()); + } else { + if (_ruleStartTimes.containsKey(event.getRuleCall().getRule())) { + long duration = System.nanoTime() - _ruleStartTimes.get(event.getRuleCall().getRule()); + _ruleDurations.put(event.getRuleCall().getRule(), + _ruleDurations.getOrDefault(event.getRuleCall().getRule(), 0L) + duration); + } + } + } + + @Override + public void ruleProductionSucceeded(RuleProductionEvent event) { + } + + @Override + public void relEquivalenceFound(RelEquivalenceEvent event) { + /* Not used */ + } + + @Override + public void relDiscarded(RelDiscardedEvent event) { + /* Not used */ + } + + @Override + public void relChosen(RelChosenEvent event) { + /* Not used */ + } + + public void printRuleTimings() { + String ruleTimings = getRuleTimings(); + LOGGER.info(ruleTimings); + _plannerContext.getPlannerOutput().put(RULE_TIMINGS, ruleTimings); + } + + public String getRuleTimings() { + StringBuilder sb = new StringBuilder(); + sb.append("--- Rule Execution Times ---"); + for (Map.Entry<RelOptRule, Long> entry : _ruleDurations.entrySet()) { + sb.append("\nRule: ").append(entry.getKey()).append(" -> Time: ").append(entry.getValue() / 1_000_000.0); + } + return sb.toString(); + } Review Comment: done, also found a bug in the `PinotRelJsonWriter` 😮💨 and fixed that -- 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. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org