This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (126 loc) · 4.92 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const hanaClient = require("@sap/hana-client");
const fs = require("fs");
const path = require("path");
const argv = require('minimist')(process.argv.slice(2));
//Amount of Iterations
var iterations = typeof argv.it !== 'undefined' ? argv.it : 1;
//Connection Details
var schema = typeof argv.schema !== 'undefined' ? argv.schema : "DATA_LAKE";
var host = typeof argv.host !== 'undefined' ? argv.host : "3.122.200.232";
var port = typeof argv.port !== 'undefined' ? argv.port : "30215";
var user = typeof argv.user !== 'undefined' ? argv.user : "";
var password = typeof argv.pw !== 'undefined' ? argv.pw : "";
var db = typeof argv.db !== 'undefined' ? argv.db : "HDB";
//Misc
var table_prefix = typeof argv.tablePrefix !== 'undefined' ? argv.tablePrefix : "GEN";
var mountdir = path.join(__dirname+"/");
var simulateOnly = false;
if(user == "" || password == "")
{return console.error("[ERROR] User or password not specified, exiting!");}
console.debug(argv);
const connection = hanaClient.createConnection();
const connectionParams = {
host : host,
port : port,
uid : user,
pwd : password,
databaseName : db
}
function Test(hrstart, table)
{
var sql = `SELECT COUNT(*) AS ROWS FROM "`+schema+`"."`+table+`"`;
connection.exec(sql, (err, rows) => {
if (err) {
return console.error('[ERROR] SQL execute error:', err);
}
console.log(rows);
console.log("[INFO] # Rows in table "+table+" -> "+rows[0].ROWS);
var hrend = process.hrtime(hrstart)
console.info('[INFO] Execution time: %ds %dms', hrend[0], hrend[1] / 1000000);
const stats = fs.statSync("my.csv");
const fileSizeInBytes = stats.size;
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);
var kpimb = fileSizeInMegabytes / ( hrend[0] + hrend[1] / 1000000000 );
var kpirows = rows[0].ROWS / ( hrend[0] + hrend[1] / 1000000000 );
console.info('[INFO] Throughput: '+Math.round(kpimb * 100)/100+' MB/s '+Math.round(kpirows * 100)/100+' rows/s');
});
}
function WriteFile(file, body, resolve)
{
fs.writeFile(file, body, function(err) {
resolve("Stuff worked!");
if(err) {
return console.error(err);
}
console.log('[INFO] File <'+file+'> written successfully!');
});
}
async function executeSQL()
{
for (let index = 0; index < iterations; index++) {
var hrstart = process.hrtime();
var table = table_prefix+index;
var file = table+".ctl";
//PREPARE SQL
var testsql = fs.readFileSync('test.sql').toString();
testsql = testsql.replace(/\"SCHEMA\"/g, '"'+schema+'"');
testsql = testsql.replace(/\"TABLE\"/g, '"'+table+'"');
testsql = testsql.replace(/\/sapmnt\/log\/test.ctl/g, mountdir+file);
//EXECUTE SQL
var sqlstatements = testsql.split(";");
for (let kindex = 0; kindex < sqlstatements.length; kindex++)
{
var sql = sqlstatements[kindex];
sql = sql.trim();
if(sql.charAt(0) != "#" && sql != "")
{
console.log("[INFO] Executing... '"+sql+";'");
if(!simulateOnly)
{
await new Promise(function(resolve, reject)
{
connection.exec(sql, (err, rows) => {
resolve();
if (err) {
return console.error('[ERROR] SQL execute error:', err);
}
if(rows)
{
console.log("[INFO] Results:", rows);
console.log(`[INFO] Query '${sql}' returned ${rows.length} items`);
}
});
});
}
}
if(kindex == sqlstatements.length-1)
Test(hrstart, table);
}
}
}
connection.connect(connectionParams, (err) => {
if (err && !simulateOnly) {
return console.error("[ERROR] Connection error", err);
}
//CREATE CTL FILE
var mypromises = [];
for (let index = 0; index < iterations; index++) {
var table = table_prefix+index;
//PREPARE & CREATE CTL
var testctl = fs.readFileSync('test.ctl').toString();
testctl = testctl.replace(/SCHEMA/g, schema);
testctl = testctl.replace(/TABLE/g, table);
testctl = testctl.replace(/\/sapmnt\/log\//g, mountdir);
var file = table+".ctl";
var mypromise = new Promise(function(resolve, reject) {
WriteFile(file, testctl, resolve);
});
mypromises.push(mypromise);
}
//WAIT UNTIL .CTL FILES WRITTEN
Promise.all(mypromises).then(function(values) {
console.log("[INFO] All .ctl files written");
executeSQL();
});
//connection.disconnect();
});