swc/crates/swc_common/benches/serde.rs
Donny/강동윤 9b76783281
refactor(common): Cleanup & rustfmt (#3495)
swc_common:
 - Merge identical source codes.

swc_css_parser:
 - Deny `clippy::all`.
 - Fix lints.
2022-02-09 06:33:32 +00:00

105 lines
1.9 KiB
Rust

#![feature(test)]
#![feature(bench_black_box)]
extern crate test;
use std::hint::black_box;
use ast_node::ast_node;
use serde::{Deserialize, Serialize};
use swc_common::{Span, DUMMY_SP};
use test::Bencher;
#[derive(Serialize, Deserialize)]
pub struct SerdeStr {
span: Span,
value: String,
}
#[ast_node("String")]
pub struct Str {
span: Span,
value: String,
}
#[derive(Serialize, Deserialize)]
pub struct SerdeNum {
span: Span,
value: u64,
}
#[ast_node("Number")]
pub struct Num {
span: Span,
value: u64,
}
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Serde {
Number(SerdeNum),
String(SerdeStr),
}
#[ast_node]
pub enum AstNode {
#[tag("Number")]
Number(Num),
#[tag("String")]
String(Str),
}
#[bench]
fn ser_serde(b: &mut Bencher) {
let src = Serde::String(SerdeStr {
span: DUMMY_SP,
value: String::from("perf-diff"),
});
b.iter(|| black_box(serde_json::to_string(&src).unwrap()));
}
#[bench]
fn de_serde(b: &mut Bencher) {
let src = serde_json::to_string(&Serde::String(SerdeStr {
span: DUMMY_SP,
value: String::from("perf-diff"),
}))
.unwrap();
println!("{}", src);
b.bytes = src.len() as _;
b.iter(|| {
let t: Serde = serde_json::from_str(&src).unwrap();
black_box(t);
});
}
#[bench]
fn ser_ast_node(b: &mut Bencher) {
let src = AstNode::String(Str {
span: DUMMY_SP,
value: String::from("perf-diff"),
});
b.iter(|| black_box(serde_json::to_string(&src).unwrap()));
}
#[bench]
fn de_ast_node(b: &mut Bencher) {
let src = serde_json::to_string(&AstNode::String(Str {
span: DUMMY_SP,
value: String::from("perf-diff"),
}))
.unwrap();
println!("{}", src);
b.bytes = src.len() as _;
b.iter(|| {
let t: AstNode = serde_json::from_str(&src).unwrap();
black_box(t);
});
}