#######################################
#       C Benchmark Tests             #
#######################################



#######################################
#            Variables                #
#######################################

c_driver_variables:

  ## Task list
  benchmark_compile: &benchmark_compile
    - name: "compile"
  benchmark_test_list: &benchmark_tests
    - name: "BenchMarkTests"

  ## Common download urls (merge in as hashes)
  mongo_download_url_prefixes:
    mongo_v32: &mongo_v32
      mongo_url: "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.10.tgz"

  ## Common sets of CFLAGS
  cflags:
    standard: &cflags_64
      cflags: "-m64 -march=x86-64"

  ## Scripts that are shared between buildvariants
  scripts:
    compile:
      unix: &compile_unix
        compile_script: |
          set -o errexit
          set -o verbose
          . "./.evergreen/find-cmake.sh"

          $CMAKE -DENABLE_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=mongoc . && make -j8 && make install
          git clone git@github.com:mongodb/mongo-c-driver-performance.git
          cd mongo-c-driver-performance
          $CMAKE -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=../mongoc . && make -j8

    mongodb:
      start_mongod_command: &start_mongod_command
        start_mongod: |
          set -o errexit
          set -o verbose
          mkdir db
          echo "Starting Mongodb"
          ./mongodb/bin/mongod --dbpath `pwd`/db --logpath `pwd`/db/db.log --fork

  benchmark_common: &benchmark_common
    <<: *compile_unix
    <<: *start_mongod_command

#######################################
#            Functions                #
#######################################

functions:

  "compile function" :
    command: shell.exec
    params:
      working_dir: "mongo-c-driver"
      script: |
          export PATH=${extra_path}:$PATH
          export CFLAGS="${cflags}"
          ${compile_script}

  "fetch source" :
    command: git.get_project
    params:
      directory: "mongo-c-driver"

  "fetch artifacts" :
    command: shell.exec
    params:
      working_dir: "mongo-c-driver"
      script: |
        set -o verbose
        set -o errexit
        rm -rf mongo-c-driver*
        curl --retry 5 http://s3.amazonaws.com/mciuploads/mongo-c-driver/c-driver-benchmark-compile/${revision}/artifacts/${file}.tar.gz -o c-perf-binaries.tar.gz -sS --max-time 120
        ${decompress} c-perf-binaries.tar.gz
        rm c-perf-binaries.tar.gz

  "fetch mongodb" :
    command: shell.exec
    params:
      working_dir: "mongo-c-driver"
      script: |
        set -o verbose
        set -o errexit
        curl --retry 5 -s ${mongo_url} --output mongo-archive.${ext|tgz}
        ${decompress} mongo-archive.${ext|tgz}
        mv mongodb* mongodb
        chmod +x ./mongodb/bin/mongod${extension}

  "fetch driver test data" :
    command: shell.exec
    params:
      working_dir: "mongo-c-driver"
      script: |
        set -o verbose
        set -o errexit
        curl --retry 5 https://s3.amazonaws.com/boxes.10gen.com/build/driver-test-data.tar.gz -o driver-test-data.tar.gz -sS --max-time 120
        ${decompress} driver-test-data.tar.gz

  "run benchmark tests" :
    command: shell.exec
    params:
      working_dir: "mongo-c-driver"
      script: |
        set -o errexit
        set -o verbose

        result=-1  # Negative one.
        ulimit -c unlimited || true

        # Run this function on exit.
        done=false
        finish () {
           set +o errexit
           if [ "$done" = false ]; then
              # There was an error.
              echo "something went wrong, killing mongod and exiting"
              killall -9 mongod
           fi

           exit $result
        }

        trap finish EXIT

        echo "Starting mongod"
        ${start_mongod}
        sleep 15

        echo "Running Benchmark tests "
        start_time=$(date +%s)
        LD_LIBRARY_PATH=`pwd`/mongoc/lib64:$LD_LIBRARY_PATH ./mongo-c-driver-performance/mongo-c-performance ./data
        set +o errexit
        result=$?

        cat results.json

        result=$?
        end_time=$(date +%s)
        elapsed_secs=$((end_time-start_time))
        if [ $result -eq 0 ]; then
          status='"pass"'
          failures=0
        else
          status='"fail"'
          failures=1
        fi
        echo "{\"failures\": $failures, \"results\": [{\"status\": $status, \"exit_code\": $result, \"test_file\": \"BenchMarkTests\", \"start\": $start_time, \"end\": $end_time, \"elapsed\": $elapsed_secs}]}" > report.json
        cat report.json
       
        set +o errexit
        killall -9 mongod

        done=true
        exit $result

  "attach benchmark test results" :
    command: attach.results
    params:
      file_location: mongo-c-driver/report.json

  "send dashboard data" :
    command: json.send
    params:
      name: perf
      file: mongo-c-driver/results.json

  "backtrace":
    - command: shell.exec
      params:
        working_dir: "mongo-c-driver"
        script: |
          echo "Debugging core files"

          shopt -s nullglob
          for i in *core*; do
             echo $i
             echo "backtrace full" | gdb -q ./mongo-c-driver-performance/mongo-c-performance $i
          done

pre:
  # Remove and recreate old directory
  - command: shell.exec
    params:
      script: |
        rm -rf mongo-c-driver
        mkdir mongo-c-driver

post:
  - func: "backtrace"
  # Remove and recreate old directory
  - command: shell.exec
    params:
      script: |
        rm -rf mongo-c-driver
        mkdir mongo-c-driver

#######################################
#               Tasks                 #
#######################################

tasks:
    - name: compile
      commands:
        - func: "fetch source"
        - command: git.apply_patch
          params:
            directory: "mongo-c-driver"
        - func: "compile function"
        - command: shell.exec
          params:
              working_dir: "mongo-c-driver"
              script: |
                set -o errexit
                set -o verbose
                tar czf ../mongo-c-driver.tar.gz .
        - command: s3.put
          params:
            aws_key: ${aws_key}
            aws_secret: ${aws_secret}
            local_file: mongo-c-driver.tar.gz
            remote_file: mongo-c-driver/${build_variant}/${revision}/artifacts/c-perf-binaries.tar.gz
            bucket: mciuploads
            permissions: public-read
            content_type: ${content_type|application/x-gzip}

    - name: BenchMarkTests
      depends_on:
      - variant: c-driver-benchmark-compile
        name: compile
      commands:
        - func: "fetch artifacts"
          vars:
            file: c-perf-binaries
        - func: "fetch mongodb"
        - func: "fetch driver test data"
        - func: "run benchmark tests"
        - func: "attach benchmark test results"
        - func: "send dashboard data"


#######################################
#           Buildvariants             #
#######################################

buildvariants:

- name: c-driver-benchmark-compile
  display_name: "C Driver Benchmark Compile"
  expansions:
    <<: *cflags_64
    <<: *mongo_v32
    <<: *benchmark_common
  run_on:
     - rhel62-small
  tasks: *benchmark_compile

- name: c-driver-benchmark-mongo32
  display_name: "C Driver Benchmark Mongo 3.2"
  expansions:
    <<: *cflags_64
    <<: *mongo_v32
    <<: *benchmark_common
  run_on:
     - centos6-perf
  tasks: *benchmark_tests
