From 2d9daa17ec5a4cffcdf6222bf626bf81fcf53cdb Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Tue, 1 May 2018 12:58:56 +0300 Subject: [PATCH] [swcomp] Rewrite benchmark commands using new BenchmarkCommand protocol --- Sources/swcomp/Benchmarks/CompBz2.swift | 42 ++------------------- Sources/swcomp/Benchmarks/CompDeflate.swift | 42 ++------------------- Sources/swcomp/Benchmarks/Info7z.swift | 42 ++------------------- Sources/swcomp/Benchmarks/InfoTar.swift | 42 ++------------------- Sources/swcomp/Benchmarks/InfoZip.swift | 42 ++------------------- Sources/swcomp/Benchmarks/UnBz2.swift | 42 ++------------------- Sources/swcomp/Benchmarks/UnGzip.swift | 42 ++------------------- Sources/swcomp/Benchmarks/UnXz.swift | 42 ++------------------- 8 files changed, 24 insertions(+), 312 deletions(-) diff --git a/Sources/swcomp/Benchmarks/CompBz2.swift b/Sources/swcomp/Benchmarks/CompBz2.swift index 8d4667ec..2c8655a4 100644 --- a/Sources/swcomp/Benchmarks/CompBz2.swift +++ b/Sources/swcomp/Benchmarks/CompBz2.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class CompBz2: Command { +class CompBz2: BenchmarkCommand { let name = "comp-bz2" let shortDescription = "Performs benchmarking of BZip2 compression using specified files" let files = CollectedParameter() - func execute() throws { - print("BZip2 Compression Benchmark") - print("===========================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = BZip2.compress(data: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = BZip2.compress(data: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("-----------------------------------") - } - } + let benchmarkName = "BZip2 Compression" + let benchmarkFunction: (Data) throws -> Any = BZip2.compress } diff --git a/Sources/swcomp/Benchmarks/CompDeflate.swift b/Sources/swcomp/Benchmarks/CompDeflate.swift index 5fba6ad4..011e578b 100644 --- a/Sources/swcomp/Benchmarks/CompDeflate.swift +++ b/Sources/swcomp/Benchmarks/CompDeflate.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class CompDeflate: Command { +class CompDeflate: BenchmarkCommand { let name = "comp-deflate" let shortDescription = "Performs benchmarking of Deflate compression using specified files" let files = CollectedParameter() - func execute() throws { - print("Deflate Compression Benchmark") - print("=============================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = Deflate.compress(data: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = Deflate.compress(data: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("----------------------------------") - } - } + let benchmarkName = "Deflate Compression" + let benchmarkFunction: (Data) throws -> Any = Deflate.compress } diff --git a/Sources/swcomp/Benchmarks/Info7z.swift b/Sources/swcomp/Benchmarks/Info7z.swift index a315926d..818d1c00 100644 --- a/Sources/swcomp/Benchmarks/Info7z.swift +++ b/Sources/swcomp/Benchmarks/Info7z.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class Info7z: Command { +class Info7z: BenchmarkCommand { let name = "info-7z" let shortDescription = "Performs benchmarking of 7-Zip info function using specified files" let files = CollectedParameter() - func execute() throws { - print("7-Zip info function Benchmark") - print("=============================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try SevenZipContainer.info(container: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try SevenZipContainer.info(container: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("---------------------------------------") - } - } + let benchmarkName = "7-Zip info function" + let benchmarkFunction: (Data) throws -> Any = SevenZipContainer.info } diff --git a/Sources/swcomp/Benchmarks/InfoTar.swift b/Sources/swcomp/Benchmarks/InfoTar.swift index 89852bf9..809378c2 100644 --- a/Sources/swcomp/Benchmarks/InfoTar.swift +++ b/Sources/swcomp/Benchmarks/InfoTar.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class InfoTar: Command { +class InfoTar: BenchmarkCommand { let name = "info-tar" let shortDescription = "Performs benchmarking of TAR info function using specified files" let files = CollectedParameter() - func execute() throws { - print("TAR info function Benchmark") - print("===========================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try TarContainer.info(container: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try TarContainer.info(container: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("-------------------------------------") - } - } + let benchmarkName = "TAR info function" + let benchmarkFunction: (Data) throws -> Any = TarContainer.info } diff --git a/Sources/swcomp/Benchmarks/InfoZip.swift b/Sources/swcomp/Benchmarks/InfoZip.swift index 53a12eb8..0b0fab59 100644 --- a/Sources/swcomp/Benchmarks/InfoZip.swift +++ b/Sources/swcomp/Benchmarks/InfoZip.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class InfoZip: Command { +class InfoZip: BenchmarkCommand { let name = "info-zip" let shortDescription = "Performs benchmarking of ZIP info function using specified files" let files = CollectedParameter() - func execute() throws { - print("ZIP info function Benchmark") - print("===========================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try ZipContainer.info(container: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try ZipContainer.info(container: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("-------------------------------------") - } - } + let benchmarkName = "ZIP info function" + let benchmarkFunction: (Data) throws -> Any = ZipContainer.info } diff --git a/Sources/swcomp/Benchmarks/UnBz2.swift b/Sources/swcomp/Benchmarks/UnBz2.swift index 3a4c8788..583388d3 100644 --- a/Sources/swcomp/Benchmarks/UnBz2.swift +++ b/Sources/swcomp/Benchmarks/UnBz2.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class UnBz2: Command { +class UnBz2: BenchmarkCommand { let name = "un-bz2" let shortDescription = "Performs benchmarking of BZip2 unarchiving using specified files" let files = CollectedParameter() - func execute() throws { - print("BZip2 Unarchive Benchmark") - print("=========================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try BZip2.decompress(data: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try BZip2.decompress(data: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("-----------------------------------") - } - } + let benchmarkName = "BZip2 Unarchive" + let benchmarkFunction: (Data) throws -> Any = BZip2.decompress } diff --git a/Sources/swcomp/Benchmarks/UnGzip.swift b/Sources/swcomp/Benchmarks/UnGzip.swift index 8639aa06..5c5cbd8c 100644 --- a/Sources/swcomp/Benchmarks/UnGzip.swift +++ b/Sources/swcomp/Benchmarks/UnGzip.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class UnGzip: Command { +class UnGzip: BenchmarkCommand { let name = "un-gzip" let shortDescription = "Performs benchmarking of GZip unarchiving using specified files" let files = CollectedParameter() - func execute() throws { - print("GZip Unarchive Benchmark") - print("========================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try GzipArchive.unarchive(archive: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try GzipArchive.unarchive(archive: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("----------------------------------") - } - } + let benchmarkName = "GZip Unarchive" + let benchmarkFunction: (Data) throws -> Any = GzipArchive.unarchive } diff --git a/Sources/swcomp/Benchmarks/UnXz.swift b/Sources/swcomp/Benchmarks/UnXz.swift index 5b797f6d..7f79f505 100644 --- a/Sources/swcomp/Benchmarks/UnXz.swift +++ b/Sources/swcomp/Benchmarks/UnXz.swift @@ -7,50 +7,14 @@ import Foundation import SWCompression import SwiftCLI -#if os(Linux) - import CoreFoundation -#endif - -class UnXz: Command { +class UnXz: BenchmarkCommand { let name = "un-xz" let shortDescription = "Performs benchmarking of XZ unarchiving using specified files" let files = CollectedParameter() - func execute() throws { - print("XZ Unarchive Benchmark") - print("======================") - - for file in self.files.value { - print("File: \(file)\n") - - let inputURL = URL(fileURLWithPath: file) - let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) - - var totalTime: Double = 0 - - var maxTime = Double(Int.min) - var minTime = Double(Int.max) - - _ = try XZArchive.unarchive(archive: fileData) - for i in 1...10 { - print("Iteration \(i): ", terminator: "") - let startTime = CFAbsoluteTimeGetCurrent() - _ = try XZArchive.unarchive(archive: fileData) - let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime - print(String(format: "%.3f", timeElapsed)) - totalTime += timeElapsed - if timeElapsed > maxTime { - maxTime = timeElapsed - } - if timeElapsed < minTime { - minTime = timeElapsed - } - } - print(String(format: "\nAverage time: %.3f \u{B1} %.3f", totalTime / 10, (maxTime - minTime) / 2)) - print("--------------------------------") - } - } + let benchmarkName = "XZ Unarchive" + let benchmarkFunction: (Data) throws -> Any = XZArchive.unarchive }