Skip to content

Commit

Permalink
Add timing tests for escaping JSON strings
Browse files Browse the repository at this point in the history
On my machine, the escaped test case has a runtime of 74ms. The chosen range of up to 5000ms provides a wide range that precludes the pathological case while still being acheivable on underpowered hardware.

Tests should be converted to proper benchmarks in a later PR (see xyncro#70).

Also exposes internals to `Chiron.Tests`.
  • Loading branch information
neoeinstein committed Sep 20, 2016
1 parent 0797106 commit 269fc15
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Chiron/Chiron.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Chiron
module Chiron

open System
open System.Globalization
Expand Down Expand Up @@ -1130,3 +1130,6 @@ module Patterns =
Optic.get (Json.Object_ >?> Map.key_ key)
>> Option.bind (Json.tryDeserialize >> function | Choice1Of2 a -> Some a
| _ -> None)

[<assembly:System.Runtime.CompilerServices.InternalsVisibleTo("Chiron.Tests")>]
()
22 changes: 22 additions & 0 deletions tests/Chiron.Tests/Chiron.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ let ``Json.format returns correct values`` () =
Json.format (String "") =! "\"\""
Json.format (String "\t") =! "\"\\t\""

[<Fact>]
let ``escape is not pathological for cases with escapes`` () =
let testObj =
Json.Array
[ for i in 1..100000 do
yield Json.String <| "he\nllo\u0006\t" + string i ]
let testString = Json.format testObj
let sw = System.Diagnostics.Stopwatch.StartNew()
let escaped = Escaping.escape testString
let time = sw.ElapsedMilliseconds
printfn "String length: %i, JSON escaped length: %i, Time: %i ms" (String.length testString) (String.length escaped) time
Assert.InRange(time, 0L, 5000L)

[<Fact>]
let ``escape is not pathological for non-escaped cases`` () =
let testString = String.replicate 2500000 "a"
let sw = System.Diagnostics.Stopwatch.StartNew()
let escaped = Escaping.escape testString
let time = sw.ElapsedMilliseconds
printfn "String length: %i, JSON escaped length: %i, Time: %i ms" (String.length testString) (String.length escaped) time
Assert.InRange(time, 0L, 5000L)

(* Mapping
Tests exercising mapping functions between Json and other F#
Expand Down

0 comments on commit 269fc15

Please sign in to comment.