57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const { performance } = require('perf_hooks');
|
|
const Sqlite = require('better-sqlite3');
|
|
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'account-import-performance-'));
|
|
const dbPath = path.join(tempDir, 'accounts.db');
|
|
const Config = require('../src/core/config');
|
|
Config.DATA_DIR = tempDir;
|
|
Config.DB_PATH = dbPath;
|
|
const database = require('../src/core/database');
|
|
|
|
try {
|
|
database.initDB();
|
|
const db = database.getDb();
|
|
const transactionStates = [];
|
|
db.function('capture_import_transaction', () => {
|
|
transactionStates.push(db.inTransaction);
|
|
return 1;
|
|
});
|
|
db.exec(`
|
|
CREATE TRIGGER capture_account_import_transaction
|
|
AFTER INSERT ON accounts
|
|
BEGIN
|
|
SELECT capture_import_transaction();
|
|
END
|
|
`);
|
|
|
|
const text = Array.from({ length: 1000 }, (_, index) => {
|
|
const id = String(index).padStart(4, '0');
|
|
return `bulk${id}@example.com----Password${id}----helper${id}@example.com----JBSWY3DPEHPK3PXP`;
|
|
}).join('\n');
|
|
|
|
const startedAt = performance.now();
|
|
const result = database.importAccountsFromText(text);
|
|
const elapsedMs = performance.now() - startedAt;
|
|
|
|
assert.strictEqual(result.successCount, 1000);
|
|
assert.strictEqual(result.errorCount, 0);
|
|
assert.strictEqual(database.getAllAccounts().length, 1000);
|
|
assert.strictEqual(transactionStates.length, 1000);
|
|
assert(transactionStates.every(Boolean), '每条账号写入都应位于同一个批量事务中');
|
|
assert(elapsedMs < 15000, `1000 条账号导入耗时异常: ${elapsedMs.toFixed(0)}ms`);
|
|
|
|
const last = database.getAccount('bulk0999@example.com');
|
|
assert.strictEqual(last.secret_key, 'jbswy3dpehpk3pxp');
|
|
assert.strictEqual(last.raw_account_line.includes('bulk0999@example.com'), true);
|
|
|
|
console.log(`account_import_performance.test.js: PASS (${elapsedMs.toFixed(0)}ms)`);
|
|
} finally {
|
|
try { database.getDb().close(); } catch (_) {}
|
|
try { new Sqlite(dbPath).close(); } catch (_) {}
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|