Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
  • Loading branch information
divagant-martian authored and ackintosh committed Sep 10, 2022
1 parent 639cb0a commit ca0f583
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -46,8 +46,10 @@ harness = false
name = "text"
path = "benches/encoding/text.rs"
harness = false
required-features = []

[[bench]]
name = "proto"
path = "benches/encoding/proto.rs"
harness = false
required-features = ["protobuf"]
8 changes: 5 additions & 3 deletions benches/encoding/proto.rs
@@ -1,7 +1,9 @@
// Benchmark inspired by https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs
// Benchmark inspired by
// https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs:write

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use prometheus_client::encoding::proto::{encode, Encode, EncodeMetric};
use prometheus_client::encoding::proto::{encode, EncodeMetric};
use prometheus_client::encoding::Encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
Expand All @@ -17,7 +19,7 @@ pub fn proto(c: &mut Criterion) {
some_number: u64,
}

#[derive(Clone, Hash, PartialEq, Eq)]
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
enum Method {
Get,
#[allow(dead_code)]
Expand Down
21 changes: 21 additions & 0 deletions benches/encoding/text.rs
Expand Up @@ -2,6 +2,7 @@

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use prometheus_client::encoding::text::{encode, Encode, EncodeMetric};
use prometheus_client::encoding::Encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
Expand Down Expand Up @@ -33,6 +34,26 @@ pub fn text(c: &mut Criterion) {
Five,
}

#[cfg(feature = "protobuf")]
impl std::fmt::Display for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Method::Get => f.write_str("Get"),
Method::Put => f.write_str("Put"),
}
}
}

#[cfg(feature = "protobuf")]
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Two => f.write_str("2"),
Status::Four => f.write_str("4"),
Status::Five => f.write_str("5"),
}
}
}
impl Encode for Status {
fn encode(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
let status = match self {
Expand Down
10 changes: 6 additions & 4 deletions derive-encode/src/lib.rs
Expand Up @@ -61,7 +61,7 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
syn::Data::Union(_) => panic!("Can not derive Encode for union."),
};

let mut gen = quote! {
let gen = quote! {
impl prometheus_client::encoding::text::Encode for #name {
fn encode(&self, writer: &mut dyn std::io::Write) -> std::result::Result<(), std::io::Error> {
#body
Expand All @@ -71,18 +71,20 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
}
};

if cfg!(feature = "protobuf") {
#[cfg(feature = "protobuf")]
let gen = {
let protobuf = derive_protobuf_encode(ast);
gen = quote! {
quote! {
#gen

#protobuf
}
}
};

gen.into()
}

#[cfg(feature = "protobuf")]
fn derive_protobuf_encode(ast: DeriveInput) -> TokenStream2 {
let name = &ast.ident;

Expand Down
1 change: 1 addition & 0 deletions derive-encode/tests/lib.rs
@@ -1,4 +1,5 @@
use prometheus_client::encoding::text::{encode, Encode};
use prometheus_client::encoding::Encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
Expand Down

0 comments on commit ca0f583

Please sign in to comment.