Skip to content

Instantly share code, notes, and snippets.

@alasarr
Last active October 6, 2023 17:48
Show Gist options
  • Save alasarr/b3a7fbf8228c0d5d4fa8c675924fd20d to your computer and use it in GitHub Desktop.
Save alasarr/b3a7fbf8228c0d5d4fa8c675924fd20d to your computer and use it in GitHub Desktop.
Testing concurrency
const { Pool } = require('pg');
const N_REQUESTS = 32;
const CONCURRENCY = 32;
// Replace these with your Redshift cluster details
const config = {
user: 'XXXX',
host: 'XXX',
database: 'XXX',
password: 'XXX',
port: 5439,
max: CONCURRENCY, // The pool size is used to control concurrency
};
const pool = new Pool(config);
let x = 159; //used to modify the query
const executeQuery = async () => {
const client = await pool.connect();
try {
const startTime = performance.now();
await client.query('SET enable_result_cache_for_session TO off;')
let query = `
SELECT data FROM
carto_dev_data.tilesets.geography_usa_blockgroup_2019_tileset
WHERE
z=10 AND
x=${x} AND
y=363 AND
data IS NOT NULL`
// x += 1
// console.log(query)
await client.query(query);
const endTime = performance.now();
const elapsedTime = Math.round(endTime - startTime);
query = `SELECT pg_last_query_id()`
const result = await client.query(query);
console.log(`${result.rows[0].pg_last_query_id}, ${elapsedTime}`);
} finally {
client.release();
}
};
const executeConcurrentRequests = async () => {
const requests = [];
for (let i = 0; i < N_REQUESTS; i++) {
requests.push(executeQuery());
}
await Promise.all(requests);
await pool.end(); // Close the connection pool after all requests are complete
};
executeConcurrentRequests().catch(err => console.error('Error:', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment