Skip to content

Instantly share code, notes, and snippets.

@robherley
Last active March 29, 2019 19:55
Show Gist options
  • Save robherley/070d7de9e2429522c49bec9125136450 to your computer and use it in GitHub Desktop.
Save robherley/070d7de9e2429522c49bec9125136450 to your computer and use it in GitHub Desktop.
CS 554 - Lab 4 Grading Script
const { promisify } = require('util');
const axios = require('axios');
const consola = require('consola');
const client = require('redis').createClient();
const flushall = promisify(client.flushall).bind(client);
axios.interceptors.request.use(
config => {
config.meta = { startTime: new Date() };
return config;
},
error => {
return Promise.reject(error);
}
);
axios.interceptors.response.use(
response => {
response.config.meta.endTime = new Date();
const { startTime, endTime } = response.config.meta;
response.duration = endTime - startTime;
return response;
},
error => {
error.config.metadata.endTime = new Date();
error.duration =
error.config.metadata.endTime - error.config.metadata.startTime;
return Promise.reject(error);
}
);
const PORT = process.env.PORT || 3000;
const errors = [];
(async () => {
// Wipe the Redis DB
await flushall();
try {
consola.info('GET: /api/people/1');
const { data, duration: d1 } = await axios.get(
`http://localhost:${PORT}/api/people/1`
);
consola.success('Resolved Successfully');
console.log('\t', data);
if (d1 <= 5000) {
errors.push([10, 'getById did not take at least 5 seconds']);
}
if (typeof data !== 'object') {
errors.push([
5,
'GET: /api/people/1 response data is not of type object'
]);
} else if (data.id !== 1) {
errors.push([5, 'GET: /api/people/1 response data is invalid schema']);
}
consola.info('Testing Second Response GET: /api/people/1');
const { duration: d2 } = await axios.get(
`http://localhost:${PORT}/api/people/1`
);
const diff = d1 - d2;
if (diff < 250) { // 250ms padding to check if different times
errors.push([
5,
`GET: /api/people/:id did not cache response properly (t1: ${d1}, t2: ${d2})`
]);
consola.error('Did Not Cache Request Properly');
} else {
consola.success('Cached Request Properly');
}
} catch (e) {
consola.error('GET: /api/people/1 Fails', e);
process.exit(1);
}
try {
consola.info('Running 30 Additional GET Requests...');
for (let i = 0; i < 30; i++) {
await axios.get(`http://localhost:${PORT}/api/people/1`);
}
consola.success('Finished Additional Requests');
} catch (e) {
consola.error('Unable to complete additional GET requests', e);
process.exit(1);
}
try {
consola.info('GET /api/people/history');
const { data } = await axios.get(
`http://localhost:${PORT}/api/people/history`
);
consola.success('Resolved Successfully');
console.log('\t', data);
if (!Array.isArray(data)) {
errors.push([
10,
'GET: /api/people/history response data is not an Array'
]);
} else {
if (typeof data[0] !== 'object') {
errors.push([
5,
'GET: /api/people/history array items are not of type object'
]);
}
if (data.length !== 20) {
errors.push([
5,
'GET: /api/people/history response array does properly print last 20 users'
]);
}
}
} catch (e) {
consola.error('GET: /api/people/history Fails', e);
process.exit(1);
}
console.log('\n-----');
const score = errors.reduce((acc, [score, msg]) => {
console.log(`\t-${score}; ${msg}`);
return acc + score;
}, 0);
consola.success('Final Score:', 100 - score);
process.exit(0);
})().catch(err => {
consola.error('Script Failed to Finish Successfully');
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment