forked from hnasr/javascript_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.js
More file actions
72 lines (55 loc) · 1.82 KB
/
all.js
File metadata and controls
72 lines (55 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const app = require("express")();
const {Pool} = require("pg");
const {Client} = require("pg");
let oldCount = 0;
let oldSum = 0;
let poolCount = 0;
let poolSum = 0;
const pool = new Pool({
"host": "husseinmac.local",
"port": 5432,
"user":"postgres",
"password" : "postgres",
"database" : "husseindb",
"max": 10
})
app.get("/old", async (req, res) => {
const fromDate = new Date();
oldCount++;
const client = new Client({
"host": "husseinmac.local",
"port": 5432,
"user":"postgres",
"password" : "postgres",
"database" : "husseindb"
})
//connect
await client.connect();
//return all rows
const results = await client.query("select * from employees")
console.table(results.rows)
//end
client.end();
const toDate = new Date();
const elapsed = toDate.getTime() - fromDate.getTime();
oldSum += elapsed;
//send it to the wire
res.send({"rows": results.rows, "elapsed": elapsed, "avg" : Math.round(oldSum/ oldCount), "method": "old"})
})
app.get("/pool", async (req, res) => {
const fromDate = new Date();
poolCount ++;
//return all rows
const results = await pool.query("select * from employees")
console.table(results.rows)
const toDate = new Date();
const elapsed = toDate.getTime() - fromDate.getTime();
poolSum += elapsed;
//send it to the wire
res.send({"rows": results.rows, "elapsed": elapsed, "avg": Math.round(poolSum/poolCount), "method": "pool"})
})
app.listen(9000, () => console.log("Listening on port 9000"))
/*
for (let i = 0; i < 1000; i++) fetch(`http://localhost:9000/old`).then(a=>a.json()).then(console.log).catch(console.error);
for (let i = 0; i < 1000; i++) fetch(`http://localhost:9000/pool`).then(a=>a.json()).then(console.log).catch(console.error);
*/