Skip to content

Commit

Permalink
refactor(benchmark): migrate to Deno.bench
Browse files Browse the repository at this point in the history
  • Loading branch information
aykxt committed Jul 14, 2023
1 parent a04b867 commit ea25f83
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 308 deletions.
109 changes: 29 additions & 80 deletions benchmarks/aes.ts
Original file line number Diff line number Diff line change
@@ -1,182 +1,131 @@
import { bench, runBenchmarks } from "../dev_deps.ts";
import { AES as GodCryptoAES } from "https://deno.land/x/god_crypto@v1.4.11/aes.ts";
import { Aes } from "../aes.ts";
import { Cbc, Cfb, Ctr, Ecb, Ofb } from "../block-modes.ts";
import { AES as GodCryptoAES } from "https://deno.land/x/god_crypto@v1.4.9/aes.ts";
import { args } from "./utils/benchmarkArgs.ts";

const { runs: _runs, ...opts } = args;
const runs = _runs || 25;

const key = new Uint8Array(16);
const iv = new Uint8Array(Aes.BLOCK_SIZE);
const data = new Uint8Array(1024 * 1024 * 2);

bench({
Deno.bench({
name: "AES-128-ECB 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Ecb(Aes, key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-ECB 2MiB Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ecb(Aes, key);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CBC 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Cbc(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CBC 2MiB Decrypt",
runs,
func(b) {
fn() {
const cipher = new Cbc(Aes, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CFB 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Cfb(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CFB 2MiB Decrypt",
runs,
func(b) {
fn() {
const cipher = new Cfb(Aes, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-OFB 2MiB Encrypt/Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ofb(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CTR 2MiB Encrypt/Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ctr(Aes, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-ECB (GodCrypto) 2MiB Encrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, { mode: "ecb" });
b.start();
await cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-ECB (GodCrypto) 2MiB Decrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, { mode: "ecb" });
b.start();
await cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CBC (GodCrypto) 2MiB Encrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, {
mode: "cbc",
iv,
});
b.start();
await cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CBC (GodCrypto) 2MiB Decrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, {
mode: "cbc",
iv,
});
b.start();
await cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CFB (GodCrypto) 2MiB Encrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, {
mode: "cfb",
iv,
});
b.start();
await cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "AES-128-CFB (GodCrypto) 2MiB Decrypt",
runs,
async func(b) {
async fn() {
const cipher = new GodCryptoAES(key, {
mode: "cfb",
iv,
});
b.start();
await cipher.decrypt(data);
b.stop();
},
});

if (import.meta.main) {
runBenchmarks(opts);
}
6 changes: 0 additions & 6 deletions benchmarks/all.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { runBenchmarks } from "../dev_deps.ts";
import { args } from "./utils/benchmarkArgs.ts";
import "./aes.ts";
import "./blowfish.ts";
import "./cast5.ts";
import "./des.ts";
import "./tdes.ts";

const { runs: _, ...opts } = args;

runBenchmarks(opts);
76 changes: 19 additions & 57 deletions benchmarks/blowfish.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,79 @@
import { bench, runBenchmarks } from "../dev_deps.ts";
import { Blowfish } from "../blowfish.ts";
import { Cbc, Cfb, Ctr, Ecb, Ofb } from "../block-modes.ts";
import { args } from "./utils/benchmarkArgs.ts";

const { runs: _runs, ...opts } = args;
const runs = _runs || 25;
import { Blowfish } from "../blowfish.ts";

const key = new Uint8Array(8);
const iv = new Uint8Array(Blowfish.BLOCK_SIZE);
const data = new Uint8Array(1024 * 1024 * 2);

bench({
Deno.bench({
name: "Blowfish-ECB 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-ECB 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-ECB 2MiB Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ecb(Blowfish, key);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-CBC 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Cbc(Blowfish, key, iv);

b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-CBC 2MiB Decrypt",
runs,
func(b) {
fn() {
const bf = new Cbc(Blowfish, key, iv);
b.start();
bf.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-CFB 2MiB Encrypt",
runs,
func(b) {
fn() {
const cipher = new Cfb(Blowfish, key, iv);

b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-CFB 2MiB Decrypt",
runs,
func(b) {
fn() {
const cipher = new Cfb(Blowfish, key, iv);
b.start();
cipher.decrypt(data);
b.stop();
},
});

bench({
Deno.bench({
// Encryption and decryption are the same
name: "Blowfish-OFB 2MiB Encrypt/Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ofb(Blowfish, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

bench({
Deno.bench({
name: "Blowfish-CTR 2MiB Encrypt/Decrypt",
runs,
func(b) {
fn() {
const cipher = new Ctr(Blowfish, key, iv);
b.start();
cipher.encrypt(data);
b.stop();
},
});

if (import.meta.main) {
runBenchmarks(opts);
}
Loading

0 comments on commit ea25f83

Please sign in to comment.