================================================================================
api/benchmarkModule
================================================================================

amends ".../snippetTest.pkl"

import "pkl:Benchmark"

facts {
  ["run microbenchmark"] {
    local benchmark = new Benchmark.Microbenchmark {
      expression = IntSeq(1, 10).fold(0, (result, next) -> result + next)
      iterations = 3
      iterationTime = 1.ms
      isVerbose = false
    }
    local result = benchmark.run()
    
    result.iterations == 3
    result.repetitions >= 1
    result.samples == null
    result.min > 0.s
    result.max >= result.min
    result.mean >= result.min
    result.mean <= result.max
  }
  
  ["run output benchmark (in verbose mode)"] {
    local benchmark = new Benchmark.OutputBenchmark {
      sourceModule = import("../../input-helper/api/benchmark/moduleToBenchmark.pkl")
      iterations = 3
      iterationTime = 1.ms
      isVerbose = true
    }
    local result = benchmark.run()

    result.iterations == 3
    result.repetitions >= 1
    result.samples != null
    result.samples.length == 3
    result.min == result.samples.min
    result.max == result.samples.max
    result.mean >= result.min
    result.mean <= result.max
  }
  
  ["run parser benchmark"] {
    local benchmark = new Benchmark.ParserBenchmark {
      sourceText = "result = IntSeq(1, 10).fold(0, (result, next) -> result + next)"
      iterations = 3
      iterationTime = 1.ms
      isVerbose = false
    }
    local result = benchmark.run()

    result.iterations == 3
    result.repetitions >= 1
    result.samples == null
    result.min > 0.s
    result.max >= result.min
    result.mean >= result.min
    result.mean <= result.max
  }

  ["evaluate entire benchmark module"] {
    local benchmark = new Benchmark {
      iterations = 3
      iterationTime = 1.ms

      microbenchmarks {
        ["micro"] {
          expression = IntSeq(1, 10).fold(0, (result, next) -> result + next)
        }
      }

      outputBenchmarks {
        ["output"] {
          sourceModule = import("../../input-helper/api/benchmark/moduleToBenchmark.pkl")
        }
      }

      parserBenchmarks {
        ["parser"] {
          sourceText = "result = IntSeq(1, 10).fold(0, (result, next) -> result + next)"
        }
      }
    }

    local text = benchmark.output.text
    text.contains("[\"micro\"]")
    text.contains("[\"output\"]")
    text.contains("[\"parser\"]")
  }
}

--------------------------------------------------------------------------------

