Skip to content

Commit 2e887d1

Browse files
committed
Add examples/fastpkbdf2_bench.
This allows for performance comparisons between *ring* and other implementations. I think the benchmark is too noisy. Unfortunately, |cargo bench| doesn't work well for measuring this because |cargo bench| insists on doing a huge number of iterations, which means that each run takes too long in a edit-compile-measure cycle.
1 parent cb782e0 commit 2e887d1

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ libc = "0.1"
1616

1717
# TODO: [dev-dependencies]
1818
rustc-serialize = "0.3.15"
19+
time = "0.1.32"
1920

2021
[profile.release]
2122
opt-level = 3

examples/fastpbkdf2_bench.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2015 Brian Smith.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND AND THE AUTHORS DISCLAIM ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10+
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12+
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
// Derived from the CC0-licensed implementation by Joseph Birr-Pixton at
16+
// https://github.com/ctz/rust-fastpbkdf2/blob/master/pbkdf2-bench/src/main.rs
17+
// commit b000c72c7b3beb0bee761bebcf07ac3f0875d1ad. This version only measures
18+
// *ring*.
19+
20+
extern crate ring;
21+
extern crate time;
22+
23+
use ring::pbkdf2;
24+
use time::SteadyTime;
25+
26+
const ITERATIONS: usize = 1 << 20;
27+
const PASSWORD: &'static [u8] = b"password";
28+
const SALT: &'static [u8] = b"salt";
29+
30+
fn bench<F>(name: &'static str, f: F) where F: FnOnce() {
31+
let start = SteadyTime::now();
32+
f();
33+
let duration = SteadyTime::now() - start;
34+
println!("{} = {}ms", name, duration.num_milliseconds());
35+
}
36+
37+
fn ring_sha1() {
38+
let mut out = [0u8; 20];
39+
pbkdf2::derive(&pbkdf2::HMAC_SHA1, ITERATIONS, PASSWORD, SALT, &mut out);
40+
}
41+
42+
fn ring_sha256() {
43+
let mut out = [0u8; 32];
44+
pbkdf2::derive(&pbkdf2::HMAC_SHA256, ITERATIONS, PASSWORD, SALT, &mut out);
45+
}
46+
47+
fn ring_sha512() {
48+
let mut out = [0u8; 64];
49+
pbkdf2::derive(&pbkdf2::HMAC_SHA512, ITERATIONS, PASSWORD, SALT, &mut out);
50+
}
51+
52+
fn main() {
53+
bench("ring-sha1", ring_sha1);
54+
bench("ring-sha256", ring_sha256);
55+
bench("ring-sha512", ring_sha512);
56+
}

0 commit comments

Comments
 (0)