ayushtkn commented on code in PR #4749:
URL: https://github.com/apache/hive/pull/4749#discussion_r1346452047
##########
common/src/java/org/apache/hadoop/hive/ql/log/PerfLogger.java:
##########
@@ -131,7 +128,9 @@ public static void setPerfLogger(PerfLogger
resetPerfLogger) {
public void perfLogBegin(String callerName, String method) {
long startTime = System.currentTimeMillis();
startTimes.put(method, Long.valueOf(startTime));
- LOG.debug("<PERFLOG method={} from={}>", method, callerName);
+ if (LOG.isDebugEnabled()) {
Review Comment:
do we need ``isDebugEnabled`` check, sl4j will check that, I don't think we
need explicit checks?
##########
common/src/java/org/apache/hadoop/hive/ql/log/PerfLogger.java:
##########
@@ -177,21 +176,13 @@ public long perfLogEnd(String callerName, String method,
String additionalInfo)
}
public Long getStartTime(String method) {
- long startTime = 0L;
-
- if (startTimes.containsKey(method)) {
- startTime = startTimes.get(method);
- }
- return startTime;
+ Long time = startTimes.get(method);
+ return time != null? time.longValue() : 0L;
Review Comment:
can we do
``
return startTimes.getOrDefault(method, 0L);
``
##########
common/src/test/org/apache/hadoop/hive/ql/log/PerfLoggerTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+
+package org.apache.hadoop.hive.ql.log;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class PerfLoggerTest {
+ private static void snooze(int ms) {
+ try {
+ Thread.currentThread().sleep(ms);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void testBasic() {
+ final PerfLogger pl = PerfLogger.getPerfLogger(null, true);
+ pl.perfLogBegin("test", PerfLogger.COMPILE);
+ snooze(100);
+ pl.perfLogEnd("test", PerfLogger.COMPILE);
+ long duration = pl.getDuration(PerfLogger.COMPILE);
+ Assert.assertTrue(duration >= 100);
+ }
+
+ @Test
+ public void testMT() throws InterruptedException {
+ final PerfLogger pl = PerfLogger.getPerfLogger(null, true);
+ // we run concurrently the getEndTimes and perfLogBegin/perfLogEnd:
+ // on a Mac M1, this test fails easily if the perflooger maps are hashmaps
+ ExecutorService executorService = Executors.newFixedThreadPool(64);
+ // An executing threads counter
+ AtomicInteger count = new AtomicInteger(0);
+ // getEndTimes in a loop
+ executorService.execute(new Runnable() {
+ public void run() {
+ PerfLogger.setPerfLogger(pl);
+ try {
+ count.incrementAndGet();
+ snooze(100);
+ for (int i = 0; i < 64; ++i) {
+ snooze(50);
+ Map<String, Long> et = pl.getEndTimes();
+ Assert.assertNotNull(et);
+ }
+ } finally {
+ count.decrementAndGet();
+ synchronized (count) {
+ count.notifyAll();
+ }
+ }
+ }
+ });
+ // 32 threads calling perLogBeing/perfLogEnd
+ for(int t = 0; t < 31; ++t) {
+ executorService.execute(new Runnable() {
Review Comment:
can use lambda
##########
common/src/java/org/apache/hadoop/hive/ql/log/PerfLogger.java:
##########
@@ -89,17 +90,13 @@ public class PerfLogger {
public static final String HIVE_GET_NOT_NULL_CONSTRAINT =
"getNotNullConstraints";
public static final String HIVE_GET_TABLE_CONSTRAINTS =
"getTableConstraints";
- protected final Map<String, Long> startTimes = new HashMap<String, Long>();
- protected final Map<String, Long> endTimes = new HashMap<String, Long>();
+ protected final Map<String, Long> startTimes = new ConcurrentHashMap<>();
+ protected final Map<String, Long> endTimes = new ConcurrentHashMap<>();
- static final private Logger LOG =
LoggerFactory.getLogger(PerfLogger.class.getName());
- protected static final ThreadLocal<PerfLogger> perfLogger = new
ThreadLocal<PerfLogger>();
+ private static final Logger LOG =
LoggerFactory.getLogger(PerfLogger.class.getName());
+ protected static final ThreadLocal<PerfLogger> perfLogger = new
ThreadLocal<>();
- private PerfLogger() {
- // Use getPerfLogger to get an instance of PerfLogger
- }
-
Review Comment:
why is this removed?
##########
common/src/java/org/apache/hadoop/hive/ql/log/PerfLogger.java:
##########
@@ -203,11 +194,12 @@ public boolean endTimeHasMethod(String method) {
}
public Long getDuration(String method) {
- long duration = 0;
- if (startTimes.containsKey(method) && endTimes.containsKey(method)) {
- duration = endTimes.get(method) - startTimes.get(method);
+ Long startTime = startTimes.get(method);
+ Long endTime = endTimes.get(method);
+ if (startTime != null && endTime != null) {
+ return endTime.longValue() - startTime.longValue();
Review Comment:
return type is ``Long``, why ``.longValue()``?
Can do directly
```
return endTime - startTime;
```
##########
common/src/test/org/apache/hadoop/hive/ql/log/PerfLoggerTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+
+package org.apache.hadoop.hive.ql.log;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class PerfLoggerTest {
+ private static void snooze(int ms) {
+ try {
+ Thread.currentThread().sleep(ms);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void testBasic() {
+ final PerfLogger pl = PerfLogger.getPerfLogger(null, true);
+ pl.perfLogBegin("test", PerfLogger.COMPILE);
+ snooze(100);
+ pl.perfLogEnd("test", PerfLogger.COMPILE);
+ long duration = pl.getDuration(PerfLogger.COMPILE);
+ Assert.assertTrue(duration >= 100);
+ }
+
+ @Test
+ public void testMT() throws InterruptedException {
+ final PerfLogger pl = PerfLogger.getPerfLogger(null, true);
+ // we run concurrently the getEndTimes and perfLogBegin/perfLogEnd:
+ // on a Mac M1, this test fails easily if the perflooger maps are hashmaps
+ ExecutorService executorService = Executors.newFixedThreadPool(64);
+ // An executing threads counter
+ AtomicInteger count = new AtomicInteger(0);
+ // getEndTimes in a loop
+ executorService.execute(new Runnable() {
Review Comment:
can use lambda
```
executorService.execute(() -> {
PerfLogger.setPerfLogger(pl);
try {
count.incrementAndGet();
snooze(100);
for (int i = 0; i < 64; ++i) {
snooze(50);
Map<String, Long> et = pl.getEndTimes();
Assert.assertNotNull(et);
}
} finally {
count.decrementAndGet();
synchronized (count) {
count.notifyAll();
}
}
```
##########
common/src/java/org/apache/hadoop/hive/ql/log/PerfLogger.java:
##########
@@ -177,21 +176,13 @@ public long perfLogEnd(String callerName, String method,
String additionalInfo)
}
public Long getStartTime(String method) {
- long startTime = 0L;
-
- if (startTimes.containsKey(method)) {
- startTime = startTimes.get(method);
- }
- return startTime;
+ Long time = startTimes.get(method);
+ return time != null? time.longValue() : 0L;
}
public Long getEndTime(String method) {
- long endTime = 0L;
-
- if (endTimes.containsKey(method)) {
- endTime = endTimes.get(method);
- }
- return endTime;
+ Long time = endTimes.get(method);
+ return time != null? time.longValue() : 0L;
Review Comment:
Can do?
```
return endTimes.getOrDefault(method, 0L);
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]