2014-09-06 20:44:53 -07:00
|
|
|
#![feature(phase, macro_rules)]
|
2014-09-07 01:19:36 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
|
|
|
|
#[phase(plugin)]
|
|
|
|
extern crate serde2_macros;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
extern crate serde2;
|
|
|
|
extern crate serialize;
|
|
|
|
extern crate test;
|
|
|
|
|
2014-09-07 01:19:36 -07:00
|
|
|
use std::io;
|
2014-12-05 16:24:32 -08:00
|
|
|
use std::io::ByRefWriter;
|
2014-09-06 20:44:53 -07:00
|
|
|
use test::Bencher;
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
use serde2::json::ser::escape_str;
|
2014-09-07 01:19:36 -07:00
|
|
|
use serde2::json;
|
2014-12-05 16:24:32 -08:00
|
|
|
use serde2::ser::{Serialize, Serializer};
|
2014-09-06 20:44:53 -07:00
|
|
|
use serde2::ser;
|
2014-12-05 22:19:30 -08:00
|
|
|
use serde2::de::{Deserialize, Deserializer};
|
|
|
|
use serde2::de;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
use serialize::Encodable;
|
|
|
|
|
2014-12-08 23:39:43 -08:00
|
|
|
enum HttpField {
|
|
|
|
Protocol,
|
|
|
|
Status,
|
|
|
|
HostStatus,
|
|
|
|
UpStatus,
|
|
|
|
Method,
|
|
|
|
ContentType,
|
|
|
|
UserAgent,
|
|
|
|
Referer,
|
|
|
|
RequestUri,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for HttpField {
|
|
|
|
fn deserialize(state: &mut S) -> Result<HttpField, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
'a,
|
|
|
|
S: de::Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Visitor<S, HttpField, E> for Visitor {
|
|
|
|
fn visit_str(&mut self, value: &str) -> Result<HttpField, E> {
|
|
|
|
let x = match value {
|
|
|
|
"protocol" => HttpField::Protocol,
|
|
|
|
"status" => HttpField::Status,
|
|
|
|
"host_status" => HttpField::HostStatus,
|
|
|
|
"up_status" => HttpField::UpStatus,
|
|
|
|
"method" => HttpField::Method,
|
|
|
|
"content_type" => HttpField::ContentType,
|
|
|
|
"user_agent" => HttpField::UserAgent,
|
|
|
|
"referer" => HttpField::Referer,
|
|
|
|
"request_uri" => HttpField::RequestUri,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[deriving(Show, PartialEq, Encodable, Decodable)]
|
2014-10-14 18:08:34 -07:00
|
|
|
#[deriving_serialize]
|
|
|
|
//#[deriving_deserialize]
|
2014-09-06 20:44:53 -07: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-05 22:19:30 -08:00
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for Http {
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<Http, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
2014-12-05 22:19:30 -08:00
|
|
|
> de::Visitor<S, Http, E> for Visitor {
|
|
|
|
fn visit_map<
|
|
|
|
Visitor: de::MapVisitor<S, E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
>(&mut self, mut visitor: Visitor) -> Result<Http, E> {
|
2014-12-05 22:19:30 -08:00
|
|
|
let mut protocol = None;
|
|
|
|
let mut status = None;
|
|
|
|
let mut host_status = None;
|
|
|
|
let mut up_status = None;
|
|
|
|
let mut method = None;
|
|
|
|
let mut content_type = None;
|
|
|
|
let mut user_agent = None;
|
|
|
|
let mut referer = None;
|
|
|
|
let mut request_uri = None;
|
|
|
|
|
|
|
|
loop {
|
2014-12-08 23:13:17 -08:00
|
|
|
match try!(visitor.visit_key()) {
|
2014-12-08 23:39:43 -08:00
|
|
|
Some(field) => {
|
|
|
|
match field {
|
|
|
|
HttpField::Protocol => { protocol = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::Status => { status = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::HostStatus => { host_status = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::UpStatus => { up_status = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::Method => { method = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::ContentType => { content_type = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::UserAgent => { user_agent = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::Referer => { referer = Some(try!(visitor.visit_value())); }
|
|
|
|
HttpField::RequestUri => { request_uri = Some(try!(visitor.visit_value())); }
|
2014-12-05 22:19:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Http {
|
|
|
|
protocol: protocol.unwrap(),
|
|
|
|
status: status.unwrap(),
|
|
|
|
host_status: host_status.unwrap(),
|
|
|
|
up_status: up_status.unwrap(),
|
|
|
|
method: method.unwrap(),
|
|
|
|
content_type: content_type.unwrap(),
|
|
|
|
user_agent: user_agent.unwrap(),
|
|
|
|
referer: referer.unwrap(),
|
|
|
|
request_uri: request_uri.unwrap(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07:00
|
|
|
enum HttpProtocol {
|
|
|
|
HTTP_PROTOCOL_UNKNOWN,
|
|
|
|
HTTP10,
|
|
|
|
HTTP11,
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for HttpProtocol {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<
|
|
|
|
S: de::Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for HttpProtocol {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<HttpProtocol, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07:00
|
|
|
enum HttpMethod {
|
|
|
|
METHOD_UNKNOWN,
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
DELETE,
|
|
|
|
PUT,
|
|
|
|
HEAD,
|
|
|
|
PURGE,
|
|
|
|
OPTIONS,
|
|
|
|
PROPFIND,
|
|
|
|
MKCOL,
|
|
|
|
PATCH,
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for HttpMethod {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<
|
|
|
|
S: de::Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for HttpMethod {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<HttpMethod, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07:00
|
|
|
enum CacheStatus {
|
|
|
|
CACHESTATUS_UNKNOWN,
|
|
|
|
Miss,
|
|
|
|
Expired,
|
|
|
|
Hit,
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for CacheStatus {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for CacheStatus {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<CacheStatus, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:39:43 -08:00
|
|
|
enum OriginField {
|
|
|
|
Ip,
|
|
|
|
Port,
|
|
|
|
Hostname,
|
|
|
|
Protocol,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for OriginField {
|
|
|
|
fn deserialize(state: &mut S) -> Result<OriginField, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
'a,
|
|
|
|
S: de::Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Visitor<S, OriginField, E> for Visitor {
|
|
|
|
fn visit_str(&mut self, value: &str) -> Result<OriginField, E> {
|
|
|
|
let x = match value {
|
|
|
|
"ip" => OriginField::Ip,
|
|
|
|
"port" => OriginField::Port,
|
|
|
|
"hostname" => OriginField::Hostname,
|
|
|
|
"protocol" => OriginField::Protocol,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[deriving(Show, PartialEq, Encodable, Decodable)]
|
2014-10-14 18:08:34 -07:00
|
|
|
#[deriving_serialize]
|
|
|
|
//#[deriving_deserialize]
|
2014-09-06 20:44:53 -07:00
|
|
|
struct Origin {
|
|
|
|
ip: String,
|
|
|
|
port: u32,
|
|
|
|
hostname: String,
|
|
|
|
protocol: OriginProtocol,
|
|
|
|
}
|
|
|
|
|
2014-12-05 22:19:30 -08:00
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
2014-12-05 22:19:30 -08:00
|
|
|
> Deserialize<S, E> for Origin {
|
|
|
|
fn deserialize(state: &mut S) -> Result<Origin, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
2014-12-05 22:19:30 -08:00
|
|
|
> de::Visitor<S, Origin, E> for Visitor {
|
|
|
|
fn visit_map<
|
|
|
|
Visitor: de::MapVisitor<S, E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
>(&mut self, mut visitor: Visitor) -> Result<Origin, E> {
|
2014-12-05 22:19:30 -08:00
|
|
|
let mut ip = None;
|
|
|
|
let mut port = None;
|
|
|
|
let mut hostname = None;
|
|
|
|
let mut protocol = None;
|
|
|
|
|
|
|
|
loop {
|
2014-12-08 23:13:17 -08:00
|
|
|
match try!(visitor.visit_key()) {
|
2014-12-08 23:39:43 -08:00
|
|
|
Some(field) => {
|
|
|
|
match field {
|
|
|
|
OriginField::Ip => { ip = Some(try!(visitor.visit_value())); }
|
|
|
|
OriginField::Port => { port = Some(try!(visitor.visit_value())); }
|
|
|
|
OriginField::Hostname => { hostname = Some(try!(visitor.visit_value())); }
|
|
|
|
OriginField::Protocol => { protocol = Some(try!(visitor.visit_value())); }
|
2014-12-05 22:19:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Origin {
|
|
|
|
ip: ip.unwrap(),
|
|
|
|
port: port.unwrap(),
|
|
|
|
hostname: hostname.unwrap(),
|
|
|
|
protocol: protocol.unwrap(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07:00
|
|
|
enum OriginProtocol {
|
|
|
|
ORIGIN_PROTOCOL_UNKNOWN,
|
|
|
|
HTTP,
|
|
|
|
HTTPS,
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for OriginProtocol {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for OriginProtocol {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<OriginProtocol, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07:00
|
|
|
enum ZonePlan {
|
|
|
|
ZONEPLAN_UNKNOWN,
|
|
|
|
FREE,
|
|
|
|
PRO,
|
|
|
|
BIZ,
|
|
|
|
ENT,
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for ZonePlan {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for ZonePlan {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<ZonePlan, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:58:31 -08:00
|
|
|
#[deriving(Copy, Show, PartialEq, FromPrimitive)]
|
2014-09-06 20:44:53 -07: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-05 16:24:32 -08: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-10-14 18:08:34 -07:00
|
|
|
impl ser::Serialize for Country {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-10-14 18:08:34 -07:00
|
|
|
fn visit<
|
|
|
|
S,
|
|
|
|
R,
|
|
|
|
E,
|
|
|
|
V: ser::Visitor<S, R, E>,
|
|
|
|
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
|
|
|
|
visitor.visit_uint(state, *self as uint)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:13:17 -08:00
|
|
|
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for Country {
|
2014-09-06 20:44:53 -07:00
|
|
|
#[inline]
|
2014-12-05 22:19:30 -08:00
|
|
|
fn deserialize(state: &mut S) -> Result<Country, E> {
|
|
|
|
de::deserialize_from_primitive(state)
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 23:39:43 -08:00
|
|
|
enum LogField {
|
|
|
|
Timestamp,
|
|
|
|
ZoneId,
|
|
|
|
ZonePlan,
|
|
|
|
Http,
|
|
|
|
Origin,
|
|
|
|
Country,
|
|
|
|
CacheStatus,
|
|
|
|
ServerIp,
|
|
|
|
ServerName,
|
|
|
|
RemoteIp,
|
|
|
|
BytesDlv,
|
|
|
|
RayId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Deserialize<S, E> for LogField {
|
|
|
|
fn deserialize(state: &mut S) -> Result<LogField, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
'a,
|
|
|
|
S: de::Deserializer<E>,
|
|
|
|
E: de::Error,
|
|
|
|
> de::Visitor<S, LogField, E> for Visitor {
|
|
|
|
fn visit_str(&mut self, value: &str) -> Result<LogField, E> {
|
|
|
|
let x = match value {
|
|
|
|
"timestamp" => LogField::Timestamp,
|
|
|
|
"zone_id" => LogField::ZoneId,
|
|
|
|
"zone_plan" => LogField::ZonePlan,
|
|
|
|
"http" => LogField::Http,
|
|
|
|
"origin" => LogField::Origin,
|
|
|
|
"country" => LogField::Country,
|
|
|
|
"cache_status" => LogField::CacheStatus,
|
|
|
|
"server_ip" => LogField::ServerIp,
|
|
|
|
"server_name" => LogField::ServerName,
|
|
|
|
"remote_ip" => LogField::RemoteIp,
|
|
|
|
"bytes_dlv" => LogField::BytesDlv,
|
|
|
|
"ray_id" => LogField::RayId,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[deriving(Show, PartialEq, Encodable, Decodable)]
|
2014-10-14 18:08:34 -07:00
|
|
|
#[deriving_serialize]
|
|
|
|
//#[deriving_deserialize]
|
2014-09-06 20:44:53 -07: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-12-05 22:19:30 -08:00
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
2014-12-05 22:19:30 -08:00
|
|
|
> Deserialize<S, E> for Log {
|
|
|
|
fn deserialize(state: &mut S) -> Result<Log, E> {
|
|
|
|
struct Visitor;
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Deserializer<E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
E: de::Error,
|
2014-12-05 22:19:30 -08:00
|
|
|
> de::Visitor<S, Log, E> for Visitor {
|
|
|
|
fn visit_map<
|
|
|
|
Visitor: de::MapVisitor<S, E>,
|
2014-12-08 23:13:17 -08:00
|
|
|
>(&mut self, mut visitor: Visitor) -> Result<Log, E> {
|
2014-12-05 22:19:30 -08:00
|
|
|
let mut timestamp = None;
|
|
|
|
let mut zone_id = None;
|
|
|
|
let mut zone_plan = None;
|
|
|
|
let mut http = None;
|
|
|
|
let mut origin = None;
|
|
|
|
let mut country = None;
|
|
|
|
let mut cache_status = None;
|
|
|
|
let mut server_ip = None;
|
|
|
|
let mut server_name = None;
|
|
|
|
let mut remote_ip = None;
|
|
|
|
let mut bytes_dlv = None;
|
|
|
|
let mut ray_id = None;
|
|
|
|
|
|
|
|
loop {
|
2014-12-08 23:13:17 -08:00
|
|
|
match try!(visitor.visit_key()) {
|
2014-12-08 23:39:43 -08:00
|
|
|
Some(field) => {
|
|
|
|
match field {
|
|
|
|
LogField::Timestamp => { timestamp = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::ZoneId => { zone_id = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::ZonePlan => { zone_plan = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::Http => { http = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::Origin => { origin = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::Country => { country = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::CacheStatus => { cache_status = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::ServerIp => { server_ip = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::ServerName => { server_name = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::RemoteIp => { remote_ip = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::BytesDlv => { bytes_dlv = Some(try!(visitor.visit_value())); }
|
|
|
|
LogField::RayId => { ray_id = Some(try!(visitor.visit_value())); }
|
2014-12-05 22:19:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Log {
|
|
|
|
timestamp: timestamp.unwrap(),
|
|
|
|
zone_id: zone_id.unwrap(),
|
|
|
|
zone_plan: zone_plan.unwrap(),
|
|
|
|
http: http.unwrap(),
|
|
|
|
origin: origin.unwrap(),
|
|
|
|
country: country.unwrap(),
|
|
|
|
cache_status: cache_status.unwrap(),
|
|
|
|
server_ip: server_ip.unwrap(),
|
|
|
|
server_name: server_name.unwrap(),
|
|
|
|
remote_ip: remote_ip.unwrap(),
|
|
|
|
bytes_dlv: bytes_dlv.unwrap(),
|
|
|
|
ray_id: ray_id.unwrap(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.visit(&mut Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
impl Log {
|
|
|
|
fn new() -> Log {
|
|
|
|
Log {
|
2014-09-07 01:02:19 -07:00
|
|
|
timestamp: 2837513946597,
|
2014-09-06 20:44:53 -07:00
|
|
|
zone_id: 123456,
|
2014-12-05 16:24:32 -08:00
|
|
|
zone_plan: ZonePlan::FREE,
|
2014-09-06 20:44:53 -07:00
|
|
|
http: Http {
|
2014-12-05 16:24:32 -08:00
|
|
|
protocol: HttpProtocol::HTTP11,
|
2014-09-06 20:44:53 -07:00
|
|
|
status: 200,
|
|
|
|
host_status: 503,
|
|
|
|
up_status: 520,
|
2014-12-05 16:24:32 -08:00
|
|
|
method: HttpMethod::GET,
|
2014-09-06 20:44:53 -07: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-12-05 16:24:32 -08:00
|
|
|
protocol: OriginProtocol::HTTPS,
|
2014-09-06 20:44:53 -07:00
|
|
|
},
|
2014-12-05 16:24:32 -08:00
|
|
|
country: Country::US,
|
|
|
|
cache_status: CacheStatus::Hit,
|
2014-09-06 20:44:53 -07: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-09-07 01:19:36 -07: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 {
|
|
|
|
buf: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyMemWriter0 {
|
|
|
|
pub fn with_capacity(cap: uint) -> MyMemWriter0 {
|
|
|
|
MyMemWriter0 {
|
|
|
|
buf: Vec::with_capacity(cap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Writer for MyMemWriter0 {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
|
|
|
|
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-09-06 20:44:53 -07:00
|
|
|
}
|
2014-09-07 01:19:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LLVM isn't yet able to lower `Vec::push_all` into a memcpy, so this helps
|
|
|
|
// MemWriter eke out that last bit of performance.
|
|
|
|
//#[inline(always)]
|
|
|
|
fn push_all_bytes(dst: &mut Vec<u8>, src: &[u8]) {
|
|
|
|
let dst_len = dst.len();
|
|
|
|
let src_len = src.len();
|
|
|
|
|
2014-11-17 19:21:46 -08:00
|
|
|
dst.reserve(src_len);
|
2014-09-07 01:19:36 -07:00
|
|
|
|
|
|
|
unsafe {
|
2014-11-17 19:21:46 -08:00
|
|
|
// we would have failed if `reserve` overflowed.
|
2014-09-07 01:19:36 -07: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for MyMemWriter1 {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
|
|
|
|
push_all_bytes(&mut self.buf, buf);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08: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() {
|
|
|
|
use serialize::Encodable;
|
|
|
|
|
|
|
|
let log = Log::new();
|
|
|
|
|
|
|
|
let mut wr = Vec::with_capacity(1024);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer);
|
|
|
|
log.encode(&mut encoder).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(wr.as_slice(), JSON_STR.as_bytes());
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
#[bench]
|
|
|
|
fn bench_encoder(b: &mut Bencher) {
|
|
|
|
let log = Log::new();
|
2014-12-05 16:24:32 -08: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-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.clear();
|
|
|
|
|
|
|
|
let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer);
|
|
|
|
log.encode(&mut encoder).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_serializer() {
|
|
|
|
let log = Log::new();
|
|
|
|
let json = json::to_vec(&log).unwrap();
|
|
|
|
assert_eq!(json, JSON_STR.as_bytes());
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
#[bench]
|
|
|
|
fn bench_serializer(b: &mut Bencher) {
|
|
|
|
let log = Log::new();
|
|
|
|
let json = json::to_vec(&log).unwrap();
|
|
|
|
b.bytes = json.len() as u64;
|
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
let _ = json::to_vec(&log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_serializer_vec() {
|
|
|
|
let log = Log::new();
|
|
|
|
let wr = Vec::with_capacity(1024);
|
|
|
|
let mut serializer = json::Writer::new(wr);
|
|
|
|
serializer.visit(&log).unwrap();
|
|
|
|
|
2014-12-05 22:19:30 -08:00
|
|
|
let json = serializer.into_inner();
|
2014-12-05 16:24:32 -08:00
|
|
|
assert_eq!(json.as_slice(), JSON_STR.as_bytes());
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_serializer_vec(b: &mut Bencher) {
|
|
|
|
let log = Log::new();
|
|
|
|
let json = json::to_vec(&log).unwrap();
|
|
|
|
b.bytes = json.len() as u64;
|
|
|
|
|
|
|
|
let mut wr = Vec::with_capacity(1024);
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
b.iter(|| {
|
|
|
|
wr.clear();
|
|
|
|
|
|
|
|
let mut serializer = json::Writer::new(wr.by_ref());
|
|
|
|
serializer.visit(&log).unwrap();
|
2014-12-05 22:19:30 -08:00
|
|
|
let _json = serializer.into_inner();
|
2014-12-05 16:24:32 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_copy(b: &mut Bencher) {
|
|
|
|
let json = JSON_STR.as_bytes().to_vec();
|
2014-09-06 20:44:53 -07:00
|
|
|
b.bytes = json.len() as u64;
|
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
let _json = JSON_STR.as_bytes().to_vec();
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
fn manual_no_escape<W: Writer>(wr: &mut W, log: &Log) {
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str("{\"timestamp\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.timestamp)).unwrap();
|
|
|
|
wr.write_str(",\"zone_id\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.zone_id)).unwrap();
|
|
|
|
wr.write_str(",\"zone_plan\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "{}", log.zone_plan as uint)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
wr.write_str(",\"http\":{\"protocol\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.http.protocol as uint)).unwrap();
|
|
|
|
wr.write_str(",\"status\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.http.status)).unwrap();
|
|
|
|
wr.write_str(",\"host_status\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.http.host_status)).unwrap();
|
|
|
|
wr.write_str(",\"up_status\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.http.up_status)).unwrap();
|
|
|
|
wr.write_str(",\"method\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.http.method as uint)).unwrap();
|
|
|
|
wr.write_str(",\"content_type\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.http.content_type)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"user_agent\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.http.user_agent)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"referer\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.http.referer)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"request_uri\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.http.request_uri)).unwrap();
|
|
|
|
|
|
|
|
wr.write_str("},\"origin\":{").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str("\"ip\":").unwrap();
|
|
|
|
(write!(wr, "\"{}\"", log.origin.ip)).unwrap();
|
|
|
|
wr.write_str(",\"port\":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.origin.port)).unwrap();
|
|
|
|
wr.write_str(",\"hostname\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.origin.hostname)).unwrap();
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"protocol\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.origin.protocol as uint)).unwrap();
|
|
|
|
|
|
|
|
wr.write_str("},\"country\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.country as uint)).unwrap();
|
|
|
|
wr.write_str(",\"cache_status\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.cache_status as uint)).unwrap();
|
|
|
|
wr.write_str(",\"server_ip\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.server_ip)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"server_name\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.server_name)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"remote_ip\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.remote_ip)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str(",\"bytes_dlv\":").unwrap();
|
|
|
|
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
|
|
|
|
|
|
|
wr.write_str(",\"ray_id\":").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
(write!(wr, "\"{}\"", log.ray_id)).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str("}").unwrap();
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
fn manual_escape<W: Writer>(wr: &mut W, log: &Log) {
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str("{").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "timestamp").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.timestamp)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "zone_id").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.zone_id)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "zone_plan").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.zone_plan as int)).unwrap();
|
|
|
|
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "http").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":{").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "protocol").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.http.protocol as uint)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "status").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.http.status)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "host_status").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.http.host_status)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "up_status").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.http.up_status)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "method").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.http.method as uint)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "content_type").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.http.content_type.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "user_agent").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.http.user_agent.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "referer").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.http.referer.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "request_uri").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.http.request_uri.as_slice()).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str("},").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "origin").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":{").unwrap();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "ip").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.origin.ip.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "port").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.origin.port)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "hostname").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.origin.hostname.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "protocol").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.origin.protocol as uint)).unwrap();
|
|
|
|
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str("},").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "country").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.country as uint)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "cache_status").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.cache_status as uint)).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "server_ip").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.server_ip.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "server_name").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.server_name.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "remote_ip").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.remote_ip.as_slice()).unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "bytes_dlv").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
|
|
|
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(",").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, "ray_id").unwrap();
|
2014-09-07 01:04:39 -07:00
|
|
|
wr.write_str(":").unwrap();
|
2014-12-05 16:24:32 -08:00
|
|
|
escape_str(wr, log.ray_id.as_slice()).unwrap();
|
2014-09-06 20:44:53 -07:00
|
|
|
wr.write_str("}").unwrap();
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_vec_no_escape() {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = Vec::with_capacity(1024);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let json = String::from_utf8(wr).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_vec_no_escape(b: &mut Bencher) {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = Vec::with_capacity(1024);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.clear();
|
|
|
|
manual_no_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_vec_escape() {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = Vec::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let json = String::from_utf8(wr).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_vec_escape(b: &mut Bencher) {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = Vec::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.clear();
|
|
|
|
|
|
|
|
manual_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_my_mem_writer0_no_escape() {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter0::with_capacity(1000);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let json = String::from_utf8(wr.buf).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_my_mem_writer0_no_escape(b: &mut Bencher) {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter0::with_capacity(1024);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.buf.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.buf.clear();
|
|
|
|
|
|
|
|
manual_no_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_my_mem_writer0_escape() {
|
|
|
|
let log = Log::new();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter0::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
|
|
|
|
|
|
|
let json = String::from_utf8(wr.buf).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_my_mem_writer0_escape(b: &mut Bencher) {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter0::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.buf.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.buf.clear();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
manual_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_my_mem_writer1_no_escape() {
|
|
|
|
let log = Log::new();
|
|
|
|
|
|
|
|
let mut wr = MyMemWriter1::with_capacity(1024);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
|
|
|
|
|
|
|
let json = String::from_utf8(wr.buf).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
|
|
|
}
|
|
|
|
|
2014-09-06 20:44:53 -07:00
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_my_mem_writer1_no_escape(b: &mut Bencher) {
|
2014-09-06 20:44:53 -07:00
|
|
|
let log = Log::new();
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter1::with_capacity(1024);
|
|
|
|
manual_no_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.buf.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.buf.clear();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
manual_no_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
#[test]
|
|
|
|
fn test_manual_my_mem_writer1_escape() {
|
|
|
|
let log = Log::new();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter1::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let json = String::from_utf8(wr.buf).unwrap();
|
|
|
|
assert_eq!(JSON_STR, json.as_slice());
|
2014-09-06 20:44:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-12-05 16:24:32 -08:00
|
|
|
fn bench_manual_my_mem_writer1_escape(b: &mut Bencher) {
|
|
|
|
let log = Log::new();
|
2014-09-06 20:44:53 -07:00
|
|
|
|
2014-12-05 16:24:32 -08:00
|
|
|
let mut wr = MyMemWriter1::with_capacity(1024);
|
|
|
|
manual_escape(&mut wr, &log);
|
|
|
|
b.bytes = wr.buf.len() as u64;
|
2014-09-06 20:44:53 -07:00
|
|
|
|
|
|
|
b.iter(|| {
|
2014-12-05 16:24:32 -08:00
|
|
|
wr.buf.clear();
|
|
|
|
|
|
|
|
manual_escape(&mut wr, &log);
|
2014-09-06 20:44:53 -07:00
|
|
|
});
|
|
|
|
}
|
2014-12-05 22:19:30 -08:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_deserializer(b: &mut Bencher) {
|
|
|
|
b.bytes = JSON_STR.len() as u64;
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let _log: Log = json::from_str(JSON_STR).unwrap();
|
|
|
|
});
|
|
|
|
}
|