2014-09-06 22:41:13 -05:00
#![ feature(phase, macro_rules) ]
2014-06-20 18:32:30 -05:00
#![ allow(non_camel_case_types) ]
2014-09-06 22:41:13 -05:00
#[ phase(plugin) ]
extern crate serde_macros ;
extern crate serde ;
2014-06-20 18:32:30 -05:00
extern crate serialize ;
extern crate test ;
2014-07-06 01:44:43 -05:00
use std ::io ;
2014-12-01 14:21:18 -06:00
use std ::io ::ByRefWriter ;
2014-06-20 18:32:30 -05:00
use test ::Bencher ;
2014-09-06 22:41:13 -05:00
use serde ::de ;
2014-11-14 21:57:53 -06:00
use serde ::json ::ser ::escape_str ;
2014-09-06 22:41:13 -05:00
use serde ::json ;
2014-11-13 09:54:44 -06:00
use serde ::ser ::Serialize ;
2014-09-06 22:41:13 -05:00
use serde ::ser ;
2014-06-20 18:32:30 -05:00
2014-12-05 18:24:32 -06:00
use serialize ::Encodable ;
2014-12-01 15:09:35 -06:00
#[ deriving(Show, PartialEq, Encodable, Decodable) ]
2014-11-13 09:54:44 -06:00
#[ deriving_serialize ]
#[ deriving_deserialize ]
2014-06-20 18:32:30 -05:00
struct Http {
protocol : HttpProtocol ,
status : u32 ,
host_status : u32 ,
up_status : u32 ,
method : HttpMethod ,
content_type : String ,
user_agent : String ,
referer : String ,
request_uri : String ,
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum HttpProtocol {
HTTP_PROTOCOL_UNKNOWN ,
HTTP10 ,
HTTP11 ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for HttpProtocol {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for HttpProtocol {
fn decode ( d : & mut D ) -> Result < HttpProtocol , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for HttpProtocol {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for HttpProtocol {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < HttpProtocol , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum HttpMethod {
METHOD_UNKNOWN ,
GET ,
POST ,
DELETE ,
PUT ,
HEAD ,
PURGE ,
OPTIONS ,
PROPFIND ,
MKCOL ,
PATCH ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for HttpMethod {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for HttpMethod {
fn decode ( d : & mut D ) -> Result < HttpMethod , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for HttpMethod {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for HttpMethod {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < HttpMethod , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum CacheStatus {
CACHESTATUS_UNKNOWN ,
Miss ,
Expired ,
Hit ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for CacheStatus {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for CacheStatus {
fn decode ( d : & mut D ) -> Result < CacheStatus , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for CacheStatus {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for CacheStatus {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < CacheStatus , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-01 15:09:35 -06:00
#[ deriving(Show, PartialEq, Encodable, Decodable) ]
2014-11-13 09:54:44 -06:00
#[ deriving_serialize ]
#[ deriving_deserialize ]
2014-06-20 18:32:30 -05:00
struct Origin {
ip : String ,
port : u32 ,
hostname : String ,
protocol : OriginProtocol ,
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum OriginProtocol {
ORIGIN_PROTOCOL_UNKNOWN ,
HTTP ,
HTTPS ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for OriginProtocol {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for OriginProtocol {
fn decode ( d : & mut D ) -> Result < OriginProtocol , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for OriginProtocol {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for OriginProtocol {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < OriginProtocol , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum ZonePlan {
ZONEPLAN_UNKNOWN ,
FREE ,
PRO ,
BIZ ,
ENT ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for ZonePlan {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for ZonePlan {
fn decode ( d : & mut D ) -> Result < ZonePlan , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for ZonePlan {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for ZonePlan {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < ZonePlan , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-13 18:55:42 -06:00
#[ deriving(Copy, Show, PartialEq, FromPrimitive) ]
2014-06-20 18:32:30 -05:00
enum Country {
UNKNOWN ,
A1 ,
A2 ,
O1 ,
AD ,
AE ,
AF ,
AG ,
AI ,
AL ,
AM ,
AO ,
AP ,
AQ ,
AR ,
AS ,
AT ,
AU ,
AW ,
AX ,
AZ ,
BA ,
BB ,
BD ,
BE ,
BF ,
BG ,
BH ,
BI ,
BJ ,
BL ,
BM ,
BN ,
BO ,
BQ ,
BR ,
BS ,
BT ,
BV ,
BW ,
BY ,
BZ ,
CA ,
CC ,
CD ,
CF ,
CG ,
CH ,
CI ,
CK ,
CL ,
CM ,
CN ,
CO ,
CR ,
CU ,
CV ,
CW ,
CX ,
CY ,
CZ ,
DE ,
DJ ,
DK ,
DM ,
DO ,
DZ ,
EC ,
EE ,
EG ,
EH ,
ER ,
ES ,
ET ,
EU ,
FI ,
FJ ,
FK ,
FM ,
FO ,
FR ,
GA ,
GB ,
GD ,
GE ,
GF ,
GG ,
GH ,
GI ,
GL ,
GM ,
GN ,
GP ,
GQ ,
GR ,
GS ,
GT ,
GU ,
GW ,
GY ,
HK ,
HM ,
HN ,
HR ,
HT ,
HU ,
ID ,
IE ,
IL ,
IM ,
IN ,
IO ,
IQ ,
IR ,
IS ,
IT ,
JE ,
JM ,
JO ,
JP ,
KE ,
KG ,
KH ,
KI ,
KM ,
KN ,
KP ,
KR ,
KW ,
KY ,
KZ ,
LA ,
LB ,
LC ,
LI ,
LK ,
LR ,
LS ,
LT ,
LU ,
LV ,
LY ,
MA ,
MC ,
MD ,
ME ,
MF ,
MG ,
MH ,
MK ,
ML ,
MM ,
MN ,
MO ,
MP ,
MQ ,
MR ,
MS ,
MT ,
MU ,
MV ,
MW ,
MX ,
MY ,
MZ ,
NA ,
NC ,
NE ,
NF ,
NG ,
NI ,
NL ,
NO ,
NP ,
NR ,
NU ,
NZ ,
OM ,
PA ,
PE ,
PF ,
PG ,
PH ,
PK ,
PL ,
PM ,
PN ,
PR ,
PS ,
PT ,
PW ,
PY ,
QA ,
RE ,
RO ,
RS ,
RU ,
RW ,
SA ,
SB ,
SC ,
SD ,
SE ,
SG ,
SH ,
SI ,
SJ ,
SK ,
SL ,
SM ,
SN ,
SO ,
SR ,
SS ,
ST ,
SV ,
SX ,
SY ,
SZ ,
TC ,
TD ,
TF ,
TG ,
TH ,
TJ ,
TK ,
TL ,
TM ,
TN ,
TO ,
TR ,
TT ,
TV ,
TW ,
TZ ,
UA ,
UG ,
UM ,
US ,
UY ,
UZ ,
VA ,
VC ,
VE ,
VG ,
VI ,
VN ,
VU ,
WF ,
WS ,
XX ,
YE ,
YT ,
ZA ,
ZM ,
ZW ,
}
2014-12-01 15:09:35 -06:00
impl < S : serialize ::Encoder < E > , E > serialize ::Encodable < S , E > for Country {
fn encode ( & self , s : & mut S ) -> Result < ( ) , E > {
( * self as uint ) . encode ( s )
}
}
impl < D : ::serialize ::Decoder < E > , E > serialize ::Decodable < D , E > for Country {
fn decode ( d : & mut D ) -> Result < Country , E > {
match FromPrimitive ::from_uint ( try ! ( d . read_uint ( ) ) ) {
Some ( value ) = > Ok ( value ) ,
None = > Err ( d . error ( " cannot convert from uint " ) ) ,
}
}
}
2014-11-13 09:54:44 -06:00
impl < S : ser ::Serializer < E > , E > ser ::Serialize < S , E > for Country {
2014-06-20 18:32:30 -05:00
#[ inline ]
2014-08-31 22:44:19 -05:00
fn serialize ( & self , s : & mut S ) -> Result < ( ) , E > {
2014-06-20 18:32:30 -05:00
s . serialize_uint ( * self as uint )
}
}
2014-11-13 09:54:44 -06:00
impl < D : de ::Deserializer < E > , E > de ::Deserialize < D , E > for Country {
2014-08-02 18:06:05 -05:00
#[ inline ]
2014-08-19 17:18:47 -05:00
fn deserialize_token ( d : & mut D , token : de ::Token ) -> Result < Country , E > {
2014-08-03 14:34:57 -05:00
d . expect_from_primitive ( token )
2014-08-02 18:06:05 -05:00
}
}
2014-12-01 15:09:35 -06:00
#[ deriving(Show, PartialEq, Encodable, Decodable) ]
2014-11-13 09:54:44 -06:00
#[ deriving_serialize ]
#[ deriving_deserialize ]
2014-06-20 18:32:30 -05:00
struct Log {
timestamp : i64 ,
zone_id : u32 ,
zone_plan : ZonePlan ,
http : Http ,
origin : Origin ,
country : Country ,
cache_status : CacheStatus ,
server_ip : String ,
server_name : String ,
remote_ip : String ,
bytes_dlv : u64 ,
ray_id : String ,
}
2014-07-17 23:28:31 -05:00
impl Log {
fn new ( ) -> Log {
Log {
2014-09-07 03:02:19 -05:00
timestamp : 2837513946597 ,
2014-07-17 23:28:31 -05:00
zone_id : 123456 ,
2014-11-22 18:27:01 -06:00
zone_plan : ZonePlan ::FREE ,
2014-07-17 23:28:31 -05:00
http : Http {
2014-11-22 18:27:01 -06:00
protocol : HttpProtocol ::HTTP11 ,
2014-07-17 23:28:31 -05:00
status : 200 ,
host_status : 503 ,
up_status : 520 ,
2014-11-22 18:27:01 -06:00
method : HttpMethod ::GET ,
2014-07-17 23:28:31 -05:00
content_type : " text/html " . to_string ( ) ,
user_agent : " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36 " . to_string ( ) ,
referer : " https://www.cloudflare.com/ " . to_string ( ) ,
request_uri : " /cdn-cgi/trace " . to_string ( ) ,
} ,
origin : Origin {
ip : " 1.2.3.4 " . to_string ( ) ,
port : 8000 ,
hostname : " www.example.com " . to_string ( ) ,
2014-11-22 18:27:01 -06:00
protocol : OriginProtocol ::HTTPS ,
2014-07-17 23:28:31 -05:00
} ,
2014-11-22 18:27:01 -06:00
country : Country ::US ,
cache_status : CacheStatus ::Hit ,
2014-07-17 23:28:31 -05:00
server_ip : " 192.168.1.1 " . to_string ( ) ,
server_name : " metal.cloudflare.com " . to_string ( ) ,
remote_ip : " 10.1.2.3 " . to_string ( ) ,
bytes_dlv : 123456 ,
ray_id : " 10c73629cce30078-LAX " . to_string ( ) ,
}
}
}
2014-07-09 13:58:18 -05:00
macro_rules ! likely (
( $val :expr ) = > {
{
extern {
#[ link_name = " llvm.expect.i8 " ]
fn expect ( val : u8 , expected_val : u8 ) -> u8 ;
}
let x : bool = $val ;
unsafe { expect ( x as u8 , 1 ) ! = 0 }
}
}
)
macro_rules ! unlikely (
( $val :expr ) = > {
{
extern {
#[ link_name = " llvm.expect.i8 " ]
fn expect ( val : u8 , expected_val : u8 ) -> u8 ;
}
let x : bool = $val ;
unsafe { expect ( x as u8 , 0 ) ! = 0 }
}
}
)
struct MyMemWriter0 {
2014-07-06 01:44:43 -05:00
buf : Vec < u8 > ,
}
2014-07-09 13:58:18 -05:00
impl MyMemWriter0 {
pub fn with_capacity ( cap : uint ) -> MyMemWriter0 {
MyMemWriter0 {
2014-07-06 01:44:43 -05:00
buf : Vec ::with_capacity ( cap )
}
}
}
2014-07-09 13:58:18 -05:00
impl Writer for MyMemWriter0 {
2014-07-06 01:44:43 -05:00
#[ inline ]
fn write ( & mut self , buf : & [ u8 ] ) -> io ::IoResult < ( ) > {
2014-07-09 13:58:18 -05:00
self . buf . push_all ( buf ) ;
Ok ( ( ) )
}
}
struct MyMemWriter1 {
buf : Vec < u8 > ,
}
impl MyMemWriter1 {
pub fn with_capacity ( cap : uint ) -> MyMemWriter1 {
MyMemWriter1 {
buf : Vec ::with_capacity ( cap )
}
}
}
2014-07-17 23:28:31 -05:00
// LLVM isn't yet able to lower `Vec::push_all` into a memcpy, so this helps
2014-09-07 03:19:36 -05:00
// MemWriter eke out that last bit of performance.
2014-12-01 15:32:00 -06:00
#[ inline ]
2014-07-17 23:28:31 -05:00
fn push_all_bytes ( dst : & mut Vec < u8 > , src : & [ u8 ] ) {
let dst_len = dst . len ( ) ;
let src_len = src . len ( ) ;
2014-11-07 14:28:31 -06:00
dst . reserve ( src_len ) ;
2014-07-17 23:28:31 -05:00
unsafe {
2014-11-07 14:28:31 -06:00
// we would have failed if `reserve` overflowed.
2014-07-17 23:28:31 -05:00
dst . set_len ( dst_len + src_len ) ;
::std ::ptr ::copy_nonoverlapping_memory (
dst . as_mut_ptr ( ) . offset ( dst_len as int ) ,
src . as_ptr ( ) ,
src_len ) ;
}
}
2014-07-09 13:58:18 -05:00
impl Writer for MyMemWriter1 {
#[ inline ]
fn write ( & mut self , buf : & [ u8 ] ) -> io ::IoResult < ( ) > {
2014-07-17 23:28:31 -05:00
push_all_bytes ( & mut self . buf , buf ) ;
2014-07-06 01:44:43 -05:00
Ok ( ( ) )
}
}
2014-12-01 15:09:35 -06:00
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"}"# ;
#[ test ]
fn test_encoder ( ) {
2014-12-01 15:20:37 -06:00
use serialize ::Encodable ;
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
2014-12-01 15:20:37 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
{
2014-12-05 18:24:32 -06:00
let mut encoder = serialize ::json ::Encoder ::new ( & mut wr as & mut Writer ) ;
2014-12-01 15:20:37 -06:00
log . encode ( & mut encoder ) . unwrap ( ) ;
}
assert_eq! ( wr . as_slice ( ) , JSON_STR . as_bytes ( ) ) ;
2014-12-01 15:09:35 -06:00
}
2014-06-20 18:32:30 -05:00
#[ bench ]
fn bench_encoder ( b : & mut Bencher ) {
let log = Log ::new ( ) ;
2014-12-05 18:24:32 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
{
let mut encoder = serialize ::json ::Encoder ::new ( & mut wr as & mut Writer ) ;
log . encode ( & mut encoder ) . unwrap ( ) ;
}
b . bytes = wr . len ( ) as u64 ;
2014-06-20 18:32:30 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . clear ( ) ;
let mut encoder = serialize ::json ::Encoder ::new ( & mut wr as & mut Writer ) ;
log . encode ( & mut encoder ) . unwrap ( ) ;
2014-06-20 18:32:30 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_serializer ( ) {
let log = Log ::new ( ) ;
let json = json ::to_vec ( & log ) ;
2014-12-05 18:24:32 -06:00
assert_eq! ( json , JSON_STR . as_bytes ( ) ) ;
2014-12-01 15:09:35 -06:00
}
2014-06-20 18:32:30 -05:00
#[ bench ]
fn bench_serializer ( b : & mut Bencher ) {
let log = Log ::new ( ) ;
2014-07-06 01:44:43 -05:00
let json = json ::to_vec ( & log ) ;
b . bytes = json . len ( ) as u64 ;
b . iter ( | | {
2014-12-05 18:24:32 -06:00
let _ = json ::to_vec ( & log ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_serializer_vec ( ) {
let log = Log ::new ( ) ;
let wr = Vec ::with_capacity ( 1024 ) ;
let mut serializer = json ::Serializer ::new ( wr ) ;
log . serialize ( & mut serializer ) . unwrap ( ) ;
let json = serializer . unwrap ( ) ;
assert_eq! ( json . as_slice ( ) , JSON_STR . as_bytes ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-12-01 15:09:35 -06:00
fn bench_serializer_vec ( b : & mut Bencher ) {
2014-07-17 23:28:31 -05:00
let log = Log ::new ( ) ;
let json = json ::to_vec ( & log ) ;
b . bytes = json . len ( ) as u64 ;
2014-12-01 15:20:37 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-07-17 23:28:31 -05:00
b . iter ( | | {
2014-12-01 15:20:37 -06:00
wr . clear ( ) ;
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
2014-08-31 22:44:19 -05:00
log . serialize ( & mut serializer ) . unwrap ( ) ;
2014-12-01 14:21:18 -06:00
let _json = serializer . unwrap ( ) ;
2014-07-17 23:28:31 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_serializer_my_mem_writer0 ( ) {
let log = Log ::new ( ) ;
2014-12-01 15:20:37 -06:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-01 15:09:35 -06:00
2014-12-01 15:20:37 -06:00
{
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
log . serialize ( & mut serializer ) . unwrap ( ) ;
}
2014-12-05 18:24:32 -06:00
assert_eq! ( wr . buf . as_slice ( ) , JSON_STR . as_bytes ( ) ) ;
2014-12-01 15:09:35 -06:00
}
2014-07-17 23:28:31 -05:00
#[ bench ]
fn bench_serializer_my_mem_writer0 ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
let json = json ::to_vec ( & log ) ;
b . bytes = json . len ( ) as u64 ;
2014-06-20 18:32:30 -05:00
2014-12-01 15:20:37 -06:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-06-20 18:32:30 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
2014-08-31 22:44:19 -05:00
log . serialize ( & mut serializer ) . unwrap ( ) ;
2014-06-20 18:32:30 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_serializer_my_mem_writer1 ( ) {
let log = Log ::new ( ) ;
2014-12-01 15:20:37 -06:00
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-01 15:09:35 -06:00
2014-12-01 15:20:37 -06:00
{
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
log . serialize ( & mut serializer ) . unwrap ( ) ;
}
2014-12-05 18:24:32 -06:00
assert_eq! ( wr . buf . as_slice ( ) , JSON_STR . as_bytes ( ) ) ;
2014-12-01 15:09:35 -06:00
}
2014-07-17 23:28:31 -05:00
#[ bench ]
fn bench_serializer_my_mem_writer1 ( b : & mut Bencher ) {
let log = Log ::new ( ) ;
let json = json ::to_vec ( & log ) ;
b . bytes = json . len ( ) as u64 ;
2014-12-01 15:20:37 -06:00
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-07-17 23:28:31 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
2014-08-31 22:44:19 -05:00
log . serialize ( & mut serializer ) . unwrap ( ) ;
2014-07-17 23:28:31 -05:00
} ) ;
}
2014-07-03 09:07:30 -05:00
#[ bench ]
fn bench_copy ( b : & mut Bencher ) {
2014-12-01 15:09:35 -06:00
let json = JSON_STR . as_bytes ( ) . to_vec ( ) ;
2014-07-06 01:44:43 -05:00
b . bytes = json . len ( ) as u64 ;
b . iter ( | | {
2014-12-01 15:09:35 -06:00
let _json = JSON_STR . as_bytes ( ) . to_vec ( ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-12-14 00:58:05 -06:00
fn manual_serialize_no_escape < W : Writer > ( wr : & mut W , log : & Log ) {
2014-07-06 01:44:43 -05:00
wr . write_str ( " { \" timestamp \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . timestamp ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" zone_id \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . zone_id ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" zone_plan \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . zone_plan as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" http \" :{ \" protocol \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . protocol as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" status \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . status ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" host_status \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . host_status ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" up_status \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . up_status ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" method \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . method as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" content_type \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . http . content_type ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" user_agent \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . http . user_agent ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" referer \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . http . referer ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" request_uri \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . http . request_uri ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " }, \" origin \" :{ " ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
2014-09-07 03:04:39 -05:00
wr . write_str ( " \" ip \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . origin . ip ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , \" port \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . origin . port ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" hostname \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . origin . hostname ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" protocol \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . origin . protocol as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " }, \" country \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . country as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" cache_status \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . cache_status as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" server_ip \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . server_ip ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" server_name \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . server_name ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" remote_ip \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . remote_ip ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" bytes_dlv \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . bytes_dlv ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " , \" ray_id \" : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " \" {} \" " , log . ray_id ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " } " ) . unwrap ( ) ;
}
2014-12-14 00:58:05 -06:00
fn manual_serialize_escape < W : Writer > ( wr : & mut W , log : & Log ) {
2014-09-07 03:04:39 -05:00
wr . write_str ( " { " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " timestamp " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . timestamp ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " zone_id " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . zone_id ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " zone_plan " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . zone_plan as int ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " http " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " :{ " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " protocol " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . protocol as uint ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " status " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . status ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " host_status " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . host_status ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " up_status " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . up_status ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " method " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . http . method as uint ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " content_type " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . http . content_type . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " user_agent " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . http . user_agent . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " referer " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . http . referer . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " request_uri " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . http . request_uri . as_slice ( ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
2014-09-07 03:04:39 -05:00
wr . write_str ( " }, " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " origin " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " :{ " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " ip " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . origin . ip . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " port " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . origin . port ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " hostname " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . origin . hostname . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " protocol " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . origin . protocol as uint ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
2014-09-07 03:04:39 -05:00
wr . write_str ( " }, " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " country " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . country as uint ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " cache_status " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . cache_status as uint ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " server_ip " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . server_ip . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " server_name " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . server_name . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " remote_ip " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . remote_ip . as_slice ( ) ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " bytes_dlv " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
( write! ( wr , " {} " , log . bytes_dlv ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
2014-09-07 03:04:39 -05:00
wr . write_str ( " , " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , " ray_id " ) . unwrap ( ) ;
2014-09-07 03:04:39 -05:00
wr . write_str ( " : " ) . unwrap ( ) ;
2014-12-05 18:24:32 -06:00
escape_str ( wr , log . ray_id . as_slice ( ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
wr . write_str ( " } " ) . unwrap ( ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_vec_no_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
let json = String ::from_utf8 ( wr ) . unwrap ( ) ;
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_vec_no_escape ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-12-01 14:21:18 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 14:21:18 -06:00
b . bytes = wr . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-01 15:20:37 -06:00
wr . clear ( ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_vec_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
let json = String ::from_utf8 ( wr ) . unwrap ( ) ;
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_vec_escape ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-12-01 14:21:18 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-01 14:21:18 -06:00
b . bytes = wr . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-01 15:20:37 -06:00
wr . clear ( ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_my_mem_writer0_no_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
let mut wr = MyMemWriter0 ::with_capacity ( 1000 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_my_mem_writer0_no_escape ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-12-05 18:24:32 -06:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:20:37 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
} ) ;
}
2014-07-06 01:44:43 -05:00
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_my_mem_writer0_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
2014-07-06 01:44:43 -05:00
2014-12-01 15:09:35 -06:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
2014-07-06 01:44:43 -05:00
}
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_my_mem_writer0_escape ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-12-01 15:09:35 -06:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-01 15:20:37 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
2014-07-03 09:07:30 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-07-09 13:58:18 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_my_mem_writer1_no_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-09 13:58:18 -05:00
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_my_mem_writer1_no_escape ( b : & mut Bencher ) {
2014-07-09 13:58:18 -05:00
let log = Log ::new ( ) ;
2014-12-05 18:24:32 -06:00
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-12-01 15:20:37 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
2014-07-09 13:58:18 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
2014-12-14 00:58:05 -06:00
manual_serialize_no_escape ( & mut wr , & log ) ;
2014-07-09 13:58:18 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
2014-12-14 00:58:05 -06:00
fn test_manual_serialize_my_mem_writer1_escape ( ) {
2014-12-01 15:09:35 -06:00
let log = Log ::new ( ) ;
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-09 13:58:18 -05:00
#[ bench ]
2014-12-14 00:58:05 -06:00
fn bench_manual_serialize_my_mem_writer1_escape ( b : & mut Bencher ) {
2014-07-09 13:58:18 -05:00
let log = Log ::new ( ) ;
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-12-05 18:24:32 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
2014-07-09 13:58:18 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-14 00:58:05 -06:00
manual_serialize_escape ( & mut wr , & log ) ;
2014-07-03 09:07:30 -05:00
} ) ;
}
2014-12-05 18:24:32 -06:00
fn direct < W : Writer > ( wr : & mut W , log : & Log ) {
2014-09-06 22:41:13 -05:00
use serde ::ser ::Serializer ;
2014-07-06 01:44:43 -05:00
2014-12-05 18:24:32 -06:00
let mut serializer = json ::Serializer ::new ( wr . by_ref ( ) ) ;
2014-12-01 15:09:35 -06:00
2014-07-06 01:44:43 -05:00
serializer . serialize_struct_start ( " Log " , 12 ) . unwrap ( ) ;
2014-07-31 09:18:26 -05:00
serializer . serialize_struct_elt ( " timestamp " , & log . timestamp ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " zone_id " , & log . zone_id ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " zone_plan " , & ( log . zone_plan as uint ) ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
serializer . serialize_struct_elt ( " http " , & log . http ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " origin " , & log . origin ) . unwrap ( ) ;
2014-07-31 09:18:26 -05:00
serializer . serialize_struct_elt ( " country " , & ( log . country as uint ) ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " cache_status " , & ( log . cache_status as uint ) ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " server_ip " , & log . server_ip . as_slice ( ) ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " server_name " , & log . server_name . as_slice ( ) ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " remote_ip " , & log . remote_ip . as_slice ( ) ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " bytes_dlv " , & log . bytes_dlv ) . unwrap ( ) ;
serializer . serialize_struct_elt ( " ray_id " , & log . ray_id . as_slice ( ) ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
serializer . serialize_struct_end ( ) . unwrap ( ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_direct_vec ( ) {
let log = Log ::new ( ) ;
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
let json = String ::from_utf8 ( wr ) . unwrap ( ) ;
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-12-01 15:09:35 -06:00
fn bench_direct_vec ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-12-01 14:21:18 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 14:21:18 -06:00
b . bytes = wr . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-01 14:21:18 -06:00
let mut wr = Vec ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_direct_my_mem_writer0 ( ) {
let log = Log ::new ( ) ;
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:09:35 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:09:35 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
2014-07-06 01:44:43 -05:00
#[ bench ]
2014-07-09 13:58:18 -05:00
fn bench_direct_my_mem_writer0 ( b : & mut Bencher ) {
2014-07-06 01:44:43 -05:00
let log = Log ::new ( ) ;
2014-07-09 13:58:18 -05:00
let mut wr = MyMemWriter0 ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:20:37 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:20:37 -06:00
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-07-03 09:07:30 -05:00
} ) ;
}
2014-12-01 15:32:00 -06:00
#[ test ]
fn test_direct_my_mem_writer1 ( ) {
let log = Log ::new ( ) ;
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:32:00 -06:00
2014-12-05 18:24:32 -06:00
let json = String ::from_utf8 ( wr . buf ) . unwrap ( ) ;
2014-12-01 15:32:00 -06:00
assert_eq! ( JSON_STR , json . as_slice ( ) ) ;
}
#[ bench ]
fn bench_direct_my_mem_writer1 ( b : & mut Bencher ) {
let log = Log ::new ( ) ;
let mut wr = MyMemWriter1 ::with_capacity ( 1024 ) ;
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:32:00 -06:00
b . bytes = wr . buf . len ( ) as u64 ;
b . iter ( | | {
2014-12-05 18:24:32 -06:00
wr . buf . clear ( ) ;
2014-12-01 15:32:00 -06:00
2014-12-05 18:24:32 -06:00
direct ( & mut wr , & log ) ;
2014-12-01 15:32:00 -06:00
} ) ;
}
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_decoder ( ) {
let json = serialize ::json ::from_str ( JSON_STR ) . unwrap ( ) ;
let mut decoder = serialize ::json ::Decoder ::new ( json ) ;
let log : Log = serialize ::Decodable ::decode ( & mut decoder ) . unwrap ( ) ;
assert_eq! ( log , Log ::new ( ) ) ;
}
2014-06-20 18:32:30 -05:00
#[ bench ]
2014-06-22 09:33:45 -05:00
fn bench_decoder ( b : & mut Bencher ) {
2014-12-01 15:09:35 -06:00
b . bytes = JSON_STR . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
2014-06-20 18:32:30 -05:00
b . iter ( | | {
2014-12-01 15:09:35 -06:00
let json = serialize ::json ::from_str ( JSON_STR ) . unwrap ( ) ;
2014-06-20 18:32:30 -05:00
let mut decoder = serialize ::json ::Decoder ::new ( json ) ;
let _log : Log = serialize ::Decodable ::decode ( & mut decoder ) . unwrap ( ) ;
} ) ;
}
2014-07-06 01:44:43 -05:00
2014-12-01 15:09:35 -06:00
#[ test ]
fn test_deserializer ( ) {
let log : Log = json ::from_str ( JSON_STR ) . unwrap ( ) ;
assert_eq! ( log , Log ::new ( ) ) ;
}
2014-12-14 01:39:19 -06:00
//////////////////////////////////////////////////////////////////////////////
2014-12-14 00:58:05 -06:00
#[ inline ]
2014-12-14 01:39:19 -06:00
fn manual_reader_ignore < R : Reader > ( rdr : & mut R , buf : & mut [ u8 ] , key : & [ u8 ] ) {
2014-12-14 00:58:05 -06:00
let buf = buf . slice_mut ( 0 , key . len ( ) ) ;
rdr . read ( buf ) . unwrap ( ) ;
assert_eq! ( buf , key ) ;
}
#[ inline ]
2014-12-14 01:39:19 -06:00
fn manual_reader_field < R : Reader > ( rdr : & mut R , buf : & mut [ u8 ] , key : & [ u8 ] ) {
2014-12-14 00:58:05 -06:00
let b = rdr . read_byte ( ) . unwrap ( ) ;
assert_eq! ( b , b '"' ) ;
2014-12-14 01:39:19 -06:00
manual_reader_ignore ( rdr , buf , key ) ;
2014-12-14 00:58:05 -06:00
let b = rdr . read_byte ( ) . unwrap ( ) ;
assert_eq! ( b , b '"' ) ;
let b = rdr . read_byte ( ) . unwrap ( ) ;
assert_eq! ( b , b ':' ) ;
}
#[ inline ]
2014-12-14 01:39:19 -06:00
fn manual_reader_int < R : Reader > ( rdr : & mut R , buf : & mut [ u8 ] , key : & [ u8 ] ) -> i64 {
manual_reader_field ( rdr , buf , key ) ;
2014-12-14 00:58:05 -06:00
let mut res = 0 ;
loop {
let byte = rdr . read_byte ( ) . unwrap ( ) ;
match byte {
b '0' .. . b '9' = > {
res * = 10 ;
res + = ( byte as i64 ) - ( b '0' as i64 ) ;
}
_ = > { break ; }
}
}
res
}
#[ inline ]
2014-12-14 01:39:19 -06:00
fn manual_reader_string < R : Reader > ( rdr : & mut R , buf : & mut [ u8 ] , key : & [ u8 ] ) -> String {
manual_reader_field ( rdr , buf , key ) ;
manual_reader_ignore ( rdr , buf , b " \" " ) ;
2014-12-14 00:58:05 -06:00
let mut idx = 0 ;
loop {
let byte = rdr . read_byte ( ) . unwrap ( ) ;
match byte {
b '"' = > { break ; }
byte = > { buf [ idx ] = byte ; }
} ;
idx + = 1 ;
}
let b = rdr . read_byte ( ) . unwrap ( ) ;
assert! ( b = = b ',' | | b = = b ']' | | b = = b '}' ) ;
String ::from_utf8 ( buf . slice_to ( idx ) . to_vec ( ) ) . unwrap ( )
}
#[ inline ]
2014-12-14 01:39:19 -06:00
fn manual_reader_deserialize < R : Reader > ( rdr : & mut R ) -> Log {
2014-12-14 00:58:05 -06:00
let mut buf = [ 0 , .. 128 ] ;
2014-12-14 01:39:19 -06:00
manual_reader_ignore ( rdr , & mut buf , b " { " ) ;
let timestamp = manual_reader_int ( rdr , & mut buf , b " timestamp " ) ;
let zone_id = manual_reader_int ( rdr , & mut buf , b " zone_id " ) ;
let zone_plan = manual_reader_int ( rdr , & mut buf , b " zone_plan " ) ;
2014-12-14 00:58:05 -06:00
2014-12-14 01:39:19 -06:00
manual_reader_field ( rdr , & mut buf , b " http " ) ;
manual_reader_ignore ( rdr , & mut buf , b " { " ) ;
2014-12-14 00:58:05 -06:00
2014-12-14 01:39:19 -06:00
let protocol = manual_reader_int ( rdr , & mut buf , b " protocol " ) ;
let status = manual_reader_int ( rdr , & mut buf , b " status " ) ;
let host_status = manual_reader_int ( rdr , & mut buf , b " host_status " ) ;
let up_status = manual_reader_int ( rdr , & mut buf , b " up_status " ) ;
let method = manual_reader_int ( rdr , & mut buf , b " method " ) ;
let content_type = manual_reader_string ( rdr , & mut buf , b " content_type " ) ;
let user_agent = manual_reader_string ( rdr , & mut buf , b " user_agent " ) ;
let referer = manual_reader_string ( rdr , & mut buf , b " referer " ) ;
let request_uri = manual_reader_string ( rdr , & mut buf , b " request_uri " ) ;
2014-12-14 00:58:05 -06:00
let http = Http {
protocol : FromPrimitive ::from_i64 ( protocol ) . unwrap ( ) ,
status : FromPrimitive ::from_i64 ( status ) . unwrap ( ) ,
host_status : FromPrimitive ::from_i64 ( host_status ) . unwrap ( ) ,
up_status : FromPrimitive ::from_i64 ( up_status ) . unwrap ( ) ,
method : FromPrimitive ::from_i64 ( method ) . unwrap ( ) ,
content_type : content_type ,
user_agent : user_agent ,
referer : referer ,
request_uri : request_uri ,
} ;
2014-12-14 01:39:19 -06:00
manual_reader_ignore ( rdr , & mut buf , b " , " ) ;
manual_reader_field ( rdr , & mut buf , b " origin " ) ;
manual_reader_ignore ( rdr , & mut buf , b " { " ) ;
2014-12-14 00:58:05 -06:00
2014-12-14 01:39:19 -06:00
let ip = manual_reader_string ( rdr , & mut buf , b " ip " ) ;
let port = manual_reader_int ( rdr , & mut buf , b " port " ) ;
let hostname = manual_reader_string ( rdr , & mut buf , b " hostname " ) ;
let protocol = manual_reader_int ( rdr , & mut buf , b " protocol " ) ;
2014-12-14 00:58:05 -06:00
let origin = Origin {
ip : ip ,
port : FromPrimitive ::from_i64 ( port ) . unwrap ( ) ,
hostname : hostname ,
protocol : FromPrimitive ::from_i64 ( protocol ) . unwrap ( ) ,
} ;
2014-12-14 01:39:19 -06:00
manual_reader_ignore ( rdr , & mut buf , b " , " ) ;
let country = manual_reader_int ( rdr , & mut buf , b " country " ) ;
let cache_status = manual_reader_int ( rdr , & mut buf , b " cache_status " ) ;
let server_ip = manual_reader_string ( rdr , & mut buf , b " server_ip " ) ;
let server_name = manual_reader_string ( rdr , & mut buf , b " server_name " ) ;
let remote_ip = manual_reader_string ( rdr , & mut buf , b " remote_ip " ) ;
let bytes_dlv = manual_reader_int ( rdr , & mut buf , b " bytes_dlv " ) ;
let ray_id = manual_reader_string ( rdr , & mut buf , b " ray_id " ) ;
2014-12-14 00:58:05 -06:00
Log {
timestamp : timestamp ,
zone_id : FromPrimitive ::from_i64 ( zone_id ) . unwrap ( ) ,
zone_plan : FromPrimitive ::from_i64 ( zone_plan ) . unwrap ( ) ,
http : http ,
origin : origin ,
country : FromPrimitive ::from_i64 ( country ) . unwrap ( ) ,
cache_status : FromPrimitive ::from_i64 ( cache_status ) . unwrap ( ) ,
server_ip : server_ip ,
server_name : server_name ,
remote_ip : remote_ip ,
bytes_dlv : FromPrimitive ::from_i64 ( bytes_dlv ) . unwrap ( ) ,
ray_id : ray_id ,
}
}
2014-12-14 01:39:19 -06:00
//////////////////////////////////////////////////////////////////////////////
#[ inline ]
fn manual_iter_ignore < R : Iterator < u8 > > ( mut rdr : R , buf : & mut [ u8 ] , key : & [ u8 ] ) {
let buf = buf . slice_mut ( 0 , key . len ( ) ) ;
for idx in range ( 0 , key . len ( ) ) {
buf [ idx ] = rdr . next ( ) . unwrap ( ) ;
}
assert_eq! ( buf , key ) ;
}
#[ inline ]
fn manual_iter_field < R : Iterator < u8 > > ( mut rdr : R , buf : & mut [ u8 ] , key : & [ u8 ] ) {
let b = rdr . next ( ) . unwrap ( ) ;
assert_eq! ( b , b '"' ) ;
manual_iter_ignore ( rdr . by_ref ( ) , buf , key ) ;
let b = rdr . next ( ) . unwrap ( ) ;
assert_eq! ( b , b '"' ) ;
let b = rdr . next ( ) . unwrap ( ) ;
assert_eq! ( b , b ':' ) ;
}
#[ inline ]
fn manual_iter_int < R : Iterator < u8 > > ( mut rdr : R , buf : & mut [ u8 ] , key : & [ u8 ] ) -> i64 {
manual_iter_field ( rdr . by_ref ( ) , buf , key ) ;
let mut res = 0 ;
loop {
let byte = rdr . next ( ) . unwrap ( ) ;
match byte {
b '0' .. . b '9' = > {
res * = 10 ;
res + = ( byte as i64 ) - ( b '0' as i64 ) ;
}
_ = > { break ; }
}
}
res
}
#[ inline ]
fn manual_iter_string < R : Iterator < u8 > > ( mut rdr : R , buf : & mut [ u8 ] , key : & [ u8 ] ) -> String {
manual_iter_field ( rdr . by_ref ( ) , buf , key ) ;
manual_iter_ignore ( rdr . by_ref ( ) , buf , b " \" " ) ;
let mut idx = 0 ;
loop {
let byte = rdr . next ( ) . unwrap ( ) ;
match byte {
b '"' = > { break ; }
byte = > { buf [ idx ] = byte ; }
} ;
idx + = 1 ;
}
let b = rdr . next ( ) . unwrap ( ) ;
assert! ( b = = b ',' | | b = = b ']' | | b = = b '}' ) ;
String ::from_utf8 ( buf . slice_to ( idx ) . to_vec ( ) ) . unwrap ( )
}
#[ inline ]
fn manual_iter_deserialize < R : Iterator < u8 > > ( mut rdr : R ) -> Log {
let mut buf = [ 0 u8 , .. 128 ] ;
manual_iter_ignore ( rdr . by_ref ( ) , & mut buf , b " { " ) ;
let timestamp = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " timestamp " ) ;
let zone_id = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " zone_id " ) ;
let zone_plan = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " zone_plan " ) ;
manual_iter_field ( rdr . by_ref ( ) , & mut buf , b " http " ) ;
manual_iter_ignore ( rdr . by_ref ( ) , & mut buf , b " { " ) ;
let protocol = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " protocol " ) ;
let status = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " status " ) ;
let host_status = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " host_status " ) ;
let up_status = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " up_status " ) ;
let method = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " method " ) ;
let content_type = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " content_type " ) ;
let user_agent = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " user_agent " ) ;
let referer = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " referer " ) ;
let request_uri = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " request_uri " ) ;
let http = Http {
protocol : FromPrimitive ::from_i64 ( protocol ) . unwrap ( ) ,
status : FromPrimitive ::from_i64 ( status ) . unwrap ( ) ,
host_status : FromPrimitive ::from_i64 ( host_status ) . unwrap ( ) ,
up_status : FromPrimitive ::from_i64 ( up_status ) . unwrap ( ) ,
method : FromPrimitive ::from_i64 ( method ) . unwrap ( ) ,
content_type : content_type ,
user_agent : user_agent ,
referer : referer ,
request_uri : request_uri ,
} ;
manual_iter_ignore ( rdr . by_ref ( ) , & mut buf , b " , " ) ;
manual_iter_field ( rdr . by_ref ( ) , & mut buf , b " origin " ) ;
manual_iter_ignore ( rdr . by_ref ( ) , & mut buf , b " { " ) ;
let ip = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " ip " ) ;
let port = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " port " ) ;
let hostname = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " hostname " ) ;
let protocol = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " protocol " ) ;
let origin = Origin {
ip : ip ,
port : FromPrimitive ::from_i64 ( port ) . unwrap ( ) ,
hostname : hostname ,
protocol : FromPrimitive ::from_i64 ( protocol ) . unwrap ( ) ,
} ;
manual_iter_ignore ( rdr . by_ref ( ) , & mut buf , b " , " ) ;
let country = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " country " ) ;
let cache_status = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " cache_status " ) ;
let server_ip = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " server_ip " ) ;
let server_name = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " server_name " ) ;
let remote_ip = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " remote_ip " ) ;
let bytes_dlv = manual_iter_int ( rdr . by_ref ( ) , & mut buf , b " bytes_dlv " ) ;
let ray_id = manual_iter_string ( rdr . by_ref ( ) , & mut buf , b " ray_id " ) ;
Log {
timestamp : timestamp ,
zone_id : FromPrimitive ::from_i64 ( zone_id ) . unwrap ( ) ,
zone_plan : FromPrimitive ::from_i64 ( zone_plan ) . unwrap ( ) ,
http : http ,
origin : origin ,
country : FromPrimitive ::from_i64 ( country ) . unwrap ( ) ,
cache_status : FromPrimitive ::from_i64 ( cache_status ) . unwrap ( ) ,
server_ip : server_ip ,
server_name : server_name ,
remote_ip : remote_ip ,
bytes_dlv : FromPrimitive ::from_i64 ( bytes_dlv ) . unwrap ( ) ,
ray_id : ray_id ,
}
}
//////////////////////////////////////////////////////////////////////////////
2014-07-06 01:44:43 -05:00
#[ bench ]
fn bench_deserializer ( b : & mut Bencher ) {
2014-12-01 15:09:35 -06:00
b . bytes = JSON_STR . len ( ) as u64 ;
2014-07-06 01:44:43 -05:00
b . iter ( | | {
2014-12-01 15:09:35 -06:00
let _log : Log = json ::from_str ( JSON_STR ) . unwrap ( ) ;
2014-07-06 01:44:43 -05:00
} ) ;
}
2014-10-22 21:22:49 -05:00
2014-12-14 01:39:19 -06:00
#[ bench ]
fn bench_deserializers ( b : & mut Bencher ) {
let s = r # "{"timestamp":25469139677502,"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"}"# ;
b . bytes = s . len ( ) as u64 ;
2014-12-14 13:31:24 -06:00
for _ in range ( 0 i , 10000 ) {
2014-12-14 01:39:19 -06:00
let _log : Log = json ::from_str ( s ) . unwrap ( ) ;
2014-12-14 13:31:24 -06:00
}
2014-12-14 01:39:19 -06:00
}
//////////////////////////////////////////////////////////////////////////////
2014-12-14 00:58:05 -06:00
#[ test ]
2014-12-14 01:39:19 -06:00
fn test_reader_manual_deserializer ( ) {
2014-12-14 00:58:05 -06:00
let mut rdr = JSON_STR . as_bytes ( ) ;
2014-12-14 01:39:19 -06:00
let log = manual_reader_deserialize ( & mut rdr ) ;
2014-12-14 00:58:05 -06:00
assert_eq! ( log , Log ::new ( ) ) ;
}
#[ bench ]
2014-12-14 01:39:19 -06:00
fn bench_reader_manual_reader_deserializer ( b : & mut Bencher ) {
2014-12-14 00:58:05 -06:00
b . bytes = JSON_STR . len ( ) as u64 ;
b . iter ( | | {
let mut rdr = JSON_STR . as_bytes ( ) ;
2014-12-14 01:39:19 -06:00
let _ = manual_reader_deserialize ( & mut rdr ) ;
2014-12-14 00:58:05 -06:00
} ) ;
}
2014-10-22 21:22:49 -05:00
#[ bench ]
2014-12-14 01:39:19 -06:00
fn bench_reader_manual_reader_deserializers ( b : & mut Bencher ) {
b . bytes = JSON_STR . len ( ) as u64 ;
2014-10-22 21:22:49 -05:00
2014-12-14 01:39:19 -06:00
for _ in range ( 0 i , 100000 ) {
let mut rdr = JSON_STR . as_bytes ( ) ;
let _ = manual_reader_deserialize ( & mut rdr ) ;
}
}
2014-10-22 21:22:49 -05:00
2014-12-14 01:39:19 -06:00
//////////////////////////////////////////////////////////////////////////////
#[ test ]
fn test_iter_manual_iter_deserializer ( ) {
let log = manual_iter_deserialize ( JSON_STR . bytes ( ) ) ;
assert_eq! ( log , Log ::new ( ) ) ;
}
#[ bench ]
fn bench_iter_manual_iter_deserializer ( b : & mut Bencher ) {
b . bytes = JSON_STR . len ( ) as u64 ;
b . iter ( | | {
let _ = manual_iter_deserialize ( JSON_STR . bytes ( ) ) ;
} ) ;
}
#[ bench ]
fn bench_iter_manual_iter_deserializers ( b : & mut Bencher ) {
b . bytes = JSON_STR . len ( ) as u64 ;
for _ in range ( 0 i , 10000 ) {
let _ = manual_iter_deserialize ( JSON_STR . bytes ( ) ) ;
}
2014-10-22 21:22:49 -05:00
}
2014-12-15 22:10:25 -06:00
//////////////////////////////////////////////////////////////////////////////
#[ test ]
fn test_iter_manual_reader_as_iter_deserializer ( ) {
use std ::io ::extensions ::Bytes ;
let iter = Bytes ::new ( & mut JSON_STR . as_bytes ( ) )
. map ( | x | x . unwrap ( ) ) ;
let log = manual_iter_deserialize ( iter ) ;
assert_eq! ( log , Log ::new ( ) ) ;
}
#[ bench ]
fn bench_iter_manual_reader_as_iter_deserializer ( b : & mut Bencher ) {
use std ::io ::extensions ::Bytes ;
b . bytes = JSON_STR . len ( ) as u64 ;
b . iter ( | | {
let iter = Bytes ::new ( & mut JSON_STR . as_bytes ( ) )
. map ( | x | x . unwrap ( ) ) ;
let _ = manual_iter_deserialize ( iter ) ;
} ) ;
}
#[ bench ]
fn bench_iter_manual_reader_as_iter_deserializers ( b : & mut Bencher ) {
b . bytes = JSON_STR . len ( ) as u64 ;
for _ in range ( 0 i , 10000 ) {
let iter = Bytes ::new ( & mut JSON_STR . as_bytes ( ) )
. map ( | x | x . unwrap ( ) ) ;
let _ = manual_iter_deserialize ( iter ) ;
}
}