(module
  (moduleHeader
    (extendsOrAmendsClause
      (stringConstant)))
  (importClause
    (stringConstant))
  (classProperty
    (identifier)
    (objectBody
      (objectEntry
        (slStringLiteral)
        (objectBody
          (objectProperty
            (modifier)
            (identifier)
            (newExpr
              (type
                (qualifiedIdentifier
                  (identifier)
                  (identifier)))
              (objectBody
                (objectProperty
                  (identifier)
                  (methodCallExpr
                    (methodCallExpr
                      (identifier)
                      (argumentList
                        (intLiteral)
                        (intLiteral)))
                    (identifier)
                    (argumentList
                      (intLiteral)
                      (functionLiteral
                        (parameterList
                          (typedIdentifier
                            (identifier))
                          (typedIdentifier
                            (identifier)))
                        (binaryExpr
                          (variableExpr
                            (identifier))
                          (variableExpr
                            (identifier)))))))
                (objectProperty
                  (identifier)
                  (intLiteral))
                (objectProperty
                  (identifier)
                  (propertyCallExpr
                    (intLiteral)
                    (identifier)))
                (objectProperty
                  (identifier)
                  (falseLiteral)))))
          (objectProperty
            (modifier)
            (identifier)
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (nullLiteral)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (intLiteral))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))))
      (objectEntry
        (slStringLiteral)
        (objectBody
          (objectProperty
            (modifier)
            (identifier)
            (newExpr
              (type
                (qualifiedIdentifier
                  (identifier)
                  (identifier)))
              (objectBody
                (objectProperty
                  (identifier)
                  (importExpr
                    (stringConstant)))
                (objectProperty
                  (identifier)
                  (intLiteral))
                (objectProperty
                  (identifier)
                  (propertyCallExpr
                    (intLiteral)
                    (identifier)))
                (objectProperty
                  (identifier)
                  (trueLiteral)))))
          (objectProperty
            (modifier)
            (identifier)
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (nullLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (propertyCallExpr
              (propertyCallExpr
                (binaryExpr
                  (propertyCallExpr
                    (variableExpr
                      (identifier))
                    (identifier))
                  (variableExpr
                    (identifier)))
                (identifier))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (propertyCallExpr
                (binaryExpr
                  (propertyCallExpr
                    (variableExpr
                      (identifier))
                    (identifier))
                  (variableExpr
                    (identifier)))
                (identifier))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))))
      (objectEntry
        (slStringLiteral)
        (objectBody
          (objectProperty
            (modifier)
            (identifier)
            (newExpr
              (type
                (qualifiedIdentifier
                  (identifier)
                  (identifier)))
              (objectBody
                (objectProperty
                  (identifier)
                  (slStringLiteral))
                (objectProperty
                  (identifier)
                  (intLiteral))
                (objectProperty
                  (identifier)
                  (propertyCallExpr
                    (intLiteral)
                    (identifier)))
                (objectProperty
                  (identifier)
                  (falseLiteral)))))
          (objectProperty
            (modifier)
            (identifier)
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (intLiteral)))
          (objectElement
            (binaryExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (nullLiteral)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (intLiteral))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))
          (objectElement
            (propertyCallExpr
              (binaryExpr
                (propertyCallExpr
                  (variableExpr
                    (identifier))
                  (identifier))
                (variableExpr
                  (identifier)))
              (identifier)))))
      (objectEntry
        (slStringLiteral)
        (objectBody
          (objectProperty
            (modifier)
            (identifier)
            (newExpr
              (type
                (qualifiedIdentifier
                  (identifier)))
              (objectBody
                (objectProperty
                  (identifier)
                  (intLiteral))
                (objectProperty
                  (identifier)
                  (propertyCallExpr
                    (intLiteral)
                    (identifier)))
                (objectProperty
                  (identifier)
                  (objectBody
                    (objectEntry
                      (slStringLiteral)
                      (objectBody
                        (objectProperty
                          (identifier)
                          (methodCallExpr
                            (methodCallExpr
                              (identifier)
                              (argumentList
                                (intLiteral)
                                (intLiteral)))
                            (identifier)
                            (argumentList
                              (intLiteral)
                              (functionLiteral
                                (parameterList
                                  (typedIdentifier
                                    (identifier))
                                  (typedIdentifier
                                    (identifier)))
                                (binaryExpr
                                  (variableExpr
                                    (identifier))
                                  (variableExpr
                                    (identifier)))))))))))
                (objectProperty
                  (identifier)
                  (objectBody
                    (objectEntry
                      (slStringLiteral)
                      (objectBody
                        (objectProperty
                          (identifier)
                          (importExpr
                            (stringConstant)))))))
                (objectProperty
                  (identifier)
                  (objectBody
                    (objectEntry
                      (slStringLiteral)
                      (objectBody
                        (objectProperty
                          (identifier)
                          (slStringLiteral)))))))))
          (objectProperty
            (modifier)
            (identifier)
            (propertyCallExpr
              (propertyCallExpr
                (variableExpr
                  (identifier))
                (identifier))
              (identifier)))
          (objectElement
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList
                (slStringLiteral
                  (escapeSequence)
                  (escapeSequence)))))
          (objectElement
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList
                (slStringLiteral
                  (escapeSequence)
                  (escapeSequence)))))
          (objectElement
            (methodCallExpr
              (variableExpr
                (identifier))
              (identifier)
              (argumentList
                (slStringLiteral
                  (escapeSequence)
                  (escapeSequence))))))))))
