MetricsPersister.java

net.minecraft.util.profiling.metrics.storage.MetricsPersister

信息

  • 全限定名:net.minecraft.util.profiling.metrics.storage.MetricsPersister
  • 类型:public class
  • 包:net.minecraft.util.profiling.metrics.storage
  • 源码路径:src/main/java/net/minecraft/util/profiling/metrics/storage/MetricsPersister.java
  • 起始行号:L28
  • 职责:

    TODO

字段/常量

  • PROFILING_RESULTS_DIR

    • 类型: Path
    • 修饰符: public static final
    • 源码定位: L29
    • 说明:

      TODO

  • METRICS_DIR_NAME

    • 类型: String
    • 修饰符: public static final
    • 源码定位: L30
    • 说明:

      TODO

  • DEVIATIONS_DIR_NAME

    • 类型: String
    • 修饰符: public static final
    • 源码定位: L31
    • 说明:

      TODO

  • PROFILING_RESULT_FILENAME

    • 类型: String
    • 修饰符: public static final
    • 源码定位: L32
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L33
    • 说明:

      TODO

  • rootFolderName

    • 类型: String
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

内部类/嵌套类型

构造器

public MetricsPersister(String rootFolderName) @ L36

  • 构造器名:MetricsPersister
  • 源码定位:L36
  • 修饰符:public

参数:

  • rootFolderName: String

说明:

TODO

方法

下面的方法块按源码顺序生成。

public Path saveReports(Set<MetricSampler> samplers, Map<MetricSampler,List<RecordedDeviation>> deviationsBySampler, ProfileResults profilerResults) @ L40

  • 方法名:saveReports
  • 源码定位:L40
  • 返回类型:Path
  • 修饰符:public

参数:

  • samplers: Set
  • deviationsBySampler: Map<MetricSampler,List>
  • profilerResults: ProfileResults

说明:

TODO

private void saveMetrics(Set<MetricSampler> samplers, Path dir) @ L65

  • 方法名:saveMetrics
  • 源码定位:L65
  • 返回类型:void
  • 修饰符:private

参数:

  • samplers: Set
  • dir: Path

说明:

TODO

private void saveCategory(MetricCategory category, List<MetricSampler> samplers, Path dir) @ L74

  • 方法名:saveCategory
  • 源码定位:L74
  • 返回类型:void
  • 修饰符:private

参数:

  • category: MetricCategory
  • samplers: List
  • dir: Path

说明:

TODO

private void saveDeviations(Map<MetricSampler,List<RecordedDeviation>> deviationsBySampler, Path directory) @ L108

  • 方法名:saveDeviations
  • 源码定位:L108
  • 返回类型:void
  • 修饰符:private

参数:

  • deviationsBySampler: Map<MetricSampler,List>
  • directory: Path

说明:

TODO

private void saveProfilingTaskExecutionResult(ProfileResults results, Path directory) @ L122

  • 方法名:saveProfilingTaskExecutionResult
  • 源码定位:L122
  • 返回类型:void
  • 修饰符:private

参数:

  • results: ProfileResults
  • directory: Path

说明:

TODO

代码

public class MetricsPersister {
    public static final Path PROFILING_RESULTS_DIR = Paths.get("debug/profiling");
    public static final String METRICS_DIR_NAME = "metrics";
    public static final String DEVIATIONS_DIR_NAME = "deviations";
    public static final String PROFILING_RESULT_FILENAME = "profiling.txt";
    private static final Logger LOGGER = LogUtils.getLogger();
    private final String rootFolderName;
 
    public MetricsPersister(String rootFolderName) {
        this.rootFolderName = rootFolderName;
    }
 
    public Path saveReports(Set<MetricSampler> samplers, Map<MetricSampler, List<RecordedDeviation>> deviationsBySampler, ProfileResults profilerResults) {
        try {
            Files.createDirectories(PROFILING_RESULTS_DIR);
        } catch (IOException var8) {
            throw new UncheckedIOException(var8);
        }
 
        try {
            Path tempDir = Files.createTempDirectory("minecraft-profiling");
            tempDir.toFile().deleteOnExit();
            Files.createDirectories(PROFILING_RESULTS_DIR);
            Path workingDir = tempDir.resolve(this.rootFolderName);
            Path metricsDir = workingDir.resolve("metrics");
            this.saveMetrics(samplers, metricsDir);
            if (!deviationsBySampler.isEmpty()) {
                this.saveDeviations(deviationsBySampler, workingDir.resolve("deviations"));
            }
 
            this.saveProfilingTaskExecutionResult(profilerResults, workingDir);
            return tempDir;
        } catch (IOException var7) {
            throw new UncheckedIOException(var7);
        }
    }
 
    private void saveMetrics(Set<MetricSampler> samplers, Path dir) {
        if (samplers.isEmpty()) {
            throw new IllegalArgumentException("Expected at least one sampler to persist");
        } else {
            Map<MetricCategory, List<MetricSampler>> samplersByCategory = samplers.stream().collect(Collectors.groupingBy(MetricSampler::getCategory));
            samplersByCategory.forEach((category, samplersInCategory) -> this.saveCategory(category, (List<MetricSampler>)samplersInCategory, dir));
        }
    }
 
    private void saveCategory(MetricCategory category, List<MetricSampler> samplers, Path dir) {
        Path file = dir.resolve(Util.sanitizeName(category.getDescription(), Identifier::validPathChar) + ".csv");
        Writer writer = null;
 
        try {
            Files.createDirectories(file.getParent());
            writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8);
            CsvOutput.Builder csvBuilder = CsvOutput.builder();
            csvBuilder.addColumn("@tick");
 
            for (MetricSampler sampler : samplers) {
                csvBuilder.addColumn(sampler.getName());
            }
 
            CsvOutput csvOutput = csvBuilder.build(writer);
            List<MetricSampler.SamplerResult> results = samplers.stream().map(MetricSampler::result).collect(Collectors.toList());
            int firstTick = results.stream().mapToInt(MetricSampler.SamplerResult::getFirstTick).summaryStatistics().getMin();
            int lastTick = results.stream().mapToInt(MetricSampler.SamplerResult::getLastTick).summaryStatistics().getMax();
 
            for (int tick = firstTick; tick <= lastTick; tick++) {
                int finalTick = tick;
                Stream<String> valuesStream = results.stream().map(it -> String.valueOf(it.valueAtTick(finalTick)));
                Object[] row = Stream.concat(Stream.of(String.valueOf(tick)), valuesStream).toArray(String[]::new);
                csvOutput.writeRow(row);
            }
 
            LOGGER.info("Flushed metrics to {}", file);
        } catch (Exception var18) {
            LOGGER.error("Could not save profiler results to {}", file, var18);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
 
    private void saveDeviations(Map<MetricSampler, List<RecordedDeviation>> deviationsBySampler, Path directory) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss.SSS", Locale.UK).withZone(ZoneId.systemDefault());
        deviationsBySampler.forEach(
            (sampler, deviations) -> deviations.forEach(
                deviation -> {
                    String timestamp = formatter.format(deviation.timestamp);
                    Path deviationLogFile = directory.resolve(Util.sanitizeName(sampler.getName(), Identifier::validPathChar))
                        .resolve(String.format(Locale.ROOT, "%d@%s.txt", deviation.tick, timestamp));
                    deviation.profilerResultAtTick.saveResults(deviationLogFile);
                }
            )
        );
    }
 
    private void saveProfilingTaskExecutionResult(ProfileResults results, Path directory) {
        results.saveResults(directory.resolve("profiling.txt"));
    }
}

引用的其他类