-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsi.js
140 lines (121 loc) · 3.07 KB
/
clsi.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
import crypto from 'crypto'
import http from 'http'
import { hrtime } from 'process'
import { promisify } from 'util'
import express from 'express'
import pino from 'pino'
import pinoHttp from 'pino-http'
import { collectDefaultMetrics, register, Summary } from 'prom-client'
const app = express()
//
// Utilties
//
// Wrap an async function so we can use it with express.
function expressify(fn) {
return (req, res, next) => {
fn(req, res, next).catch(next)
}
}
//
// Logging
//
let pinoOptions
if (process.env.NODE_ENV === 'production')
pinoOptions = {
transport: {
target: 'pino-google-cloud-format-transport',
},
}
const logger = pino(pinoOptions)
app.use(
pinoHttp({
logger,
quietReqLogger: true,
})
)
//
// Health Checks
//
app.get('/livez', (req, res) => {
res.sendStatus(200)
})
app.get(
'/readyz',
expressify(async (req, res) => {
try {
await compile('test doc'.repeat(80))
res.sendStatus(200)
} catch (err) {
res.sendStatus(500)
}
})
)
//
// Prometheus Metrics
//
collectDefaultMetrics()
const compileTimeMetric = new Summary({
name: 'compile_time',
help: 'end to end compile time',
})
app.get(
'/metrics',
expressify(async (req, res) => {
res.set('Content-Type', register.contentType)
const metrics = await register.metrics()
res.end(metrics)
})
)
//
// Server Setup
//
const server = http.createServer(app)
const port = parseInt(process.env.PORT || 8081, 10)
server.listen(port, () => logger.info({ port }, 'up'))
process.on('SIGTERM', () => server.close())
app.use(express.json())
//
// Application Code
//
app.post(
'/compile',
expressify(async (req, res) => {
let { doc, compiler } = req.body
compiler ||= 'pdftex'
req.log.info({ compiler, docSize: doc.length }, 'compile starting')
const stopTimer = compileTimeMetric.startTimer()
const output = await compile(doc)
stopTimer()
res.json({ output })
})
)
// simulate the work of a compile
const pbkdf2 = promisify(crypto.pbkdf2)
async function compile(doc) {
const iterations = parseFloat(process.env.COMPILE_ITERATIONS || 10000)
const workMs = findWorkMs(doc.length)
const endNs = hrtime.bigint() + BigInt(workMs * 1e6)
let key = Buffer.from(doc.slice(0, 16))
do {
key = await pbkdf2(key, 'salt', iterations, 16, 'md5')
} while (hrtime.bigint() < endNs)
return key.toString('hex')
}
// Decide how long the simulated compile should take, based on the
// length of the doc and a randomized work rate (ms/char).
// See https://en.wikipedia.org/wiki/Log-normal_distribution
function findWorkMs(docLength) {
// ms per character of doc input
const m = parseFloat(process.env.COMPILE_WORK_RATE || 5)
const s = parseFloat(process.env.COMPILE_WORK_SD || 1.2)
const mu = Math.log(m*m / Math.sqrt(m*m + s*s))
const sigma = Math.log(1 + s*s / m*m)
const r = mu + randomNormal() * sigma
return Math.round(Math.exp(r) * docLength)
}
// Result is roughly normal on [-1, 1]. Not very efficient.
function randomNormal(n = 50) {
let x = 0
for (let i = 0; i < n; ++i) x += Math.random()
return (x / n) * 2 - 1
}