# Elasticsearch load test, driven by the `loadr-plugin-elasticsearch` native
# protocol plugin.
#
# Elasticsearch support is NOT built into loadr core — it is a runtime-loadable
# plugin. Elasticsearch's API is plain HTTP/JSON, so the plugin talks to it over
# loadr's own hyper + hyper-rustls stack (pure-Rust TLS, no system OpenSSL); it
# does not pull in the heavy official `elasticsearch` crate. The plugin declares
# the `elasticsearch://` (and `es://`) URL schemes — both mapped onto `http://`
# internally — so once it is installed a request to one of those URLs routes
# straight to it; no explicit `protocol:` is needed. A plain `http(s)://` URL
# works too.
#
# Each request runs one operation (index/get/search/bulk) from its `plugin:`
# block. loadr records the operation latency in `elasticsearch_req_duration`,
# the count of documents written (index = 1, bulk = items succeeded) in
# `elasticsearch_docs`, and any HTTP/cluster error fails the request (status
# 1 = ok, status 0 = error).
#
# Build + install the plugin, then run:
#   cargo build -p loadr-plugin-elasticsearch --release
#   mkdir -p dist && cp plugins/loadr-plugin-elasticsearch/plugin.toml dist/ \
#     && cp target/release/libloadr_plugin_elasticsearch.so dist/
#   loadr plugin install dist
#   loadr run examples/33-elasticsearch.yaml
#
# Or point the plan's `plugins:` entry at the built artifact directly (below).
name: elasticsearch
description: index + bulk + search against Elasticsearch via loadr-plugin-elasticsearch

plugins:
  # Resolve `elasticsearch` by name from the plugins dir (after install).
  # To run straight from a build tree instead, set:
  #   path: target/release/libloadr_plugin_elasticsearch.so
  - name: elasticsearch

scenarios:
  # Steady write load: index documents one at a time.
  writes:
    executor: constant-vus
    vus: 5
    duration: 15s
    flow:
      - request:
          name: index product
          url: elasticsearch://es.example.com:9200
          plugin:
            operation: index
            index: products
            document:
              name: "vu-${vu}-item"
              price: 12.5
              stock: 3
              run: "${vu}-${iteration}"
          assert:
            - { type: status, equals: 1 }            # 1 = ok, 0 = error

  # Bulk writes amortise round-trips across many documents per request.
  bulk:
    executor: constant-vus
    vus: 2
    duration: 15s
    flow:
      - request:
          name: bulk index products
          url: elasticsearch://es.example.com:9200
          plugin:
            operation: bulk
            index: products
            operations:
              - { index: {} }
              - { name: "bulk-${vu}-a", price: 1.0, stock: 10 }
              - { index: {} }
              - { name: "bulk-${vu}-b", price: 2.0, stock: 20 }
          checks:
            - { type: status, equals: 1 }

  # Read traffic: match-all search with a bounded result size.
  reads:
    executor: constant-arrival-rate
    rate: 50
    duration: 15s
    pre_allocated_vus: 10
    max_vus: 40
    flow:
      - request:
          name: search cheap products
          url: elasticsearch://es.example.com:9200
          plugin:
            operation: search
            index: products
            query:
              size: 20
              query:
                range:
                  price: { lt: 50 }
          checks:
            - { type: status, equals: 1 }
            - { type: duration, name: search is fast, max: 300ms }

thresholds:
  checks: [ "rate>0.99" ]
  elasticsearch_req_duration: [ "p(95)<400ms" ]
  elasticsearch_reqs: [ "count>0" ]
