Skip to content

Instantly share code, notes, and snippets.

@kylejeske
Last active September 17, 2023 21:17
Show Gist options
  • Save kylejeske/44fb4f860e9e3ef3852eadced3975ba4 to your computer and use it in GitHub Desktop.
Save kylejeske/44fb4f860e9e3ef3852eadced3975ba4 to your computer and use it in GitHub Desktop.
import * as fs from 'fs';
import * as path from 'path';
interface CPUMeasurements {
average_cpu_usage: number;
max_cpu_usage: number;
min_cpu_usage: number;
}
interface MemoryMeasurements {
average_memory_usage: number;
max_memory_usage: number;
min_memory_usage: number;
}
interface ExecutionMeasurements {
start_time: string;
end_time: string;
execution_duration: string;
}
interface PerformanceTest {
test_name: string;
test_suite: string;
cpu_measurements: CPUMeasurements;
memory_measurements: MemoryMeasurements;
execution_measurements: ExecutionMeasurements;
}
function generateRandomPerformanceTest(index: number, startTime: Date): PerformanceTest {
const test: PerformanceTest = {
test_name: `PerformanceTest${index + 1}`,
test_suite: "ApplicationPerformance",
cpu_measurements: {
average_cpu_usage: parseFloat((Math.random() * 100).toFixed(2)),
max_cpu_usage: parseFloat((Math.random() * 100).toFixed(2)),
min_cpu_usage: parseFloat((Math.random() * 100).toFixed(2)),
},
memory_measurements: {
average_memory_usage: Math.floor(Math.random() * (4096 - 512 + 1)) + 512,
max_memory_usage: Math.floor(Math.random() * (4096 - 512 + 1)) + 512,
min_memory_usage: Math.floor(Math.random() * (4096 - 512 + 1)) + 512,
},
execution_measurements: {
start_time: startTime.toISOString(),
end_time: new Date(startTime.getTime() + Math.floor(Math.random() * (600000 - 60000 + 1)) + 60000).toISOString(), // Random duration between 1 minute and 10 minutes
execution_duration: `${Math.floor(Math.random() * 30) + 1} minutes`,
},
};
return test;
}
function generateRandomPerformanceTests(): PerformanceTest[] {
const tests: PerformanceTest[] = [];
let currentTime = new Date();
for (let i = 0; i < 30; i++) {
const test = generateRandomPerformanceTest(i, currentTime);
tests.push(test);
currentTime = new Date(Date.parse(test.execution_measurements.end_time));
}
return tests;
}
const performanceTests = generateRandomPerformanceTests();
// Save the generated JSON data to a file (optional)
const outputPath = path.join(__dirname, 'performance_tests.json');
fs.writeFileSync(outputPath, JSON.stringify(performanceTests, null, 2), 'utf-8');
// Print the generated JSON data
console.log(JSON.stringify(performanceTests, null, 2));
import { Client } from '@elastic/elasticsearch';
// Initialize Elasticsearch client
const client = new Client({ node: 'http://localhost:9200' }); // Replace with your Elasticsearch cluster URL
// Sample data to be inserted
const documents = [
{ index: { _index: 'my_index', _id: '1' } },
{ name: 'Document 1', description: 'This is document 1' },
{ index: { _index: 'my_index', _id: '2' } },
{ name: 'Document 2', description: 'This is document 2' },
// Add more documents here...
];
// Function to batch insert documents
async function batchInsertDocuments() {
try {
const { body: bulkResponse } = await client.bulk({ refresh: true, body: documents });
if (bulkResponse.errors) {
console.error('Error inserting documents:', bulkResponse.errors);
} else {
console.log('Documents inserted successfully!');
}
} catch (error) {
console.error('Error:', error);
} finally {
// Close the Elasticsearch client when done
client.close();
}
}
// Call the batchInsertDocuments function to insert the documents
batchInsertDocuments();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment