jsonwebtoken/benches/jwt.rs

37 lines
1.2 KiB
Rust
Raw Permalink Normal View History

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
2019-11-14 13:43:43 -05:00
use serde::{Deserialize, Serialize};
2015-11-02 16:15:45 -05:00
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
2015-11-02 16:15:45 -05:00
struct Claims {
sub: String,
2018-10-28 14:58:35 -04:00
company: String,
2015-11-02 16:15:45 -05:00
}
fn bench_encode(c: &mut Criterion) {
2018-10-28 14:58:35 -04:00
let claim = Claims { sub: "b@b.com".to_owned(), company: "ACME".to_owned() };
2019-12-29 15:50:06 -05:00
let key = EncodingKey::from_secret("secret".as_ref());
c.bench_function("bench_encode", |b| {
b.iter(|| encode(black_box(&Header::default()), black_box(&claim), black_box(&key)))
});
2015-11-02 16:15:45 -05:00
}
fn bench_decode(c: &mut Criterion) {
2015-11-05 13:09:39 -05:00
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
2019-12-29 15:50:06 -05:00
let key = DecodingKey::from_secret("secret".as_ref());
c.bench_function("bench_decode", |b| {
b.iter(|| {
decode::<Claims>(
black_box(token),
black_box(&key),
black_box(&Validation::new(Algorithm::HS256)),
)
})
});
2015-11-02 16:15:45 -05:00
}
criterion_group!(benches, bench_encode, bench_decode);
criterion_main!(benches);