lava-oushudb-dt-sql-parser/test/helper.ts
Hayden 91b7fd24c0
Feat/trino sql (#188)
* refactor: prefix trino lexer rule name with KW_

* test: add commentOtherLine function

* feat: optimize trino antlr grammar to adapt to c3

* feat: trinosqlParser supports codeCompletion and spliting

* test: trinoSql codeCompletion unit tests
2023-10-24 14:37:27 +08:00

83 lines
2.3 KiB
TypeScript

import fs from 'fs';
import path from 'path';
export const readSQL = (dirname: string, fileName: string) => {
const content = fs.readFileSync(path.join(dirname, 'fixtures', fileName), 'utf-8');
const result: string[] = [];
let tmp = '';
for (let index = 0; index < content.length; index++) {
const char = content[index];
tmp += char;
const isMulti =
tmp.includes('EXECUTE STATEMENT SET') || tmp.includes('BEGIN STATEMENT SET;');
if (!isMulti) {
// 非批量的先简单按照分号切割
if (tmp.endsWith(';')) {
result.push(tmp.trim());
tmp = '';
}
} else {
if (tmp.endsWith('END;')) {
result.push(tmp.trim());
tmp = '';
}
}
}
return result;
};
/**
* Benchmark for a function.
* @param name
* @param fn
* @param times , default 1
* @returns [ totalTime, averageTime, msg ]
*/
export function benchmark(name: string, fn: Function, times: number = 1): [number, number, string] {
const start = performance.now();
for (let i = 0; i < times; i++) {
fn();
}
const end = performance.now();
const totalTime = end - start;
const averageTime = totalTime / times;
const msg = `Benchmark: ${name} executed ${times} times. Total time: ${totalTime.toFixed(
2
)}ms. Average time: ${averageTime.toFixed(2)}ms`;
console.log(msg);
return [totalTime, averageTime, msg];
}
export function getReportTableRow(name, rows, times, totalTime, averageTime) {
return `| ${name} | ${rows} | ${times} | ${totalTime.toFixed(2)} | ${averageTime.toFixed(2)} |`;
}
export function getReportTableHeader(title: string) {
return `# ${title}
| Name | Rows | Times | Total Time(ms) | Average Time(ms) |
| ---- | ---- | ---- | ---- | ---- |\n`;
}
export function exportReportTable(markdown: string, output: string) {
fs.writeFileSync(path.join(output, 'benchmark.md'), markdown);
}
export function commentOtherLine(sqlContent: string, line: number) {
const slices = sqlContent.split('\n').map((item, index) => {
if (index !== line - 1) {
return '-- ' + item;
} else {
return item;
}
});
return slices.join('\n');
}