【问题标题】:Speed up FsCheck Arbitrary generation加速 FsCheck 任意生成
【发布时间】:2016-05-22 06:28:46
【问题描述】:

我正在编写一些生成器和一个 Arbitrary,但速度太慢(另请参阅 GC 编号)。我想我的代码有错误,但我不知道在哪里。或者我的方法 (map2 (fold)) 很“奇怪”?

发电机:

type Generators () =

    static let notAllowed = Array.append [| '0'..'9' |] [| '\n'; '\r'; '['; ']'; '/'; |] 

    static let containsInvalidValues (s : string)  = s.IndexOfAny(notAllowed) <> -1

    static member positiveIntsGen() = Arb.generate<PositiveInt> |> Gen.map int

    static member separatorStringGen() =                         
        Arb.generate<NonEmptyString> 
        |> Gen.suchThat (fun s -> s.Get.Length < 5 && not (s.Get |> containsInvalidValues))

任意:

let manyNumbersNewLineCustomDelimiterStrInput = 
    Gen.map2 (fun (ints : int[]) (nes : NonEmptyString) ->             
        Array.fold (fun acc num ->                 
            if num % 2 = 0 then acc + "," + num.ToString()
            else if num % 3 = 0 then acc + "\n" + num.ToString()
            else acc + "\n" + num.ToString()) ("//[" + nes.Get + "]\n") ints ) 
        (Generators.array12OfIntsGen()) 
        (Generators.separatorStringGen())
    |> Arb.fromGen

配置有MaxTest = 500,大约需要5分钟才能完成。

输出(使用#timer):

StrCalcTest.get_When pass an string that starts with "//[" and contains "]\n" use the multicharacter value between them as separator-Ok, passed 500 tests.

Real: 00:07:03.467, CPU: 00:07:03.296, GC gen0: 75844, gen1: 71968, gen2: 4

【问题讨论】:

    标签: f# fscheck


    【解决方案1】:

    没有实际测试任何东西,我的猜测是有问题的部分是这样的:

    Arb.generate<NonEmptyString> 
    |> Gen.suchThat (fun s -> s.Get.Length < 5 && not (s.Get |> containsInvalidValues))
    

    这意味着您将生成字符串并过滤掉所有满足特定条件的字符串。但如果条件过于严格,FsCheck 可能需要生成大量字符串,直到您真正得到一些通过测试的字符串。

    如果您可以表达规则以便生成字符串以便生成的所有内容都是有效字符串,那么我认为它应该更快。

    例如,您能否生成一个数字n(用于字符串长度),然后生成n 类型的char 值(满足您的条件),然后将它们附加以形成分隔符字符串? (我认为 FsCheck 的 gen { .. } 计算可能是一种很好的写法。)

    【讨论】:

    • 你是对的,谢谢!我最终得到了类似this 的东西。最后一个快速问题:您知道是否可以在 fsx 文件上使用静态类型创建 Generators 吗?我不能:(
    • 我不知道 - 对不起!
    猜你喜欢
    • 2019-01-24
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多