SPB Git

Repository Public MIT

PerformanceTests.swift 5.7 KB 88 lines swift

1// ORIX Calc2// File: Packages/ORIXGraph/Tests/ORIXGraphTests/PerformanceTests.swift3// Description: Real wall-clock measurements (ContinuousClock, best of 7) of sampling, implicit plotting, meshing and analysis; prints the numbers quoted in VALIDATION.md.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXExpression10@testable import ORIXGraph1112/// Limits are generous in debug builds (unoptimised closures, other suites running in parallel) and tight in release builds.13private func limit(release: Double, debug: Double) -> Double { buildConfiguration == "release" ? release : debug }1415@Suite(.serialized) struct Performance {16    @Test func explicitSamplingAt1200x800() {17        let view = Viewport(xMin: -10, xMax: 10, yMin: -6.6667, yMax: 6.6667, width: 1200, height: 800)18        for expression in ["sin(x)", "x^2", "x^3 - 3x", "1/x", "tan(x)", "exp(-x^2)", "sqrt(x)", "floor(x)", "sin(x)/x", "sin(100x)", "sin(1000x)"] {19            let f = fn(expression)20            var p = ExplicitPlot()21            let ms = bestMilliseconds { p = Sampler.sampleExplicit(f, viewport: view) }22            print("PERF|\(buildConfiguration)|explicit 1200x800|\(expression)|\(String(format: "%.3f", ms)) ms|\(p.pointCount) points|\(p.evaluations) evaluations|\(p.polylines.count) polylines|\(p.bands.reduce(0) { $0 + $1.columns.count }) band columns")23            #expect(ms < limit(release: 12, debug: 120), "\(expression): \(ms) ms")24        }25    }2627    @Test func implicitAt400x400() {28        let view = Viewport(xMin: -10, xMax: 10, yMin: -10, yMax: 10, width: 400, height: 400)29        for expression in ["x^2 + y^2 - 25", "sin(x) - cos(y)", "x*y - 1", "tan(x) - y"] {30            let f = fn2(expression)31            var p = ImplicitPlot()32            let ms = bestMilliseconds { p = ImplicitPlotter.plot(f, viewport: view) }33            print("PERF|\(buildConfiguration)|implicit 400x400|\(expression) = 0|\(String(format: "%.3f", ms)) ms|\(p.pointCount) points|\(p.evaluations) evaluations|\(p.polylines.count) polylines")34            // the stated target: under 50 ms (release build); debug builds get more room35            #expect(ms < limit(release: 50, debug: 400), "\(expression): \(ms) ms")36        }37    }3839    @Test func curvesMeshesAndAnalysis() throws {40        let view = Viewport(xMin: -3, xMax: 3, yMin: -2, yMax: 2, width: 1200, height: 800)41        var curve = CurvePlot()42        var ms = bestMilliseconds { curve = Sampler.sampleParametric(x: { sin(3 * $0) }, y: { cos(2 * $0) }, range: 0...(2 * .pi), viewport: view) }43        print("PERF|\(buildConfiguration)|parametric 1200x800|(sin 3t, cos 2t)|\(String(format: "%.3f", ms)) ms|\(curve.pointCount) points|\(curve.evaluations) evaluations")44        #expect(ms < limit(release: 10, debug: 100))4546        let surface = fn2("sin(x) * cos(y)")47        var mesh = SurfacePlotter.mesh(surface, x: -3...3, y: -3...3)48        ms = bestMilliseconds { mesh = SurfacePlotter.mesh(surface, x: -3...3, y: -3...3) }49        print("PERF|\(buildConfiguration)|surface 60x60 precise normals|sin(x) cos(y)|\(String(format: "%.3f", ms)) ms|\(mesh.vertices.count) vertices|\(mesh.triangleCount) triangles")50        #expect(ms < limit(release: 20, debug: 200))51        ms = bestMilliseconds { mesh = SurfacePlotter.mesh(surface, x: -3...3, y: -3...3, options: SurfaceOptions(preciseNormals: false)) }52        print("PERF|\(buildConfiguration)|surface 60x60 grid normals|sin(x) cos(y)|\(String(format: "%.3f", ms)) ms|\(mesh.vertices.count) vertices|\(mesh.triangleCount) triangles")53        let unit = mesh.normalized()54        var count = 055        ms = bestMilliseconds { count = PainterRenderer.project(unit, camera: Camera3D(), width: 1200, height: 800).count }56        print("PERF|\(buildConfiguration)|painter projection + sort|7200 triangles|\(String(format: "%.3f", ms)) ms|\(count) triangles")57        #expect(ms < limit(release: 20, debug: 200))5859        let f = fn("x^3 - 3x")60        var n = 061        ms = bestMilliseconds { n = GraphAnalysis.roots(of: f, in: -10...10).count + GraphAnalysis.extrema(of: f, in: -10...10).count }62        print("PERF|\(buildConfiguration)|roots + extrema on [-10, 10]|x^3 - 3x|\(String(format: "%.3f", ms)) ms|\(n) points of interest")63        #expect(n == 5 && ms < limit(release: 20, debug: 200))64        let s = fn("sin(x)")65        var value = 0.066        ms = bestMilliseconds { value = (try? GraphAnalysis.integral(of: s, from: 0, to: 2 * .pi, viewport: view).value) ?? .nan }67        print("PERF|\(buildConfiguration)|integral with shaded regions|sin(x) on [0, 2π]|\(String(format: "%.3f", ms)) ms")68        #expect(abs(value) < 1e-12 && ms < limit(release: 30, debug: 300))6970        let item = try PlotParser.parse("a sin(b x) + c")71        var context = EvalContext()72        context.variables = ["a": Value(2.0), "b": Value(3.0), "c": Value(0.5)]73        var points = 074        ms = bestMilliseconds { points = ((try? GraphEngine.geometry(for: item, viewport: view, context: context))?.pointCount) ?? 0 }75        print("PERF|\(buildConfiguration)|slider update: compile + sample 1200x800|a sin(b x) + c|\(String(format: "%.3f", ms)) ms|\(points) points")76        #expect(points > 1000 && ms < limit(release: 12, debug: 120))77    }7879    @Test func typicalPointCountsAt800x600() {80        for expression in ["sin(x)", "x^2", "x^3 - 3x", "exp(x)", "exp(-x^2)", "1/x", "tan(x)", "sqrt(x)", "ln(x)", "floor(x)", "sin(x)/x", "sin(100x)"] {81            let p = plot(expression)82            let worst = maxScreenError(p, fn(expression), .standard)83            print("COUNT|800x600|\(expression)|\(p.pointCount) points|\(p.evaluations) evaluations|\(p.polylines.count) polylines|max error \(String(format: "%.3f", worst)) px")84            #expect(p.pointCount <= 16000 && worst < 0.5)85        }86    }87}88
Files main
Menu

Theme