2022-11-12 09:29:20 -08:00
|
|
|
package com.rusefi.can.analysis;
|
|
|
|
|
2022-12-25 17:02:44 -08:00
|
|
|
import com.rusefi.can.CANPacket;
|
|
|
|
import com.rusefi.can.reader.CANLineReader;
|
|
|
|
import com.rusefi.util.FolderUtil;
|
|
|
|
|
2023-07-30 16:52:39 -07:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2022-11-12 09:29:20 -08:00
|
|
|
|
|
|
|
public class ByteRateOfChangeReports {
|
|
|
|
/**
|
|
|
|
* sweet baby O(n^2)
|
|
|
|
*/
|
2023-10-22 20:13:55 -07:00
|
|
|
public static void compareEachReportAgainstAllOthers(String reportDestinationFolder, List<ByteRateOfChange.TraceReport> reports, CanContext context) throws FileNotFoundException {
|
2022-11-12 09:29:20 -08:00
|
|
|
for (int i = 0; i < reports.size(); i++) {
|
|
|
|
for (int j = i + 1; j < reports.size(); j++)
|
2023-10-22 20:13:55 -07:00
|
|
|
compareTwoReports(reportDestinationFolder, reports.get(i), reports.get(j), context);
|
2022-11-12 09:29:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 20:13:55 -07:00
|
|
|
private static void compareTwoReports(String reportDestinationFolder, ByteRateOfChange.TraceReport traceReport1, ByteRateOfChange.TraceReport traceReport2, CanContext context) throws FileNotFoundException {
|
2023-07-30 16:52:39 -07:00
|
|
|
Set<ByteRateOfChange.ByteId> allKeys = new TreeSet<>();
|
2022-11-12 09:29:20 -08:00
|
|
|
allKeys.addAll(traceReport1.getStatistics().keySet());
|
|
|
|
allKeys.addAll(traceReport2.getStatistics().keySet());
|
|
|
|
|
2023-07-30 16:52:39 -07:00
|
|
|
String comparingFolder = reportDestinationFolder + File.separator + "comparison";
|
|
|
|
new File(comparingFolder).mkdirs();
|
|
|
|
|
|
|
|
String outputFileName = comparingFolder + File.separator + traceReport1.getSimpleFileName() + "-vs-" + traceReport2.getSimpleFileName() + ".txt";
|
|
|
|
PrintWriter report = new PrintWriter(new FileOutputStream(outputFileName));
|
2022-11-12 09:29:20 -08:00
|
|
|
|
|
|
|
report.println("Comparing unique value count per byte " + traceReport1.getSummary() + " and " + traceReport2.getSummary());
|
|
|
|
|
|
|
|
List<ByteVariationDifference> differences = new ArrayList<>();
|
|
|
|
|
2023-07-30 16:52:39 -07:00
|
|
|
report.println("******************** Sorted by key ********************");
|
|
|
|
|
|
|
|
|
2022-11-12 09:29:20 -08:00
|
|
|
for (ByteRateOfChange.ByteId id : allKeys) {
|
2023-10-22 20:13:55 -07:00
|
|
|
if (context.counterBytes.contains(id)) {
|
|
|
|
// skipping byte with a known counter
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (id.getByteIndex() == 7 && context.withChecksum.contains(id.sid)) {
|
|
|
|
// skipping known checksum byte
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-12 09:29:20 -08:00
|
|
|
ByteRateOfChange.ByteStatistics s1 = traceReport1.getStatistics().computeIfAbsent(id, ByteRateOfChange.ByteStatistics::new);
|
|
|
|
ByteRateOfChange.ByteStatistics s2 = traceReport2.getStatistics().computeIfAbsent(id, ByteRateOfChange.ByteStatistics::new);
|
|
|
|
|
2023-10-06 18:17:18 -07:00
|
|
|
if (s1.getUniqueValuesCount() != s2.getUniqueValuesCount()) {
|
|
|
|
String msg = id + ": " + s1.getUniqueValuesCount() + " vs " + s2.getUniqueValuesCount();
|
|
|
|
int deltaCount = Math.abs(s1.getUniqueValuesCount() - s2.getUniqueValuesCount());
|
2023-07-30 16:52:39 -07:00
|
|
|
differences.add(new ByteVariationDifference(deltaCount, msg));
|
2023-10-22 20:37:22 -07:00
|
|
|
report.println(msg + " delta=" + deltaCount + " / " + s1.totalTransitions + " vs " + s2.totalTransitions);
|
2022-11-12 09:29:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 16:52:39 -07:00
|
|
|
report.println("******************** Sorted by delta count ********************");
|
|
|
|
differences.sort((o1, o2) -> o2.deltaCount - o1.deltaCount);
|
2022-11-12 09:29:20 -08:00
|
|
|
for (ByteVariationDifference difference : differences)
|
|
|
|
report.println(difference.msg);
|
|
|
|
|
|
|
|
report.println(differences.size() + " total differences");
|
|
|
|
report.println();
|
|
|
|
report.println();
|
|
|
|
report.println();
|
|
|
|
report.println();
|
2023-07-30 16:52:39 -07:00
|
|
|
report.close();
|
2022-11-12 09:29:20 -08:00
|
|
|
}
|
2023-07-30 16:52:39 -07:00
|
|
|
|
2022-12-25 17:02:44 -08:00
|
|
|
public static String createOutputFolder(String inputFolderName) {
|
|
|
|
String reportDestinationFolder = inputFolderName + File.separator + "processed";
|
|
|
|
new File(reportDestinationFolder).mkdirs();
|
|
|
|
return reportDestinationFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void scanInputFolder(String inputFolderName, String fileNameSuffix) throws IOException {
|
|
|
|
String reportDestinationFolder = createOutputFolder(inputFolderName);
|
|
|
|
|
2023-10-22 20:13:55 -07:00
|
|
|
CanContext context = CanContext.read(inputFolderName);
|
|
|
|
|
2022-12-25 17:02:44 -08:00
|
|
|
List<ByteRateOfChange.TraceReport> reports = new ArrayList<>();
|
|
|
|
|
|
|
|
FolderUtil.handleFolder(inputFolderName, (simpleFileName, fullInputFileName) -> {
|
|
|
|
|
|
|
|
List<CANPacket> logFileContent = CANLineReader.getReader().readFile(fullInputFileName);
|
|
|
|
|
2023-07-30 16:52:39 -07:00
|
|
|
PerSidDump.handle(reportDestinationFolder, simpleFileName, logFileContent);
|
2023-10-22 20:13:55 -07:00
|
|
|
// at the moment we overwrite counter detection report after we process each file
|
2023-07-30 16:52:39 -07:00
|
|
|
CounterScanner.scanForCounters(reportDestinationFolder, logFileContent);
|
2023-10-22 20:13:55 -07:00
|
|
|
ChecksumScanner.scanForChecksums(reportDestinationFolder, logFileContent);
|
2023-07-30 16:52:39 -07:00
|
|
|
|
2023-07-30 21:01:10 -07:00
|
|
|
CanToMegaLogViewer.createMegaLogViewer(reportDestinationFolder, logFileContent, simpleFileName);
|
|
|
|
|
2022-12-25 17:02:44 -08:00
|
|
|
ByteRateOfChange.TraceReport report = ByteRateOfChange.process(reportDestinationFolder, simpleFileName, logFileContent);
|
|
|
|
reports.add(report);
|
|
|
|
}, fileNameSuffix);
|
2023-07-30 16:52:39 -07:00
|
|
|
|
|
|
|
|
2023-05-24 20:18:07 -07:00
|
|
|
System.out.println("Processing " + reports.size() + " report(s)");
|
2023-10-22 20:13:55 -07:00
|
|
|
compareEachReportAgainstAllOthers(reportDestinationFolder, reports, context);
|
2022-12-25 17:02:44 -08:00
|
|
|
}
|
2022-11-12 09:29:20 -08:00
|
|
|
|
|
|
|
static class ByteVariationDifference {
|
|
|
|
private final int deltaCount;
|
|
|
|
private final String msg;
|
|
|
|
|
|
|
|
public ByteVariationDifference(int deltaCount, String msg) {
|
|
|
|
this.deltaCount = deltaCount;
|
|
|
|
this.msg = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|