-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42c183a
commit 1bb0f9a
Showing
4 changed files
with
38 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,20 @@ | ||
const dbAsync = require('./db/db-async') | ||
const { ARDENT_API_HOSTNAME } = require('./consts') | ||
const { exec } = require('child_process') | ||
const commandExistsSync = require('command-exists').sync | ||
const { ARDENT_TRADE_DB } = require('./consts') | ||
|
||
// Warms the cache by hitting the 'import' stats endpoint for every commodity. | ||
// This has the knock-on effect of ensuring that all the commodity data is in | ||
// memory and so getting export data for commodities is also equally fast. | ||
// | ||
// This script was created because low traffic volumes seems to lead to data in | ||
// SQLite being unloaded from memory, resulting in queries taking 10-20 seconds | ||
// instead of being sub-second. | ||
// | ||
// I was unable to resolve the issue through changes to SQLite cache behaviour. | ||
// The database for star systems is much larger (over 100 million entries) but | ||
// does not have any performance problems, I suspect the much greater volume of | ||
// writes to the trade database and/or RAM constraints on the production server | ||
// are underlying factors triggering the performance issue for commodities. | ||
// | ||
// Queries are performed sequentially to avoid unnecessary load on the server. | ||
// | ||
// This task takes ~15 minutes to run when the cache is cold and ~3 minutes when | ||
// the cache warmed up - the goal is to keep it always warm. | ||
module.exports = async ({ debug = false }) => { | ||
if (debug === true) console.time('Time warm cache') | ||
try { | ||
const commodities = await dbAsync.all('SELECT DISTINCT(commodityName) FROM commodities') | ||
if (debug === true) console.log(`Warming cache for ${ARDENT_API_HOSTNAME}`) | ||
for (let i = 0; i < commodities.length; i++) { | ||
const { commodityName } = commodities[i] | ||
const url = `https://${ARDENT_API_HOSTNAME}/v1/commodity/name/${commodityName}/imports` | ||
if (debug === true) console.time(`${i+1} of ${commodities.length} ${commodityName}`) | ||
const res = await fetch(url) | ||
if (!res.ok) console.error(`Cache warm error fetching: ${url}`) | ||
if (debug === true) console.timeEnd(`${i+1} of ${commodities.length} ${commodityName}`) | ||
} | ||
} catch (e) { | ||
return console.error('Cache warm failed:', e) | ||
module.exports = () => { | ||
if (commandExistsSync('vmtouch')) { | ||
// Try (but don't require) to keep all trade database files in memory cache. | ||
// | ||
// Other databases like the Station and even much larger Systems database | ||
// work fine without being in memory, the trade database is a special case, | ||
// due to the nature of the data and the many ways it can be queried. | ||
// | ||
// Note: Not using vmtouch in daemon mode by design - too many side effects | ||
// but best effort prompting every 5 minutes is fine. It takes between ~90 | ||
// seconds to run from cold boot and < 1 second if already fully cached. | ||
exec(`vmtouch -t ${ARDENT_TRADE_DB}* -m 24G`, (error, stdout, stderr) => { | ||
if (error) console.error(error) | ||
}) | ||
} | ||
if (debug === true) console.timeEnd('Time warm cache') | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters