<commit_before>// Copyright (c) 2022 <NAME>
// Licensed under MIT License
//
// See LICENSE for license information

#if os(Linux)
    import CoreFoundation
#endif

import Foundation
import SwiftCLI

final class ShowBenchmarkCommand: Command {

    let name = "show"
    let shortDescription = "Print saved benchmarks results"

    @Key("-c", "--compare", description: "Compare with other saved benchmarks results")
    var comparePath: String?

    @Param var path: String

    func execute() throws {
        let newSaveFile = try SaveFile.load(from: self.path)
        var newMetadatas = Dictionary(uniqueKeysWithValues: zip(newSaveFile.metadatas.keys, (1...newSaveFile.metadatas.count).map { "(\($0))" }))
        if newMetadatas.count == 1 {
            newMetadatas[newMetadatas.first!.key] = ""
        }
        for (metadataUUID, index) in newMetadatas.sorted(by: { $0.value < $1.value }) {
            print("NEW\(index) Metadata")
            print("---------------")
            newSaveFile.metadatas[metadataUUID]!.print()
        }

        var newResults = [String: [(BenchmarkResult, UUID)]]()
        for newRun in newSaveFile.runs {
            newResults.merge(Dictionary(grouping: newRun.results.map { ($0, newRun.metadataUUID) }, by: { $0.0.id }),
                                  uniquingKeysWith: { $0 + $1 })
        }

        var baseResults = [String: [(BenchmarkResult, UUID)]]()
        var baseMetadatas = [UUID: String]()
        if let comparePath = comparePath {
            let baseSaveFile = try SaveFile.load(from: comparePath)

            baseMetadatas = Dictionary(uniqueKeysWithValues: zip(baseSaveFile.metadatas.keys, (1...baseSaveFile.metadatas.count).map { "(\($0))" }))
            if baseMetadatas.count == 1 {
                baseMetadatas[baseMetadatas.first!.key] = ""
            }
            for (metadataUUID, index) in baseMetadatas.sorted(by: { $0.value < $1.value }) {
                print("BASE\(index) Metadata")
                print("----------------")
                baseSaveFile.metadatas[metadataUUID]!.print()
            }

            for baseRun in baseSaveFile.runs {
                baseResults.merge(Dictionary(grouping: baseRun.results.map { ($0, baseRun.metadataUUID) }, by: { $0.0.id }),
                                  uniquingKeysWith: { $0 + $1 })
            }
        }

        for resultId in newResults.keys.sorted() {
            let results = newResults[resultId]!
            for (result, metadataUUID) in results {
                let benchmark = Benchmarks(rawValue: result.name)?.initialized(result.input)

                print("\(result.name) => \(result.input), iterations = \(result.iterCount)")

                if let baseResults = baseResults[resultId] {
                    print("NEW\(newMetadatas[metadataUUID]!):  average = \(benchmark.format(result.avg)), standard deviation = \(benchmark.format(result.std))")
                    for (other, baseUUID) in baseResults {
                        print("BASE\(baseMetadatas[baseUUID]!): average = \(benchmark.format(other.avg)), standard deviation = \(benchmark.format(other.std))")
                        result.printComparison(with: other)
                    }
                } else {
                    print("NEW\(newMetadatas[metadataUUID]!): average = \(benchmark.format(result.avg)), standard deviation = \(benchmark.format(result.std))")
                }

                print()
            }
        }
    }

}

fileprivate extension Optional where Wrapped == Benchmark {

    func format(_ value: Double) -> String {
        switch self {
        case .some(let benchmark):
            return benchmark.format(value)
        case .none:
            return String(value)
        }
    }

}<commit_msg>[swcomp] Optimize printing in benchmark show<commit_after>// Copyright (c) 2022 <NAME>
// Licensed under MIT License
//
// See LICENSE for license information

#if os(Linux)
    import CoreFoundation
#endif

import Foundation
import SwiftCLI

final class ShowBenchmarkCommand: Command {

    let name = "show"
    let shortDescription = "Print saved benchmarks results"

    @Key("-c", "--compare", description: "Compare with other saved benchmarks results")
    var comparePath: String?

    @Param var path: String

    func execute() throws {
        let newSaveFile = try SaveFile.load(from: self.path)
        var newMetadatas = Dictionary(uniqueKeysWithValues: zip(newSaveFile.metadatas.keys, (1...newSaveFile.metadatas.count).map { "(\($0))" }))
        if newMetadatas.count == 1 {
            newMetadatas[newMetadatas.first!.key] = ""
        }
        for (metadataUUID, index) in newMetadatas.sorted(by: { $0.value < $1.value }) {
            print("NEW\(index) Metadata")
            print("---------------")
            newSaveFile.metadatas[metadataUUID]!.print()
        }

        var newResults = [String: [(BenchmarkResult, UUID)]]()
        for newRun in newSaveFile.runs {
            newResults.merge(Dictionary(grouping: newRun.results.map { ($0, newRun.metadataUUID) }, by: { $0.0.id }),
                                  uniquingKeysWith: { $0 + $1 })
        }

        var baseResults = [String: [(BenchmarkResult, UUID)]]()
        var baseMetadatas = [UUID: String]()
        if let comparePath = comparePath {
            let baseSaveFile = try SaveFile.load(from: comparePath)

            baseMetadatas = Dictionary(uniqueKeysWithValues: zip(baseSaveFile.metadatas.keys, (1...baseSaveFile.metadatas.count).map { "(\($0))" }))
            if baseMetadatas.count == 1 {
                baseMetadatas[baseMetadatas.first!.key] = ""
            }
            for (metadataUUID, index) in baseMetadatas.sorted(by: { $0.value < $1.value }) {
                print("BASE\(index) Metadata")
                print("----------------")
                baseSaveFile.metadatas[metadataUUID]!.print()
            }

            for baseRun in baseSaveFile.runs {
                baseResults.merge(Dictionary(grouping: baseRun.results.map { ($0, baseRun.metadataUUID) }, by: { $0.0.id }),
                                  uniquingKeysWith: { $0 + $1 })
            }
        }

        for resultId in newResults.keys.sorted() {
            let results = newResults[resultId]!
            for (result, metadataUUID) in results {
                let benchmark = Benchmarks(rawValue: result.name)?.initialized(result.input)

                print("\(result.name) => \(result.input), iterations = \(result.iterCount)")

                print("NEW\(newMetadatas[metadataUUID]!):  average = \(benchmark.format(result.avg)), standard deviation = \(benchmark.format(result.std))")
                if let baseResults = baseResults[resultId] {
                    for (other, baseUUID) in baseResults {
                        print("BASE\(baseMetadatas[baseUUID]!): average = \(benchmark.format(other.avg)), standard deviation = \(benchmark.format(other.std))")
                        result.printComparison(with: other)
                    }
                }

                print()
            }
        }
    }

}

fileprivate extension Optional where Wrapped == Benchmark {

    func format(_ value: Double) -> String {
        switch self {
        case .some(let benchmark):
            return benchmark.format(value)
        case .none:
            return String(value)
        }
    }

}
{'fraction_non_alphanumeric': 0.13371304583672364, 'fraction_numerical': 0.006509357200976403, 'mean_word_length': 2.027504105090312, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7315503', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2045, 'n_words': 444}