mirror of
https://github.com/usebruno/bruno.git
synced 2024-12-23 23:29:47 +01:00
Merge pull request #397 from dozed/response-time-axios
Use axios interceptor to measure response time
This commit is contained in:
commit
e3f32dfc87
@ -11,6 +11,7 @@ const interpolateVars = require('./interpolate-vars');
|
||||
const { ScriptRuntime, TestRuntime, VarsRuntime, AssertRuntime } = require('@usebruno/js');
|
||||
const { stripExtension } = require('../utils/filesystem');
|
||||
const { getOptions } = require('../utils/bru');
|
||||
const { makeAxiosInstance } = require('../utils/axios-instance');
|
||||
|
||||
const runSingleRequest = async function (
|
||||
filename,
|
||||
@ -124,15 +125,23 @@ const runSingleRequest = async function (
|
||||
}
|
||||
|
||||
let response, responseTime;
|
||||
const start = Date.now();
|
||||
try {
|
||||
// run request
|
||||
response = await axios(request);
|
||||
responseTime = Date.now() - start;
|
||||
const axiosInstance = makeAxiosInstance();
|
||||
|
||||
/** @type {import('axios').AxiosResponse} */
|
||||
response = await axiosInstance(request);
|
||||
|
||||
// Prevents the duration on leaking to the actual result
|
||||
responseTime = response.headers.get('request-duration');
|
||||
response.headers.delete('request-duration');
|
||||
} catch (err) {
|
||||
if (err && err.response) {
|
||||
response = err.response;
|
||||
responseTime = Date.now() - start;
|
||||
|
||||
// Prevents the duration on leaking to the actual result
|
||||
responseTime = response.headers.get('request-duration');
|
||||
response.headers.delete('request-duration');
|
||||
} else {
|
||||
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));
|
||||
return {
|
||||
@ -156,10 +165,7 @@ const runSingleRequest = async function (
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
chalk.green(stripExtension(filename)) +
|
||||
chalk.dim(` (${response.status} ${response.statusText}) - ${responseTime} ms`)
|
||||
);
|
||||
console.log(chalk.green(stripExtension(filename)) + chalk.dim(` (${response.status} ${response.statusText}) - ${responseTime} ms`));
|
||||
|
||||
// run post-response vars
|
||||
const postResponseVars = get(bruJson, 'request.vars.res');
|
||||
|
40
packages/bruno-cli/src/utils/axios-instance.js
Normal file
40
packages/bruno-cli/src/utils/axios-instance.js
Normal file
@ -0,0 +1,40 @@
|
||||
const axios = require('axios');
|
||||
|
||||
/**
|
||||
* Function that configures axios with timing interceptors
|
||||
* Important to note here that the timings are not completely accurate.
|
||||
* @see https://github.com/axios/axios/issues/695
|
||||
* @returns {import('axios').AxiosStatic}
|
||||
*/
|
||||
function makeAxiosInstance() {
|
||||
/** @type {import('axios').AxiosStatic} */
|
||||
const instance = axios.create();
|
||||
|
||||
instance.interceptors.request.use((config) => {
|
||||
config.headers['request-start-time'] = Date.now();
|
||||
return config;
|
||||
});
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response) => {
|
||||
const end = Date.now();
|
||||
const start = response.config.headers['request-start-time'];
|
||||
response.headers['request-duration'] = end - start;
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
if (error.response) {
|
||||
const end = Date.now();
|
||||
const start = error.config.headers['request-start-time'];
|
||||
error.response.headers['request-duration'] = end - start;
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
makeAxiosInstance
|
||||
};
|
Loading…
Reference in New Issue
Block a user