2015-10-08 02:32:02 +03:00
|
|
|
import Cocoa
|
2015-10-14 21:17:56 +03:00
|
|
|
import Doubt
|
2015-10-16 00:40:57 +03:00
|
|
|
import Either
|
2015-10-14 21:17:56 +03:00
|
|
|
import Prelude
|
2015-10-16 00:40:57 +03:00
|
|
|
import Madness
|
2015-10-08 02:20:40 +03:00
|
|
|
|
2015-10-20 20:14:03 +03:00
|
|
|
func benchmark<T>(label: String? = nil, _ f: () -> T) -> T {
|
|
|
|
let start = NSDate.timeIntervalSinceReferenceDate()
|
|
|
|
let result = f()
|
|
|
|
let end = NSDate.timeIntervalSinceReferenceDate()
|
|
|
|
print((label.map { "\($0): " } ?? "") + "\(end - start)s")
|
|
|
|
return result
|
|
|
|
}
|
2015-10-08 02:43:22 +03:00
|
|
|
|
|
|
|
let arguments = BoundsCheckedArray(array: Process.arguments)
|
2015-10-16 00:40:57 +03:00
|
|
|
|
2015-10-20 18:12:30 +03:00
|
|
|
let empty = "{}\n"
|
2015-10-16 21:43:23 +03:00
|
|
|
print(parse(json, input: empty))
|
2015-10-16 20:08:49 +03:00
|
|
|
let dict = "{\"hello\":\"world\"}"
|
|
|
|
print(parse(json, input: dict))
|
|
|
|
let dictWithSpaces = "{ \"hello\" : \"world\" }"
|
|
|
|
print(parse(json, input: dictWithSpaces))
|
|
|
|
|
2015-10-16 21:43:23 +03:00
|
|
|
let dictWithMembers = "{\"hello\":\"world\",\"sup\":\"cat\"}"
|
|
|
|
print(parse(json, input: dictWithMembers))
|
|
|
|
|
2015-10-16 21:52:03 +03:00
|
|
|
let dictWithArray = "{\"hello\": [\"world\"],\"sup\": [\"cat\", \"dog\", \"keith\"] }"
|
|
|
|
print(parse(json, input: dictWithArray))
|
|
|
|
|
2015-10-21 00:46:07 +03:00
|
|
|
func diffAndSerialize(a aString: String, b bString: String, to: String) throws {
|
2015-10-20 20:14:14 +03:00
|
|
|
let aParsed = benchmark("parsing a") { curry(parse)(json)(aString) }
|
2015-10-20 19:09:03 +03:00
|
|
|
guard let a = aParsed.right else {
|
2015-10-20 19:10:04 +03:00
|
|
|
_ = aParsed.left.map { print("error parsing a:", $0) }
|
2015-10-21 00:46:07 +03:00
|
|
|
return
|
2015-10-20 19:09:03 +03:00
|
|
|
}
|
2015-10-20 20:14:14 +03:00
|
|
|
let bParsed = benchmark("parsing b") { curry(parse)(json)(bString) }
|
2015-10-20 19:09:03 +03:00
|
|
|
guard let b = bParsed.right else {
|
2015-10-20 19:10:04 +03:00
|
|
|
_ = bParsed.left.map { print("error parsing b:", $0) }
|
2015-10-21 00:46:07 +03:00
|
|
|
return
|
2015-10-20 19:09:03 +03:00
|
|
|
}
|
2015-10-19 22:15:38 +03:00
|
|
|
|
2015-10-20 20:14:19 +03:00
|
|
|
let diff = benchmark("diffing a & b") {
|
|
|
|
Interpreter<CofreeJSON>(equal: CofreeJSON.equals(annotation: const(true), leaf: ==), comparable: const(true), cost: Free.sum(Patch.difference)).run(a, b)
|
|
|
|
}
|
2015-10-19 22:15:38 +03:00
|
|
|
|
2015-10-20 23:27:07 +03:00
|
|
|
let range: Range<String.Index> -> Doubt.JSON = {
|
|
|
|
let start = Int(String($0.startIndex))!
|
|
|
|
let end = Int(String($0.endIndex))!
|
|
|
|
return [
|
|
|
|
.Number(Double(start)),
|
|
|
|
.Number(Double(end - start)),
|
|
|
|
]
|
|
|
|
}
|
2015-10-21 01:03:04 +03:00
|
|
|
let JSON: Doubt.JSON = benchmark("converting diff to JSON") {
|
|
|
|
[
|
|
|
|
"a": .String(aString),
|
|
|
|
"b": .String(bString),
|
|
|
|
"diff": diff.JSON(ifPure: { $0.JSON { $0.JSON(annotation: range, leaf: { $0.JSON }) } }, ifLeaf: { $0.JSON }),
|
|
|
|
]
|
2015-10-20 22:48:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let data = benchmark("serializing JSON to NSData") {
|
|
|
|
JSON.serialize()
|
|
|
|
}
|
|
|
|
|
2015-10-21 00:46:07 +03:00
|
|
|
try data.writeToFile(to, options: .DataWritingAtomic)
|
|
|
|
|
2015-10-20 22:48:08 +03:00
|
|
|
return benchmark("decoding data into string") {
|
|
|
|
NSString(data: data, encoding: NSUTF8StringEncoding) as String?
|
2015-10-20 20:15:55 +03:00
|
|
|
}
|
2015-10-19 22:15:38 +03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:12:30 +03:00
|
|
|
let readFile = { (path: String) -> String? in
|
|
|
|
guard let data = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) else { return nil }
|
|
|
|
return data as String?
|
|
|
|
}
|
|
|
|
|
2015-10-21 00:46:07 +03:00
|
|
|
if let a = arguments[1].flatMap(readFile), b = arguments[2].flatMap(readFile), c = arguments[3] {
|
|
|
|
try diffAndSerialize(a: a, b: b, to: c)
|
2015-10-16 22:41:35 +03:00
|
|
|
}
|