-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathPerformanceTestUtils.swift
69 lines (59 loc) · 2.06 KB
/
PerformanceTestUtils.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// PerformanceTest.swift
// Benchmark
//
// Created by Wojciech Czekalski on 27.04.2016.
// Copyright © 2016 wczekalski. All rights reserved.
//
import Foundation
import Diff
func performDiff(fromFilePath: String, toFilePath: String, repeatCount: Int = 10, diffFunc: @escaping ([Character], [Character]) -> Void) -> (created: String, deleted: String, same: String, changed: String) {
let old = file(fromFilePath)
let new = file(toFilePath)
let compare: ([Character], [Character]) -> String = { a, b in
var time: CFTimeInterval = 0
for _ in 0 ..< repeatCount {
time += measure({ diffFunc(a, b) })
}
time = time / CFTimeInterval(repeatCount)
return (NSString(format: "%.4f", time) as String)
}
return (compare([], old),
compare(old, []),
compare(old, old),
compare(old, new))
}
func currentDirectoryPath() -> String {
let buffer: UnsafeMutablePointer<Int8> = UnsafeMutablePointer.allocate(capacity: Int(PATH_MAX))
getcwd(buffer, Int(PATH_MAX))
let string = String(cString: buffer)
buffer.deallocate(capacity: Int(PATH_MAX))
return string
}
func measure(_ f: () -> Void) -> CFTimeInterval {
let time = CFAbsoluteTimeGetCurrent()
f()
return CFAbsoluteTimeGetCurrent() - time
}
func file(_ path: String) -> [Character] {
return try! Array(String(contentsOfFile: path).characters)
}
func diffSwift(_ a: [Character], b: [Character]) {
_ = _diffSwift(a, b: b)
}
private func _diffSwift(_ a: [Character], b: [Character]) -> Diff {
return a.diff(b)
}
func launchPath() -> String {
let path = CommandLine.arguments.first!
let dotIndex = path.index(after: path.startIndex)
var lastSlashIndex = path.index(before: path.endIndex)
let c: Character = "/"
while path.characters[lastSlashIndex] != c {
lastSlashIndex = path.index(before: lastSlashIndex)
}
return path.substring(to: lastSlashIndex).substring(from: dotIndex)
}
func proccessPath() -> String {
return currentDirectoryPath().appending(launchPath())
}