mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-06 23:59:01 +02:00
f67d5fe006
add latexmk `-time` option to clsi and record performance logs GitOrigin-RevId: 467473859359913da73f83e10b63b45603ea175c
25 lines
960 B
JavaScript
25 lines
960 B
JavaScript
const crypto = require('node:crypto')
|
|
|
|
/**
|
|
* Consistently sample a keyspace with a given sample percentage.
|
|
* The same key will always produce a consistent percentile value that
|
|
* can be compared against the sample percentage.
|
|
* Example: if key is the userId and the samplePercentage is 10, then
|
|
* we see all the activity for the 10% of users who are selected.
|
|
*
|
|
* @param {string} key - The unique identifier to be hashed and checked.
|
|
* @param {number} samplePercentage - The percentage (0-100) of keys that should return true.
|
|
* @returns {boolean} - True if the key is within the sample, false otherwise.
|
|
*/
|
|
function sampleByHash(key, samplePercentage) {
|
|
if (samplePercentage <= 0) {
|
|
return false
|
|
}
|
|
const hash = crypto.createHash('md5').update(key).digest()
|
|
const hashValue = hash.readUInt32BE(0)
|
|
const percentile = Math.floor((hashValue / 0xffffffff) * 100)
|
|
return percentile < samplePercentage
|
|
}
|
|
|
|
module.exports = { sampleByHash }
|