Last active
March 2, 2021 05:17
-
-
Save adambankin/b83a245ca059482a54647ce822c399e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require("mongodb").MongoClient; | |
const tracer = require("dd-trace").init(); | |
const StatsD = require("hot-shots"); | |
const dogstatsd = new StatsD(); | |
function getRandomInt(max, skews = 1) { | |
let result = max; | |
for (let i = 0; i < skews; i++) { | |
result = Math.random() * result; | |
} | |
return Math.floor(result); | |
} | |
function getRandomArrayItem(arr, skews) { | |
const position = getRandomInt(arr.length, skews); | |
return arr[position]; | |
} | |
function wasteTime(fakeServerLogs, data) { | |
fakeServerLogs.insertOne(data); | |
return new Promise((resolve) => setTimeout(resolve, 100)); | |
} | |
async function run() { | |
const client = new MongoClient("mongodb://localhost:27017"); | |
let isNow = false; | |
const currentTime = new Date().getTime(); | |
let prevTime = new Date("2021-02-25T03:24:00").getTime(); | |
const endpointArray = ["/index", "/cart", "/checkout", "/contact_us"]; | |
const responseTypeArray = ["200", "404", "500"]; | |
try { | |
await client.connect(); | |
const db = client.db("admin"); | |
const fakeServerLogs = db.collection("fake_server_logs"); | |
while (!isNow) { | |
prevTime = new Date(prevTime + getRandomInt(1000000, 4)).getTime(); | |
if (prevTime > currentTime) { | |
isNow = true; | |
} else { | |
const responseTime = getRandomInt(1000); | |
// create new data object | |
const data = { | |
path: getRandomArrayItem(endpointArray, 2), | |
received_timestamp: new Date(prevTime).toISOString(), | |
response_timestamp: new Date(prevTime + responseTime).toISOString(), | |
response_time: responseTime, | |
response_code: getRandomArrayItem(responseTypeArray, 3), | |
}; | |
await wasteTime(fakeServerLogs, data); | |
} | |
} | |
} finally { | |
await client.close(); | |
} | |
} | |
run().catch(console.dir); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment