aboutsummaryrefslogtreecommitdiff
path: root/Benchmarks/ParseBenchmarkTarget/ParseBenchmarkTarget.swift
blob: c99814fcaad6efd4258f2887e6255c9d4844d934 (plain)
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
import Benchmark
import Foundation
import WmapParser

let benchmarks: @Sendable () -> Benchmark? = {
  // Load the wmap files from Documentation directory
  let fileManager = FileManager.default
  let currentDirectory = fileManager.currentDirectoryPath
  let documentationPath = (currentDirectory as NSString).appendingPathComponent("Documentation")

  let examplePath = (documentationPath as NSString).appendingPathComponent("example.wmap")
  let hugePath = (documentationPath as NSString).appendingPathComponent("huge.wmap")

  let exampleSource: String
  let hugeSource: String

  do {
    exampleSource = try String(contentsOfFile: examplePath, encoding: .utf8)
  } catch {
    print("Current directory: \(currentDirectory)")
    print("Failed to load example.wmap from: \(examplePath)")
    print("Error: \(error)")
    fatalError("Failed to load example.wmap")
  }

  do {
    hugeSource = try String(contentsOfFile: hugePath, encoding: .utf8)
  } catch {
    print("Failed to load huge.wmap from: \(hugePath)")
    print("Error: \(error)")
    fatalError("Failed to load huge.wmap")
  }

  Benchmark.defaultConfiguration.maxDuration = .seconds(3)
  Benchmark.defaultConfiguration.maxIterations = 500

  Benchmark("parse example") { benchmark in
    for _ in benchmark.scaledIterations {
      blackHole(parse(exampleSource))
    }
  }

  return Benchmark("parse huge") { benchmark in
    for _ in benchmark.scaledIterations {
      blackHole(parse(hugeSource))
    }
  }
}