2015-03-19 09:35:07 -05:00
|
|
|
#![feature(custom_derive, collections, core, plugin, test)]
|
2014-06-20 18:32:30 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
2015-02-13 12:26:13 -06:00
|
|
|
#![plugin(serde_macros)]
|
2014-09-06 22:41:13 -05:00
|
|
|
|
|
|
|
extern crate serde;
|
2015-03-25 12:43:25 -05:00
|
|
|
extern crate rustc_serialize;
|
2014-06-20 18:32:30 -05:00
|
|
|
extern crate test;
|
|
|
|
|
2015-03-14 16:57:13 -05:00
|
|
|
use std::io::{self, Read, Write};
|
2015-01-04 19:18:50 -06:00
|
|
|
use std::num::FromPrimitive;
|
2014-06-20 18:32:30 -05:00
|
|
|
use test::Bencher;
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::de::{self, Deserialize, Deserializer};
|
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;
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::ser::{self, Serialize, Serializer};
|
2014-06-20 18:32:30 -05:00
|
|
|
|
2015-01-04 19:18:50 -06:00
|
|
|
use rustc_serialize::Encodable;
|
2014-12-05 18:24:32 -06:00
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, 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,
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, PartialEq, FromPrimitive)]
|
2014-06-20 18:32:30 -05:00
|
|
|
enum HttpProtocol {
|
|
|
|
HTTP_PROTOCOL_UNKNOWN,
|
|
|
|
HTTP10,
|
|
|
|
HTTP11,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for HttpProtocol {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-06 07:14:35 -06:00
|
|
|
(*self as usize).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for HttpProtocol {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<HttpProtocol, D::Error> {
|
2015-03-09 00:56:33 -05:00
|
|
|
match FromPrimitive::from_usize(try!(d.read_usize())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-16 20:17:50 -06:00
|
|
|
None => Err(d.error("cannot convert from usize")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for HttpProtocol {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for HttpProtocol {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: Deserializer,
|
|
|
|
>(state: &mut S) -> Result<HttpProtocol, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, PartialEq, FromPrimitive)]
|
2014-06-20 18:32:30 -05:00
|
|
|
enum HttpMethod {
|
|
|
|
METHOD_UNKNOWN,
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
DELETE,
|
|
|
|
PUT,
|
|
|
|
HEAD,
|
|
|
|
PURGE,
|
|
|
|
OPTIONS,
|
|
|
|
PROPFIND,
|
|
|
|
MKCOL,
|
|
|
|
PATCH,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for HttpMethod {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-06 07:14:35 -06:00
|
|
|
(*self as usize).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for HttpMethod {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<HttpMethod, D::Error> {
|
2015-03-09 00:56:33 -05:00
|
|
|
match FromPrimitive::from_usize(try!(d.read_usize())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-06 07:14:35 -06:00
|
|
|
None => Err(d.error("cannot convert from usize")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for HttpMethod {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for HttpMethod {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: de::Deserializer,
|
|
|
|
>(state: &mut S) -> Result<HttpMethod, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, PartialEq, FromPrimitive)]
|
2014-06-20 18:32:30 -05:00
|
|
|
enum CacheStatus {
|
|
|
|
CACHESTATUS_UNKNOWN,
|
|
|
|
Miss,
|
|
|
|
Expired,
|
|
|
|
Hit,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for CacheStatus {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
(*self as u8).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for CacheStatus {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<CacheStatus, D::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
match FromPrimitive::from_u8(try!(d.read_u8())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-16 20:17:50 -06:00
|
|
|
None => Err(d.error("cannot convert from u8")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for CacheStatus {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for CacheStatus {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: de::Deserializer,
|
|
|
|
>(state: &mut S) -> Result<CacheStatus, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, Deserialize)]
|
2014-06-20 18:32:30 -05:00
|
|
|
struct Origin {
|
|
|
|
ip: String,
|
|
|
|
port: u32,
|
|
|
|
hostname: String,
|
|
|
|
protocol: OriginProtocol,
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, PartialEq, FromPrimitive)]
|
2014-06-20 18:32:30 -05:00
|
|
|
enum OriginProtocol {
|
|
|
|
ORIGIN_PROTOCOL_UNKNOWN,
|
|
|
|
HTTP,
|
|
|
|
HTTPS,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for OriginProtocol {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
(*self as u8).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for OriginProtocol {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<OriginProtocol, D::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
match FromPrimitive::from_u8(try!(d.read_u8())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-16 20:17:50 -06:00
|
|
|
None => Err(d.error("cannot convert from u8")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for OriginProtocol {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for OriginProtocol {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: de::Deserializer,
|
|
|
|
>(state: &mut S) -> Result<OriginProtocol, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, PartialEq, FromPrimitive)]
|
2014-06-20 18:32:30 -05:00
|
|
|
enum ZonePlan {
|
|
|
|
ZONEPLAN_UNKNOWN,
|
|
|
|
FREE,
|
|
|
|
PRO,
|
|
|
|
BIZ,
|
|
|
|
ENT,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for ZonePlan {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
(*self as u8).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for ZonePlan {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<ZonePlan, D::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
match FromPrimitive::from_u8(try!(d.read_u8())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-16 20:17:50 -06:00
|
|
|
None => Err(d.error("cannot convert from u8")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for ZonePlan {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for ZonePlan {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: de::Deserializer,
|
|
|
|
>(state: &mut S) -> Result<ZonePlan, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 08:21:51 -06:00
|
|
|
#[derive(Copy, Debug, 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,
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Encodable for Country {
|
|
|
|
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
(*self as u8).encode(s)
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:51:59 -06:00
|
|
|
impl rustc_serialize::Decodable for Country {
|
|
|
|
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Country, D::Error> {
|
2015-02-16 20:17:50 -06:00
|
|
|
match FromPrimitive::from_u8(try!(d.read_u8())) {
|
2014-12-01 15:09:35 -06:00
|
|
|
Some(value) => Ok(value),
|
2015-02-16 20:17:50 -06:00
|
|
|
None => Err(d.error("cannot convert from u8")),
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl ser::Serialize for Country {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2015-03-12 09:33:34 -05:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
2015-03-11 13:05:46 -05:00
|
|
|
where S: ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.visit_u8(*self as u8)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
impl de::Deserialize for Country {
|
2014-08-02 18:06:05 -05:00
|
|
|
#[inline]
|
2015-03-09 00:56:33 -05:00
|
|
|
fn deserialize<
|
|
|
|
S: de::Deserializer,
|
|
|
|
>(state: &mut S) -> Result<Country, S::Error> {
|
2015-03-26 09:58:26 -05:00
|
|
|
state.visit(de::impls::PrimitiveVisitor::new())
|
2014-08-02 18:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, RustcEncodable, RustcDecodable, Serialize, 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 }
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 08:43:54 -06:00
|
|
|
);
|
2014-07-09 13:58:18 -05:00
|
|
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 08:43:54 -06:00
|
|
|
);
|
2014-07-09 13:58:18 -05:00
|
|
|
|
|
|
|
struct MyMemWriter0 {
|
2014-07-06 01:44:43 -05:00
|
|
|
buf: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:58:18 -05:00
|
|
|
impl MyMemWriter0 {
|
2015-02-06 07:14:35 -06:00
|
|
|
pub fn with_capacity(cap: usize) -> MyMemWriter0 {
|
2014-07-09 13:58:18 -05:00
|
|
|
MyMemWriter0 {
|
2014-07-06 01:44:43 -05:00
|
|
|
buf: Vec::with_capacity(cap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 16:57:13 -05:00
|
|
|
impl Write for MyMemWriter0 {
|
2014-07-06 01:44:43 -05:00
|
|
|
#[inline]
|
2015-02-14 12:01:22 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2014-07-09 13:58:18 -05:00
|
|
|
self.buf.push_all(buf);
|
2015-02-14 12:01:22 -06:00
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
2014-07-09 13:58:18 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyMemWriter1 {
|
|
|
|
buf: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyMemWriter1 {
|
2015-02-06 07:14:35 -06:00
|
|
|
pub fn with_capacity(cap: usize) -> MyMemWriter1 {
|
2014-07-09 13:58:18 -05:00
|
|
|
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);
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
::std::ptr::copy_nonoverlapping(
|
2014-07-17 23:28:31 -05:00
|
|
|
src.as_ptr(),
|
2015-04-02 00:51:02 -05:00
|
|
|
dst.as_mut_ptr().offset(dst_len as isize),
|
2014-07-17 23:28:31 -05:00
|
|
|
src_len);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 13:58:18 -05:00
|
|
|
|
2015-03-14 16:57:13 -05:00
|
|
|
impl Write for MyMemWriter1 {
|
2014-07-09 13:58:18 -05:00
|
|
|
#[inline]
|
2015-02-14 12:01:22 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2014-07-17 23:28:31 -05:00
|
|
|
push_all_bytes(&mut self.buf, buf);
|
2015-02-14 12:01:22 -06:00
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
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() {
|
2015-01-04 19:18:50 -06:00
|
|
|
use rustc_serialize::Encodable;
|
2014-12-01 15:20:37 -06:00
|
|
|
|
2014-12-01 15:09:35 -06:00
|
|
|
let log = Log::new();
|
2014-12-01 15:20:37 -06:00
|
|
|
|
2015-02-06 14:02:17 -06:00
|
|
|
let mut wr = String::with_capacity(1024);
|
2014-12-01 15:20:37 -06:00
|
|
|
|
|
|
|
{
|
2015-01-04 19:18:50 -06:00
|
|
|
let mut encoder = rustc_serialize::json::Encoder::new(&mut wr);
|
2014-12-01 15:20:37 -06:00
|
|
|
log.encode(&mut encoder).unwrap();
|
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&wr, &JSON_STR);
|
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
|
|
|
|
2015-02-06 14:02:17 -06:00
|
|
|
let mut wr = String::with_capacity(1024);
|
2014-12-05 18:24:32 -06:00
|
|
|
|
|
|
|
{
|
2015-01-04 19:18:50 -06:00
|
|
|
let mut encoder = rustc_serialize::json::Encoder::new(&mut wr);
|
2014-12-05 18:24:32 -06:00
|
|
|
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();
|
|
|
|
|
2015-01-04 19:18:50 -06:00
|
|
|
let mut encoder = rustc_serialize::json::Encoder::new(&mut wr);
|
2014-12-05 18:24:32 -06:00
|
|
|
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);
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2014-12-01 15:09:35 -06:00
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
let json = serializer.into_inner();
|
|
|
|
assert_eq!(&json, &JSON_STR.as_bytes());
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
2014-07-17 23:28:31 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-18 10:44:34 -06:00
|
|
|
#[bench]
|
|
|
|
fn bench_serializer_slice(b: &mut Bencher) {
|
|
|
|
let log = Log::new();
|
|
|
|
let json = json::to_vec(&log);
|
|
|
|
b.bytes = json.len() as u64;
|
|
|
|
|
2015-01-04 19:18:50 -06:00
|
|
|
let mut buf = [0; 1024];
|
2014-12-18 10:44:34 -06:00
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
for item in buf.iter_mut(){ *item = 0; }
|
2015-03-09 00:56:33 -05:00
|
|
|
let mut wr = &mut buf[..];
|
2014-12-18 10:44:34 -06:00
|
|
|
|
|
|
|
let mut serializer = json::Serializer::new(wr.by_ref());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
2014-12-18 10:44:34 -06: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());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
2014-12-01 15:20:37 -06:00
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&wr.buf, &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());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
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());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
2014-12-01 15:20:37 -06:00
|
|
|
}
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&wr.buf, &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());
|
2015-03-12 09:33:34 -05:00
|
|
|
log.serialize(&mut serializer).unwrap();
|
2015-03-09 00:56:33 -05:00
|
|
|
let _json = serializer.into_inner();
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-14 16:57:13 -05:00
|
|
|
fn manual_serialize_no_escape<W: Write>(wr: &mut W, log: &Log) {
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b"{\"timestamp\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.timestamp)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"zone_id\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.zone_id)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"zone_plan\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.zone_plan as usize)).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"http\":{\"protocol\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"status\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"host_status\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.host_status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"up_status\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.up_status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"method\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.http.method as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"content_type\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.http.content_type)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"user_agent\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.http.user_agent)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"referer\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.http.referer)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"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
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b"},\"origin\":{").unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b"\"ip\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.origin.ip)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"port\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.origin.port)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"hostname\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.origin.hostname)).unwrap();
|
2014-09-07 03:04:39 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"protocol\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b"},\"country\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.country as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"cache_status\":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.cache_status as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"server_ip\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.server_ip)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"server_name\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.server_name)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"remote_ip\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.remote_ip)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"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
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b",\"ray_id\":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "\"{}\"", log.ray_id)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write(b"}").unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 16:57:13 -05:00
|
|
|
fn manual_serialize_escape<W: Write>(wr: &mut W, log: &Log) {
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b"{").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "timestamp").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.timestamp)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "zone_id").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.zone_id)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "zone_plan").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.zone_plan as isize)).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "http").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":{").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "protocol").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "status").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "host_status").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.host_status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "up_status").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.http.up_status)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "method").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.http.method as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "content_type").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.http.content_type).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "user_agent").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.http.user_agent).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "referer").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.http.referer).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "request_uri").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.http.request_uri).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b"},").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "origin").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":{").unwrap();
|
2014-09-07 03:04:39 -05:00
|
|
|
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "ip").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.origin.ip).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "port").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.origin.port)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "hostname").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.origin.hostname).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "protocol").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b"},").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "country").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.country as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "cache_status").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:14:35 -06:00
|
|
|
(write!(wr, "{}", log.cache_status as usize)).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "server_ip").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.server_ip).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "server_name").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.server_name).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "remote_ip").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.remote_ip).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "bytes_dlv").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
2014-07-06 01:44:43 -05:00
|
|
|
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b",").unwrap();
|
2014-12-05 18:24:32 -06:00
|
|
|
escape_str(wr, "ray_id").unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b":").unwrap();
|
2015-02-06 07:08:02 -06:00
|
|
|
escape_str(wr, &log.ray_id).unwrap();
|
2015-02-14 12:19:15 -06:00
|
|
|
wr.write_all(b"}").unwrap();
|
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_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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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();
|
2015-03-09 00:56:33 -05:00
|
|
|
assert_eq!(&JSON_STR, &json);
|
2014-12-01 15:09:35 -06:00
|
|
|
}
|
|
|
|
|
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-06-20 18:32:30 -05:00
|
|
|
#[bench]
|
2014-06-22 09:33:45 -05:00
|
|
|
fn bench_decoder(b: &mut Bencher) {
|
2015-01-04 19:18:50 -06:00
|
|
|
use rustc_serialize::json::Json;
|
|
|
|
|
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(|| {
|
2015-01-04 19:18:50 -06:00
|
|
|
let json = Json::from_str(JSON_STR).unwrap();
|
|
|
|
let mut decoder = rustc_serialize::json::Decoder::new(json);
|
|
|
|
let _log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap();
|
2014-06-20 18:32:30 -05:00
|
|
|
});
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
}
|