Skip to content

Commit

Permalink
sort feeds on the no reference list and limit them to 25 maximum items
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamez committed Oct 3, 2024
1 parent b10320d commit 97678ee
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

/** Register for memory usage snapshots. */
public class MemoryUsageRegister {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

Expand All @@ -16,14 +17,27 @@ private MemoryUsageRegister() {
runtime = Runtime.getRuntime();
}

/** @return the singleton instance of the memory usage register. */
public static MemoryUsageRegister getInstance() {
return instance;
}

/**
* Returns the memory usage registry.
*
* @return the memory usage registry unmodifiable list.
*/
public List<MemoryUsage> getRegistry() {
return Collections.unmodifiableList(registry);
}

/**
* Returns a memory usage snapshot.
*
* @param key
* @param previous
* @return
*/
public MemoryUsage getMemoryUsageSnapshot(String key, MemoryUsage previous) {
Long memoryDiff = null;
if (previous != null) {
Expand All @@ -33,23 +47,43 @@ public MemoryUsage getMemoryUsageSnapshot(String key, MemoryUsage previous) {
key, runtime.totalMemory(), runtime.freeMemory(), runtime.maxMemory(), memoryDiff);
}

/**
* Registers a memory usage snapshot.
*
* @param key
* @return
*/
public MemoryUsage registerMemoryUsage(String key) {
MemoryUsage memoryUsage = getMemoryUsageSnapshot(key, null);
registerMemoryUsage(memoryUsage);
return memoryUsage;
}

/**
* Registers a memory usage snapshot.
*
* @param key
* @param previous previous memory usage snapshot used to compute the memory difference between
* two snapshots.
* @return
*/
public MemoryUsage registerMemoryUsage(String key, MemoryUsage previous) {
MemoryUsage memoryUsage = getMemoryUsageSnapshot(key, previous);
registerMemoryUsage(memoryUsage);
return memoryUsage;
}

/**
* Registers a memory usage snapshot.
*
* @param memoryUsage
*/
public void registerMemoryUsage(MemoryUsage memoryUsage) {
registry.add(memoryUsage);
logger.atInfo().log(memoryUsage.humanReadablePrint());
}

/** Clears the memory usage registry. */
public void clearRegistry() {
registry.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.util.stream.Collectors;
import org.mobilitydata.gtfsvalidator.performance.MemoryUsage;

/**
* Represents memory usage information for a dataset. This class contains the information associated
* with the memory usage of a dataset when running the validation process.
*/
public class DatasetMemoryUsage {

private String datasetId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.mobilitydata.gtfsvalidator.outputcomparator.io;

import java.util.Comparator;
import org.mobilitydata.gtfsvalidator.performance.MemoryUsage;

/**
* Comparator to compare two {@link DatasetMemoryUsage} objects based on the difference between the
* used memory of the two objects based on the {@link DatasetMemoryUsage#getLatestMemoryUsage}.
*/
public class LatestReportUsedMemoryIncreasedComparator implements Comparator<DatasetMemoryUsage> {

@Override
public int compare(DatasetMemoryUsage o1, DatasetMemoryUsage o2) {
if (o1 == o2) {
return 0;
}
if (o1 == null || o2 == null) {
return o1 == null ? -1 : 1;
}
if (o1.getLatestMemoryUsage() == null && o2.getLatestMemoryUsage() == null) {
return 0;
}
if (o1.getLatestMemoryUsage() == null || o2.getLatestMemoryUsage() == null) {
return o1.getLatestMemoryUsage() == null ? -1 : 1;
}
long o1MinDiff =
o1.getLatestMemoryUsage().stream()
.min(Comparator.comparingLong(MemoryUsage::usedMemory))
.get()
.usedMemory();
long o2MinDiff =
o2.getLatestMemoryUsage().stream()
.min(Comparator.comparingLong(MemoryUsage::usedMemory))
.get()
.usedMemory();
return Long.compare(o1MinDiff, o2MinDiff);
}

@Override
public Comparator<DatasetMemoryUsage> reversed() {
return Comparator.super.reversed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public int compare(DatasetMemoryUsage o1, DatasetMemoryUsage o2) {
if (o1 == null || o2 == null) {
return o1 == null ? -1 : 1;
}
if (o1.getReferenceMemoryUsage() == null && o2.getLatestMemoryUsage() == null) {
if (o1.getReferenceMemoryUsage() == null
&& o1.getLatestMemoryUsage() == null
&& o2.getReferenceMemoryUsage() == null
&& o2.getLatestMemoryUsage() == null) {
return 0;
}
if (o1.getReferenceMemoryUsage() == null || o2.getLatestMemoryUsage() == null) {
if (o1.getReferenceMemoryUsage() == null || o2.getReferenceMemoryUsage() == null) {
return o1.getReferenceMemoryUsage() == null ? -1 : 1;
}
if (o1.getLatestMemoryUsage() == null || o2.getLatestMemoryUsage() == null) {
return o1.getLatestMemoryUsage() == null ? -1 : 1;
}
long o1MinDiff =
getMinimumDifferenceByKey(o1.getReferenceUsedMemoryByKey(), o1.getLatestUsedMemoryByKey());
long o2MinDiff =
Expand All @@ -48,9 +54,4 @@ private long getMinimumDifferenceByKey(
.min()
.orElse(Long.MAX_VALUE);
}

@Override
public Comparator<DatasetMemoryUsage> reversed() {
return Comparator.super.reversed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ public String generateLogString() {
addMemoryUsageReport(decreasedMemoryUsages, "memory has decreased", b, true);
}
if (datasetsMemoryUsageNoReference.size() > 0) {
addMemoryUsageReport(datasetsMemoryUsageNoReference, "no reference available", b, false);
datasetsMemoryUsageNoReference.sort(new LatestReportUsedMemoryIncreasedComparator());
addMemoryUsageReport(
datasetsMemoryUsageNoReference.subList(
0, Math.min(datasetsMemoryUsageNoReference.size(), MEMORY_USAGE_COMPARE_MAX)),
"no reference available",
b,
false);
}
b.append("</details>\n");
}
Expand Down

0 comments on commit 97678ee

Please sign in to comment.