|
| 1 | +// #![feature(custom_derive)] |
| 2 | +// #![feature(custom_attribute)] |
| 3 | +#![feature(test)] |
| 4 | +// #![feature(plugin)] |
| 5 | +// #![plugin(serde_macros)] |
| 6 | + |
| 7 | +// for the Country enum |
| 8 | +// #![recursion_limit="259"] |
| 9 | + |
| 10 | +extern crate json; |
| 11 | +extern crate test; |
| 12 | +// extern crate serde; |
| 13 | +// extern crate serde_json; |
| 14 | +// extern crate rustc_serialize; |
| 15 | +// extern crate num_traits; |
| 16 | + |
| 17 | +#[macro_use] |
| 18 | +// mod macros; |
| 19 | + |
| 20 | +use test::Bencher; |
| 21 | + |
| 22 | +// #[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, Deserialize)] |
| 23 | +// struct Http { |
| 24 | +// protocol: HttpProtocol, |
| 25 | +// status: u32, |
| 26 | +// host_status: u32, |
| 27 | +// up_status: u32, |
| 28 | +// method: HttpMethod, |
| 29 | +// content_type: String, |
| 30 | +// user_agent: String, |
| 31 | +// referer: String, |
| 32 | +// request_uri: String, |
| 33 | +// } |
| 34 | + |
| 35 | +// c_enum!(HttpProtocol { |
| 36 | +// HTTP_PROTOCOL_UNKNOWN, |
| 37 | +// HTTP10, |
| 38 | +// HTTP11, |
| 39 | +// }); |
| 40 | + |
| 41 | +// c_enum!(HttpMethod { |
| 42 | +// METHOD_UNKNOWN, |
| 43 | +// GET, |
| 44 | +// POST, |
| 45 | +// DELETE, |
| 46 | +// PUT, |
| 47 | +// HEAD, |
| 48 | +// PURGE, |
| 49 | +// OPTIONS, |
| 50 | +// PROPFIND, |
| 51 | +// MKCOL, |
| 52 | +// PATCH, |
| 53 | +// }); |
| 54 | + |
| 55 | +// c_enum!(CacheStatus { |
| 56 | +// CACHESTATUS_UNKNOWN, |
| 57 | +// Miss, |
| 58 | +// Expired, |
| 59 | +// Hit, |
| 60 | +// }); |
| 61 | + |
| 62 | +// #[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, Deserialize)] |
| 63 | +// struct Origin { |
| 64 | +// ip: String, |
| 65 | +// port: u32, |
| 66 | +// hostname: String, |
| 67 | +// protocol: OriginProtocol, |
| 68 | +// } |
| 69 | + |
| 70 | +// c_enum!(OriginProtocol { |
| 71 | +// ORIGIN_PROTOCOL_UNKNOWN, |
| 72 | +// HTTP, |
| 73 | +// HTTPS, |
| 74 | +// }); |
| 75 | + |
| 76 | +// c_enum!(ZonePlan { |
| 77 | +// ZONEPLAN_UNKNOWN, |
| 78 | +// FREE, |
| 79 | +// PRO, |
| 80 | +// BIZ, |
| 81 | +// ENT, |
| 82 | +// }); |
| 83 | + |
| 84 | +// c_enum!(Country { |
| 85 | +// UNKNOWN, |
| 86 | +// A1, A2, O1, AD, AE, AF, AG, AI, AL, AM, AO, AP, AQ, AR, AS, AT, AU, AW, AX, |
| 87 | +// AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, BQ, BR, BS, BT, BV, |
| 88 | +// BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, |
| 89 | +// CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, EU, FI, FJ, |
| 90 | +// FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, GQ, GR, GS, |
| 91 | +// GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, |
| 92 | +// IT, JE, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, |
| 93 | +// LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, MM, MN, |
| 94 | +// MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, |
| 95 | +// NO, NP, NR, NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, |
| 96 | +// PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, SM, |
| 97 | +// SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, |
| 98 | +// TO, TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, |
| 99 | +// WF, WS, XX, YE, YT, ZA, ZM, ZW, |
| 100 | +// }); |
| 101 | + |
| 102 | +// #[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, Deserialize)] |
| 103 | +// struct Log { |
| 104 | +// timestamp: i64, |
| 105 | +// zone_id: u32, |
| 106 | +// zone_plan: ZonePlan, |
| 107 | +// http: Http, |
| 108 | +// origin: Origin, |
| 109 | +// country: Country, |
| 110 | +// cache_status: CacheStatus, |
| 111 | +// server_ip: String, |
| 112 | +// server_name: String, |
| 113 | +// remote_ip: String, |
| 114 | +// bytes_dlv: u64, |
| 115 | +// ray_id: String, |
| 116 | +// } |
| 117 | + |
| 118 | +const JSON_STR: &'static str = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":1,"http":{"protocol":2,"status":200,"host_status":503,"up_status":520,"method":1,"content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":2},"country":238,"cache_status":3,"server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#; |
| 119 | + |
| 120 | +const JSON_FLOAT_STR: &'static str = r#"[[-65.613616999999977,43.420273000000009],[-65.619720000000029,43.418052999999986],[-65.625,43.421379000000059],[-65.636123999999882,43.449714999999969],[-65.633056999999951,43.474709000000132],[-65.611389000000031,43.513054000000068],[-65.605835000000013,43.516105999999979],[-65.598343,43.515830999999935],[-65.566101000000003,43.508331000000055],[-65.561935000000005,43.504439999999988],[-65.55999799999995,43.499718000000087],[-65.573333999999988,43.476379000000065],[-65.593612999999948,43.444153000000028],[-65.613616999999977,43.420273000000009],[-59.816947999999911,43.928328999999962],[-59.841667000000029,43.918602000000021],[-59.866393999999957,43.909987999999998],[-59.879722999999956,43.906654000000003],[-59.895835999999974,43.904160000000047]]"#; |
| 121 | + |
| 122 | +// #[bench] |
| 123 | +// fn rustc_serialize_parse(b: &mut Bencher) { |
| 124 | +// b.bytes = JSON_STR.len() as u64; |
| 125 | + |
| 126 | +// b.iter(|| { |
| 127 | +// rustc_serialize::json::Json::from_str(JSON_STR).unwrap() |
| 128 | +// }); |
| 129 | +// } |
| 130 | + |
| 131 | +// #[bench] |
| 132 | +// fn rustc_serialize_stringify(b: &mut Bencher) { |
| 133 | +// let data = rustc_serialize::json::Json::from_str(JSON_STR).unwrap(); |
| 134 | + |
| 135 | +// b.bytes = rustc_serialize::json::encode(&data).unwrap().len() as u64; |
| 136 | + |
| 137 | +// b.iter(|| { |
| 138 | +// rustc_serialize::json::encode(&data).unwrap(); |
| 139 | +// }) |
| 140 | +// } |
| 141 | + |
| 142 | +// #[bench] |
| 143 | +// fn rustc_serialize_struct_parse(b: &mut Bencher) { |
| 144 | +// use rustc_serialize::json::Json; |
| 145 | + |
| 146 | +// b.bytes = JSON_STR.len() as u64; |
| 147 | + |
| 148 | +// b.iter(|| { |
| 149 | +// let json = Json::from_str(JSON_STR).unwrap(); |
| 150 | +// let mut decoder = rustc_serialize::json::Decoder::new(json); |
| 151 | +// let log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); |
| 152 | +// log |
| 153 | +// }); |
| 154 | +// } |
| 155 | + |
| 156 | +// #[bench] |
| 157 | +// fn rustc_serialize_struct_stringify(b: &mut Bencher) { |
| 158 | +// use rustc_serialize::json::Json; |
| 159 | + |
| 160 | +// b.bytes = JSON_STR.len() as u64; |
| 161 | + |
| 162 | +// let json = Json::from_str(JSON_STR).unwrap(); |
| 163 | +// let mut decoder = rustc_serialize::json::Decoder::new(json); |
| 164 | +// let log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); |
| 165 | + |
| 166 | +// b.iter(|| { |
| 167 | +// rustc_serialize::json::encode(&log).unwrap(); |
| 168 | +// }) |
| 169 | +// } |
| 170 | + |
| 171 | +// #[bench] |
| 172 | +// fn serde_json_parse(b: &mut Bencher) { |
| 173 | +// b.bytes = JSON_STR.len() as u64; |
| 174 | + |
| 175 | +// b.iter(|| { |
| 176 | +// serde_json::from_str::<serde_json::Value>(JSON_STR).unwrap(); |
| 177 | +// }); |
| 178 | +// } |
| 179 | + |
| 180 | +// #[bench] |
| 181 | +// fn serde_json_stringify(b: &mut Bencher) { |
| 182 | +// let data = serde_json::from_str::<serde_json::Value>(JSON_STR).unwrap(); |
| 183 | + |
| 184 | +// b.bytes = serde_json::to_string(&data).unwrap().len() as u64; |
| 185 | + |
| 186 | +// b.iter(|| { |
| 187 | +// serde_json::to_string(&data).unwrap(); |
| 188 | +// }) |
| 189 | +// } |
| 190 | + |
| 191 | +// #[bench] |
| 192 | +// fn serde_json_floats_parse(b: &mut Bencher) { |
| 193 | +// b.bytes = JSON_FLOAT_STR.len() as u64; |
| 194 | + |
| 195 | +// b.iter(|| { |
| 196 | +// serde_json::from_str::<serde_json::Value>(JSON_FLOAT_STR).unwrap(); |
| 197 | +// }); |
| 198 | +// } |
| 199 | + |
| 200 | +// #[bench] |
| 201 | +// fn serde_json_floats_stringify(b: &mut Bencher) { |
| 202 | +// let data = serde_json::from_str::<serde_json::Value>(JSON_FLOAT_STR).unwrap(); |
| 203 | + |
| 204 | +// b.bytes = serde_json::to_string(&data).unwrap().len() as u64; |
| 205 | + |
| 206 | +// b.iter(|| { |
| 207 | +// serde_json::to_string(&data).unwrap(); |
| 208 | +// }) |
| 209 | +// } |
| 210 | + |
| 211 | +// #[bench] |
| 212 | +// fn serde_json_struct_parse(b: &mut Bencher) { |
| 213 | +// b.bytes = JSON_STR.len() as u64; |
| 214 | + |
| 215 | +// b.iter(|| { |
| 216 | +// serde_json::from_str::<Log>(JSON_STR).unwrap(); |
| 217 | +// }); |
| 218 | +// } |
| 219 | + |
| 220 | +// #[bench] |
| 221 | +// fn serde_json_struct_stringify(b: &mut Bencher) { |
| 222 | +// b.bytes = JSON_STR.len() as u64; |
| 223 | + |
| 224 | +// let data = serde_json::from_str::<Log>(JSON_STR).unwrap(); |
| 225 | + |
| 226 | +// b.iter(|| { |
| 227 | +// serde_json::to_string(&data).unwrap(); |
| 228 | +// }); |
| 229 | +// } |
| 230 | + |
| 231 | +#[bench] |
| 232 | +fn json_rust_parse(b: &mut Bencher) { |
| 233 | + b.bytes = JSON_STR.len() as u64; |
| 234 | + |
| 235 | + b.iter(|| { |
| 236 | + json::parse(JSON_STR).unwrap(); |
| 237 | + }); |
| 238 | +} |
| 239 | + |
| 240 | +#[bench] |
| 241 | +fn json_rust_parse_floats(b: &mut Bencher) { |
| 242 | + b.bytes = JSON_FLOAT_STR.len() as u64; |
| 243 | + |
| 244 | + b.iter(|| { |
| 245 | + json::parse(JSON_FLOAT_STR).unwrap(); |
| 246 | + }); |
| 247 | +} |
| 248 | + |
| 249 | +#[bench] |
| 250 | +fn json_rust_stringify(b: &mut Bencher) { |
| 251 | + let data = json::parse(JSON_STR).unwrap(); |
| 252 | + |
| 253 | + b.bytes = data.dump().len() as u64; |
| 254 | + |
| 255 | + b.iter(|| { |
| 256 | + data.dump(); |
| 257 | + }) |
| 258 | +} |
| 259 | + |
| 260 | +#[bench] |
| 261 | +fn json_rust_stringify_io_write(b: &mut Bencher) { |
| 262 | + let data = json::parse(JSON_STR).unwrap(); |
| 263 | + |
| 264 | + b.bytes = data.dump().len() as u64; |
| 265 | + |
| 266 | + let mut target = Vec::new(); |
| 267 | + |
| 268 | + b.iter(|| { |
| 269 | + data.to_writer(&mut target); |
| 270 | + }) |
| 271 | +} |
| 272 | + |
| 273 | +#[bench] |
| 274 | +fn json_rust_stringify_floats(b: &mut Bencher) { |
| 275 | + let data = json::parse(JSON_FLOAT_STR).unwrap(); |
| 276 | + |
| 277 | + b.bytes = data.dump().len() as u64; |
| 278 | + |
| 279 | + b.iter(|| { |
| 280 | + data.dump(); |
| 281 | + }) |
| 282 | +} |
| 283 | + |
| 284 | +#[bench] |
| 285 | +fn json_rust_stringify_floats_io_write(b: &mut Bencher) { |
| 286 | + let data = json::parse(JSON_FLOAT_STR).unwrap(); |
| 287 | + |
| 288 | + b.bytes = data.dump().len() as u64; |
| 289 | + |
| 290 | + let mut target = Vec::new(); |
| 291 | + |
| 292 | + b.iter(|| { |
| 293 | + data.to_writer(&mut target); |
| 294 | + }) |
| 295 | +} |
0 commit comments