Files
shields/lib/server-config.js
Paul Melnikow cc9a6db853 Speed up font-width computation in most cases (#1390)
Ref: #1379

This takes a naive approach to font-width computation, the most compute-intensive part of rendering badges.

1. Add the widths of the individual characters.
    - These widths are measured on startup using PDFKit.
2. For each character pair, add a kerning adjustment
    - The difference between the width of each character pair, and the sum of the characters' separate widths.
    - These are computed for each character pair on startup using PDFKit.
3. For a string with characters outside the printable ASCII character set, fall back to PDFKit.

This branch averaged 0.041 ms in `makeBadge`, compared to 0.144 ms on master, a speedup of 73%. That was on a test of 10,000 consecutive requests (using the `benchmark-performance.sh` script, now checked in).

The speedup applies to badges containing exclusively printable ASCII characters. It wouldn't be as dramatic on non-ASCII text. Though, we could add some frequently used non-ASCII characters to the cached set.
2017-12-26 23:57:46 -05:00

74 lines
1.8 KiB
JavaScript

'use strict';
// This file should only be required by server.js. To simplify testing, config
// should be injected into other components needing it.
const url = require('url');
const envFlag = require('node-env-flag');
const defaults = require('./defaults');
function envArray(envVar, defaultValue, delimiter) {
delimiter = delimiter || ',';
if (envVar) {
return envVar.split(delimiter);
} else {
return defaultValue;
}
}
const isSecure = envFlag(process.env.HTTPS, false);
const port = +process.env.PORT || +process.argv[2] || (isSecure ? 443 : 80);
const address = process.env.BIND_ADDRESS || process.argv[3] || '::';
const baseUri = url.format({
protocol: isSecure ? 'https' : 'http',
hostname: address,
port,
pathname: '/',
});
// The base URI provides a suitable value for development. Production should
// configure this.
const allowedOrigin = envArray(process.env.ALLOWED_ORIGIN, baseUri.replace(/\/$/, ''), ',');
const config = {
bind: {
port,
address,
},
ssl: {
isSecure,
key: process.env.HTTPS_KEY,
cert: process.env.HTTPS_CRT,
},
baseUri,
redirectUri: process.env.REDIRECT_URI || process.env.INFOSITE,
cors: {
allowedOrigin,
},
persistence: {
dir: process.env.PERSISTENCE_DIR || './private',
},
services: {
github: {
baseUri: process.env.GITHUB_URL || 'https://api.github.com',
debug: {
enabled: envFlag(process.env.GITHUB_DEBUG_ENABLED, false),
intervalSeconds: process.env.GITHUB_DEBUG_INTERVAL_SECONDS || 300,
},
},
},
font: {
path: process.env.FONT_PATH || defaults.font.path,
fallbackPath: process.env.FALLBACK_FONT_PATH,
},
profiling: {
makeBadge: envFlag(process.env.PROFILE_MAKE_BADGE),
},
};
if (config.font.fallbackPath) {
console.log('FALLBACK_FONT_PATH is deprecated. Please use FONT_PATH.');
}
module.exports = config;