mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-30 21:23:29 +03:00
feat: upload pow performance benchmarks
This commit is contained in:
parent
d8a145cf87
commit
1c4ee5b622
3 changed files with 51 additions and 5 deletions
|
@ -63,6 +63,8 @@ export const solveCaptchaRunner = async (e: Event): Promise<void> => {
|
||||||
string: config.string,
|
string: config.string,
|
||||||
nonce: resp.work.nonce,
|
nonce: resp.work.nonce,
|
||||||
result: resp.work.result,
|
result: resp.work.result,
|
||||||
|
time: resp.work.time,
|
||||||
|
worker_type: resp.work.worker_type,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 3. submit work
|
// 3. submit work
|
||||||
|
|
|
@ -11,31 +11,66 @@
|
||||||
|
|
||||||
import { gen_pow } from "@mcaptcha/pow-wasm";
|
import { gen_pow } from "@mcaptcha/pow-wasm";
|
||||||
import * as p from "@mcaptcha/pow_sha256-polyfill";
|
import * as p from "@mcaptcha/pow_sha256-polyfill";
|
||||||
import { WasmWork, PoWConfig } from "./types";
|
import { WasmWork, PoWConfig, SubmitWork } from "./types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* proove work
|
* proove work
|
||||||
* @param {PoWConfig} config - the proof-of-work configuration using which
|
* @param {PoWConfig} config - the proof-of-work configuration using which
|
||||||
* work needs to be computed
|
* work needs to be computed
|
||||||
* */
|
* */
|
||||||
const prove = async (config: PoWConfig): Promise<WasmWork> => {
|
const prove = async (config: PoWConfig): Promise<SubmitWork> => {
|
||||||
let proof: WasmWork = null;
|
const WASM = "wasm";
|
||||||
|
const JS = "js";
|
||||||
if (WasmSupported) {
|
if (WasmSupported) {
|
||||||
|
let proof: WasmWork = null;
|
||||||
|
let res: SubmitWork = null;
|
||||||
|
let time: number = null;
|
||||||
|
|
||||||
|
const t0 = performance.now();
|
||||||
const proofString = gen_pow(
|
const proofString = gen_pow(
|
||||||
config.salt,
|
config.salt,
|
||||||
config.string,
|
config.string,
|
||||||
config.difficulty_factor
|
config.difficulty_factor
|
||||||
);
|
);
|
||||||
|
const t1 = performance.now();
|
||||||
|
time = t1 - t0;
|
||||||
|
|
||||||
proof = JSON.parse(proofString);
|
proof = JSON.parse(proofString);
|
||||||
|
const worker_type = WASM;
|
||||||
|
res = {
|
||||||
|
result: proof.result,
|
||||||
|
nonce: proof.nonce,
|
||||||
|
worker_type,
|
||||||
|
time,
|
||||||
|
};
|
||||||
|
return res;
|
||||||
} else {
|
} else {
|
||||||
console.log("WASM unsupported, expect delay during proof generation");
|
console.log("WASM unsupported, expect delay during proof generation");
|
||||||
|
|
||||||
|
let proof: WasmWork = null;
|
||||||
|
let time: number = null;
|
||||||
|
let res: SubmitWork = null;
|
||||||
|
|
||||||
|
const t0 = performance.now();
|
||||||
|
|
||||||
proof = await p.generate_work(
|
proof = await p.generate_work(
|
||||||
config.salt,
|
config.salt,
|
||||||
config.string,
|
config.string,
|
||||||
config.difficulty_factor
|
config.difficulty_factor
|
||||||
);
|
);
|
||||||
|
const t1 = performance.now();
|
||||||
|
time = t1 - t0;
|
||||||
|
|
||||||
|
const worker_type = JS;
|
||||||
|
res = {
|
||||||
|
result: proof.result,
|
||||||
|
nonce: proof.nonce,
|
||||||
|
worker_type,
|
||||||
|
time,
|
||||||
|
};
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
return proof;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// credits: @jf-bastien on Stack Overflow
|
// credits: @jf-bastien on Stack Overflow
|
||||||
|
|
|
@ -14,6 +14,15 @@ export type Work = {
|
||||||
nonce: number;
|
nonce: number;
|
||||||
string: string;
|
string: string;
|
||||||
key: string;
|
key: string;
|
||||||
|
time: number;
|
||||||
|
worker_type: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SubmitWork = {
|
||||||
|
time: number;
|
||||||
|
worker_type: string;
|
||||||
|
result: string;
|
||||||
|
nonce: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WasmWork = {
|
export type WasmWork = {
|
||||||
|
@ -22,7 +31,7 @@ export type WasmWork = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ServiceWorkerWork = {
|
export type ServiceWorkerWork = {
|
||||||
work: WasmWork;
|
work: SubmitWork;
|
||||||
duration: number;
|
duration: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue