1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-20 04:01:38 +03:00
vimr/NvimView/Support/DrawerPerf/PerfTester.swift

160 lines
4.2 KiB
Swift
Raw Normal View History

2020-08-19 01:09:13 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*
* 0.json from: https://github.com/gshslatexintro/An-Introduction-to-LaTeX
* 1.json from @telemachus
* 2.json from http://generator.lorem-ipsum.info
*/
import Cocoa
import GameKit
import os
class PerfTester {
init() {
self.cellSize = FontUtils.cellSize(of: self.font, linespacing: 1.25, characterspacing: 1)
for name in ["0", "1", "2"] {
guard let fileUrl = Bundle(for: PerfTester.self)
.url(forResource: name, withExtension: "json")
2020-09-18 23:51:51 +03:00
else {
2020-08-19 01:09:13 +03:00
preconditionFailure("Could not find \(name).json")
}
let decoder = JSONDecoder()
do {
let data = try Data(contentsOf: fileUrl)
self.ugrids.append(try decoder.decode(UGrid.self, from: data))
} catch {
preconditionFailure("Couldn't decode UGrid from \(name).json: \(error)")
}
}
self.initAttrs()
}
func render(_ index: Int) -> [[FontGlyphRun]] {
precondition((0...2).contains(index), "Wrong index!")
let ugrid = self.ugrids[index]
2020-09-18 23:51:51 +03:00
let runs = self.runs(
index,
forRowRange: 0...ugrid.size.height - 1,
columnRange: 0...ugrid.size.width - 1
)
2020-08-19 01:09:13 +03:00
return runs.parallelMap(chunkSize: 50) { run in
let font = FontUtils.font(
adding: run.attrs.fontTrait, to: self.font
)
return self.typesetter.fontGlyphRunsWithLigatures(
nvimUtf16Cells: run.cells.map { Array($0.string.utf16) },
startColumn: run.cells.startIndex,
offset: .zero,
font: font,
2020-09-18 23:51:51 +03:00
cellWidth: 20
)
2020-08-19 01:09:13 +03:00
}
}
private var ugrids = [UGrid]()
private let cellAttrsCollection = CellAttributesCollection()
private let typesetter = Typesetter()
private let font = NSFont.userFixedPitchFont(ofSize: 13)!
private let cellSize: CGSize
private func runs(
_ index: Int,
forRowRange rowRange: CountableClosedRange<Int>,
columnRange: CountableClosedRange<Int>
) -> [AttributesRun] {
2020-09-18 23:51:51 +03:00
precondition(index >= 0 && index <= 2, "Wrong index!")
2020-08-19 01:09:13 +03:00
let ugrid = self.ugrids[index]
return rowRange.map { row in
2020-09-18 23:51:51 +03:00
ugrid.cells[row][columnRange]
2021-12-23 22:54:49 +03:00
.groupedRanges(with: { cell in cell.attrId })
2020-09-18 23:51:51 +03:00
.compactMap { range in
let cells = ugrid.cells[row][range]
guard let firstCell = cells.first,
2020-12-11 09:48:39 +03:00
let attrs = self.cellAttrsCollection.attributes(
of: firstCell.attrId
)
2020-09-18 23:51:51 +03:00
else {
// GH-666: FIXME: correct error handling
self.log.error(
"row: \(row), range: \(range): " +
"Could not get CellAttributes with ID " +
"\(String(describing: cells.first?.attrId))"
)
return nil
2020-08-19 01:09:13 +03:00
}
2020-09-18 23:51:51 +03:00
return AttributesRun(
location: CGPoint.zero,
cells: ugrid.cells[row][range],
attrs: attrs
)
}
}
.flatMap { $0 }
2020-08-19 01:09:13 +03:00
}
private let fontTraitRd = GKRandomDistribution(
randomSource: randomSource,
lowestValue: 0,
highestValue: 6
)
private let intColorRd = GKRandomDistribution(
randomSource: randomSource,
lowestValue: 0,
2020-09-18 23:51:51 +03:00
highestValue: 16_777_215
2020-08-19 01:09:13 +03:00
)
private let attrsRunRd = GKRandomDistribution(
randomSource: randomSource,
lowestValue: 0,
highestValue: 10
)
2020-09-18 23:51:51 +03:00
private let log = OSLog(
subsystem: "com.qvacua.DrawerPerf",
category: "perf-tester"
)
2020-08-19 01:09:13 +03:00
private func initAttrs() {
2020-09-18 23:51:51 +03:00
for i in 1..<200 {
2020-08-19 01:09:13 +03:00
self.cellAttrsCollection.set(attributes: self.randomCellAttrs(), for: i)
}
}
private func randomCellAttrs() -> CellAttributes {
2020-09-18 23:51:51 +03:00
CellAttributes(
fontTrait: self.randomFontTrait(),
foreground: self.intColorRd.nextInt(),
background: self.intColorRd.nextInt(),
special: self.intColorRd.nextInt(),
reverse: false
)
2020-08-19 01:09:13 +03:00
}
private func randomFontTrait() -> FontTrait {
switch self.fontTraitRd.nextInt() {
case 0: return []
case 1: return [.italic]
case 2: return [.bold]
case 3: return [.underline]
case 4: return [.undercurl]
case 5: return [.italic, .bold]
case 6: return [.bold, .underline]
default: return []
}
}
}
2020-09-18 23:51:51 +03:00
private let randomSource = GKMersenneTwisterRandomSource(seed: 95_749_272_934)