aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTerry Parker <tparker@google.com>2020-10-21 11:53:35 -0400
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2020-10-21 11:53:35 -0400
commitdd593205a02ecf579f55738a98820be7bbf51e01 (patch)
tree6e07480dc51a9d0e7473a0c63a04e3f5e75b1392
parentde914b7caf763b74161552285fed409fe552df64 (diff)
parenta09e2051764c751b69eee7ba52c5e8cf7bb3197a (diff)
downloadjgit-dd593205a02ecf579f55738a98820be7bbf51e01.tar.gz
jgit-dd593205a02ecf579f55738a98820be7bbf51e01.zip
Merge "Add new performance logging"
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/logging/PerformanceLogContextTest.java66
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogContext.java67
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogRecord.java55
3 files changed, 188 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/logging/PerformanceLogContextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/logging/PerformanceLogContextTest.java
new file mode 100644
index 0000000000..e061833d17
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/logging/PerformanceLogContextTest.java
@@ -0,0 +1,66 @@
+package org.eclipse.jgit.logging;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import java.util.List;
+
+/**
+ * Tests for performance log context utilities.
+ */
+public class PerformanceLogContextTest {
+
+ @Test
+ public void testAddEvent() {
+ PerformanceLogRecord record = new PerformanceLogRecord("record", 0);
+ PerformanceLogContext.getInstance().addEvent(record);
+
+ List<PerformanceLogRecord> eventRecords = PerformanceLogContext
+ .getInstance().getEventRecords();
+ assertTrue(eventRecords.contains(record));
+ assertEquals(1, eventRecords.size());
+ }
+
+ @Test
+ public void testCleanEvents() {
+ PerformanceLogRecord record1 = new PerformanceLogRecord("record1", 0);
+ PerformanceLogContext.getInstance().addEvent(record1);
+
+ PerformanceLogRecord record2 = new PerformanceLogRecord("record2", 0);
+ PerformanceLogContext.getInstance().addEvent(record2);
+
+ PerformanceLogContext.getInstance().cleanEvents();
+ List<PerformanceLogRecord> eventRecords = PerformanceLogContext
+ .getInstance().getEventRecords();
+ assertEquals(0, eventRecords.size());
+ }
+
+ @Test
+ public void testAddEventsTwoThreads() {
+ TestRunnable thread1 = new TestRunnable("record1", 1);
+ TestRunnable thread2 = new TestRunnable("record2", 2);
+
+ new Thread(thread1).start();
+ new Thread(thread2).start();
+ }
+
+ private static class TestRunnable implements Runnable {
+ private String name;
+ private long durationMs;
+
+ public TestRunnable(String name, long durationMs) {
+ this.name = name;
+ this.durationMs = durationMs;
+ }
+
+ @Override
+ public void run() {
+ PerformanceLogRecord record = new PerformanceLogRecord(name,
+ durationMs);
+ PerformanceLogContext.getInstance().addEvent(record);
+ assertEquals(1, PerformanceLogContext.getInstance()
+ .getEventRecords().size());
+ }
+ }
+}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogContext.java b/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogContext.java
new file mode 100644
index 0000000000..4421efd7f3
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogContext.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2020, Google LLC and others
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+package org.eclipse.jgit.logging;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Singleton that collects performance logs.
+ *
+ * @since 5.10
+ */
+public class PerformanceLogContext {
+ /** Singleton instance that stores the statistics. */
+ private static final PerformanceLogContext INSTANCE = new PerformanceLogContext();
+
+ /** List that stores events as performance logs. */
+ private final ThreadLocal<List<PerformanceLogRecord>> eventRecords = new ThreadLocal<>();
+
+ private PerformanceLogContext() {
+ eventRecords.set(new ArrayList<>());
+ }
+
+ /**
+ * Get the instance of the context.
+ *
+ * @return instance of performance log context.
+ */
+ public static PerformanceLogContext getInstance() {
+ return INSTANCE;
+ }
+
+ /**
+ * Get the unmodifiable list of events as performance records.
+ *
+ * @return unmodifiable list of events as performance logs.
+ */
+ public List<PerformanceLogRecord> getEventRecords() {
+ return Collections.unmodifiableList(eventRecords.get());
+ }
+
+ /**
+ * Adds a performance log record to the current list of events.
+ *
+ * @param record
+ * performance log record that is going to be added.
+ */
+ public void addEvent(PerformanceLogRecord record) {
+ eventRecords.get().add(record);
+ }
+
+ /**
+ * Removes all of the existing records from the current list of events.
+ */
+ public void cleanEvents() {
+ eventRecords.get().clear();
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogRecord.java b/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogRecord.java
new file mode 100644
index 0000000000..3dc5f20156
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/logging/PerformanceLogRecord.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2020, Google LLC and others
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+package org.eclipse.jgit.logging;
+
+/**
+ * Class to register a performance log record.
+ *
+ * @since 5.10
+ */
+public class PerformanceLogRecord {
+ /** Name of the recorded event. */
+ private String name;
+
+ /** Duration of the recorded event in milliseconds. */
+ private long durationMs;
+
+ /**
+ * Create a new performance log record for an event.
+ *
+ * @param name
+ * name of the event.
+ * @param durationMs
+ * duration in milliseconds of the event.
+ */
+ public PerformanceLogRecord(String name, long durationMs) {
+ this.name = name;
+ this.durationMs = durationMs;
+ }
+
+ /**
+ * Get the name of the recorded event.
+ *
+ * @return name of the recorded event.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Get the duration in milliseconds of the recorded event.
+ *
+ * @return duration in milliseconds of the recorded event.
+ */
+ public long getDurationMs() {
+ return durationMs;
+ }
+} \ No newline at end of file