diff --git a/dev/stackdiff/Sources/stackdiff/Stackdiff+Dump.swift b/dev/stackdiff/Sources/stackdiff/Stackdiff+Dump.swift new file mode 100644 index 000000000..485d24b3f --- /dev/null +++ b/dev/stackdiff/Sources/stackdiff/Stackdiff+Dump.swift @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import ArgumentParser + +struct Dump: ParsableCommand { + @Argument + var file: String + + @OptionGroup + var options: StackdiffOptions + + func run() throws { + let aggregate = try self.options.load(file: self.file) + let allocs = aggregate.netAllocations + + // Sort by allocations. + let sortedStacks = aggregate.sorted { + $0.value.netAllocations > $1.value.netAllocations + } + + for (stack, weightedSubstacks) in sortedStacks { + for line in StackFormatter.lines( + forStack: stack, + weightedSubstacks: weightedSubstacks, + substackPrefix: " " + ) { + print(line) + } + print("") + } + + print("TOTAL ALLOCATIONS: \(allocs)") + } +} diff --git a/dev/stackdiff/Sources/stackdiff/Stackdiff.swift b/dev/stackdiff/Sources/stackdiff/Stackdiff.swift index 052eeb767..484253331 100644 --- a/dev/stackdiff/Sources/stackdiff/Stackdiff.swift +++ b/dev/stackdiff/Sources/stackdiff/Stackdiff.swift @@ -19,7 +19,7 @@ import Stacks @main struct Stackdiff: ParsableCommand { static var configuration: CommandConfiguration { - CommandConfiguration(subcommands: [Diff.self]) + CommandConfiguration(subcommands: [Diff.self, Dump.self]) } }