import http from 'k6/http';
import { sleep } from 'k6';
import { Rate, Trend } from 'k6/metrics';

const successRate = new Rate('OK');
const dataTrend = new Trend('DataTrend');
const clickTrend = new Trend('ClickTrend');

export const options = {
  scenarios: {
    ramp_to_current: {
      executor: 'ramping-arrival-rate',
      startRate: 0,
      preAllocatedVUs: 200,
      maxVUs: 250,
      stages: [
        { duration: '4m', target: 50, },
      ],
    },
    maintain_current: {
      executor: 'constant-arrival-rate',
      rate: 50,
      timeUnit: '1s',
      startTime: '4m',
      duration: '1m',
      preAllocatedVUs: 200,
      maxVUs: 250,
    },
    ramp_to_expected: {
      executor: 'ramping-arrival-rate',
      startRate: 50,
      startTime: '5m',
      preAllocatedVUs: 600,
      maxVUs: 750,
      stages: [
        { duration: '4m', target: 150, },
      ],
    },
    maintain_future: {
      executor: 'constant-arrival-rate',
      rate: 150,
      timeUnit: '1s',
      startTime: '9m',
      duration: '1m',
      preAllocatedVUs: 600,
      maxVUs: 750,
    },
  },
  thresholds: {
    ClickTrend: ['p(90)<100','p(95)<200'],
    DataTrend: ['p(90)<1000','p(95)<1500'],
    OK: ['rate==1'],
  },
};

/**
 * This is the test case that should be executed.
 *
 * @param {any} data the data returned by setup
 */
export default function makeAPIRequest(data) {
    const dataRes = http.get('http://localhost:8080/data');
    const clickRes = http.post('http://localhost:8080/click');

    successRate.add(dataRes.status < 300 || clickRes.status < 300);
    dataTrend.add(dataRes.timings.duration);
    clickTrend.add(clickRes.timings.duration);
}
