async function calculateP99()

in middleware/controllers/transactions.js [93:152]


async function calculateP99(req, res) {
  const { samples = 50 } = req.body
  const baseUrl = `${getEnv("CPP_TRANSACTIONS_API_BASE_URL")}/commit`;

  let promises = [];
  let responseTimes = [];

  for (let i = 0; i < Math.min(50,samples); i++) {
    const randomValue = getRandomInt(samples)
    const key = `key_${randomValue}`;
    const value = `value_${randomValue}`;

    const data = {
      id: key,
      value: value,
    };

    const config = {
      method: "post",
      maxBodyLength: Infinity,
      url: baseUrl,
      data: data,
    };

    promises.push(
      new Promise(async (resolve, reject) => {
        const startTime = Date.now();
        try {
          const response = await axios.request(config);
          const responseTime = Date.now() - startTime;
          responseTimes.push(responseTime);

          logger.info(`Key: ${key} set successfully. Response time: ${responseTime}ms`);
          resolve(response.data);
        } catch (error) {
          logger.error(`Failed to set key: ${key}. Error: ${error.message}`);
          reject(error);
        }
      })
    );
  }

  try {
    await Promise.all(promises);

    responseTimes.sort((a, b) => a - b);
    const p99Index = Math.ceil(0.99 * responseTimes.length) - 1;
    const p99 = responseTimes[p99Index];

    return res.send({
      message: `Cache warmed with ${samples} random values successfully.`,
      p99,
    });
  } catch (error) {
    logger.error("Error warming the cache:", error.message);
    return res.status(500).send({
      error: "Failed to warm cache.",
    });
  }
}