-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update md5 to cyrb, add stats to homepage
- Loading branch information
1 parent
133fd84
commit afec1cc
Showing
11 changed files
with
126 additions
and
249 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Benchmark hashing algorithms | ||
// import { xxHash32 } from 'js-xxhash'; | ||
import nodeCrypto from 'node:crypto'; | ||
import { cyrb53 } from '../server/hash.ts'; | ||
|
||
const test = 'test'.repeat(100000); | ||
|
||
// Note: due to VM optimization the later functions run faster | ||
// Need to execute in separate processes for accurate testing | ||
const jobs = { | ||
cyrb: () => cyrb53(test).toString(16), | ||
// xxhash: () => xxHash32(test).toString(16), | ||
// md5js: () => MD5.hash(test), | ||
// Works only in node | ||
md5node: () => nodeCrypto.createHash('md5').update(test).digest('hex'), | ||
// requires https in browser, also Buffer API to convert not available | ||
sha1: async () => Buffer.from(await crypto.subtle.digest('sha-1', Buffer.from(test))).toString('hex'), | ||
} | ||
|
||
console.time(process.argv[2]); | ||
console.log(await jobs[process.argv[2]]()); | ||
console.timeEnd(process.argv[2]); |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
tsx hash.mts cyrb | ||
tsx hash.mts xxhash | ||
tsx hash.mts md5js | ||
tsx hash.mts md5node | ||
tsx hash.mts sha1 |
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { gunzipSync } from 'zlib'; | ||
import fs from 'fs'; | ||
import config from './config'; | ||
|
||
let qs = 0; | ||
let eps = 0; | ||
// On boot, start with the initial data included in repo | ||
console.time('load'); | ||
let jData = JSON.parse(gunzipSync(fs.readFileSync('./jeopardy.json.gz')).toString()); | ||
updateJDataStats(); | ||
console.timeEnd('load'); | ||
console.log('loaded %d episodes', Object.keys(jData).length); | ||
let etag: string | null = null; | ||
|
||
// Periodically refetch the latest episode data and replace it in memory | ||
setInterval(refreshEpisodes, 24 * 60 * 60 * 1000); | ||
refreshEpisodes(); | ||
|
||
async function refreshEpisodes() { | ||
if (config.NODE_ENV === 'development') { | ||
return; | ||
} | ||
console.time('reload'); | ||
try { | ||
const response = await fetch( | ||
'https://github.com/howardchung/j-archive-parser/raw/release/jeopardy.json.gz', | ||
); | ||
const newEtag = response.headers.get('etag'); | ||
console.log(newEtag, etag); | ||
if (newEtag !== etag) { | ||
const arrayBuf = await response.arrayBuffer(); | ||
const buf = Buffer.from(arrayBuf); | ||
jData = JSON.parse(gunzipSync(buf).toString()); | ||
updateJDataStats(); | ||
etag = newEtag; | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
console.timeEnd('reload'); | ||
} | ||
|
||
function updateJDataStats() { | ||
console.time('count'); | ||
qs = 0; | ||
eps = 0; | ||
Object.keys(jData).forEach(key => { | ||
eps += 1; | ||
qs += jData[key].jeopardy.length; | ||
qs += jData[key].double.length; | ||
qs + jData[key].final.length; | ||
}); | ||
console.timeEnd('count'); | ||
} | ||
|
||
export function getJData() { | ||
return jData; | ||
} | ||
|
||
export function getJDataStats() { | ||
return { qs, eps }; | ||
} |
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
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
Oops, something went wrong.