2020-08-31 06:04:57 -04:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
2021-09-28 12:16:51 -04:00
|
|
|
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
|
|
|
|
2017-01-09 00:50:51 -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
|
|
|
}
|
|
|
|
|
2020-08-31 06:04:57 -04: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());
|
2015-11-05 12:10:55 -05:00
|
|
|
|
2020-08-31 06:04:57 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-31 06:04:57 -04: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());
|
|
|
|
|
2020-08-31 06:04:57 -04:00
|
|
|
c.bench_function("bench_decode", |b| {
|
|
|
|
b.iter(|| {
|
2021-09-28 12:16:51 -04:00
|
|
|
decode::<Claims>(
|
|
|
|
black_box(token),
|
|
|
|
black_box(&key),
|
|
|
|
black_box(&Validation::new(Algorithm::HS256)),
|
|
|
|
)
|
2020-08-31 06:04:57 -04:00
|
|
|
})
|
|
|
|
});
|
2015-11-02 16:15:45 -05:00
|
|
|
}
|
2020-08-31 06:04:57 -04:00
|
|
|
|
|
|
|
criterion_group!(benches, bench_encode, bench_decode);
|
|
|
|
criterion_main!(benches);
|