summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel.Tests/PerformanceTests.fs
blob: c7e65e4c618c2af5c77011b42a221da0c608d2b7 (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
49
50
51
namespace LivingVillage.Kernel.Tests

open System
open System.Diagnostics
open Microsoft.VisualStudio.TestTools.UnitTesting
open LivingVillage.Headless

[<TestClass>]
type PerformanceTests () =

    let testConfiguration =
        { PerformanceProbe.defaultConfiguration with
            WarmupTicks = 32
            MeasureTicks = 256
            Repetitions = 1 }

    [<TestMethod>]
    member _.FixedSeedAndInputSequenceProduceTheSameWorldDigest () =
        let first = PerformanceProbe.runSteps testConfiguration
        let second = PerformanceProbe.runSteps testConfiguration

        Assert.AreEqual<int64>(
            int64 (testConfiguration.WarmupTicks + testConfiguration.MeasureTicks),
            first.Tick)
        Assert.AreEqual<int>(testConfiguration.NpcCount, first.Npcs.Length)
        Assert.AreEqual<string>(PerformanceProbe.worldDigest first, PerformanceProbe.worldDigest second)

    [<TestMethod>]
    member _.MeasurementReportsConfiguredTicksAndRuntimeCounters () =
        let sample = PerformanceProbe.measure testConfiguration

        Assert.AreEqual<int>(testConfiguration.MeasureTicks, sample.MeasureTicks)
        Assert.AreEqual<int64>(int64 (testConfiguration.WarmupTicks + testConfiguration.MeasureTicks), sample.FinalTick)
        Assert.AreEqual<int64>(Stopwatch.Frequency, sample.StopwatchFrequency)
        Assert.IsTrue(sample.ElapsedStopwatchTicks > 0L)
        Assert.IsTrue(sample.AllocatedBytes >= 0L)
        Assert.IsTrue(sample.Gen0Collections >= 0L)
        Assert.IsTrue(sample.Gen1Collections >= 0L)
        Assert.IsTrue(sample.Gen2Collections >= 0L)
        let formatted = PerformanceProbe.formatSample sample
        Assert.IsTrue(formatted.Contains("ticks_per_second="))
        Assert.IsTrue(formatted.Contains("allocated_bytes="))
        Assert.IsTrue(formatted.Contains("gen0="))

    [<TestMethod>]
    member _.InvalidConfigurationReportsAUsefulFailure () =
        let invalid = { testConfiguration with NpcCount = 0 }

        match PerformanceProbe.validateConfiguration invalid with
        | Ok () -> Assert.Fail("zero NPC configuration must be rejected")
        | Error message -> Assert.IsTrue(message.Contains("NpcCount"))