use criterion::{black_box, criterion_group, criterion_main, Criterion}; use flodgatt::{ messages::*, parse_client_request::{Content::*, Reach::*, Stream::*, Timeline}, redis_to_client_stream::{RedisMsg, RedisParseOutput}, }; use lru::LruCache; use std::convert::TryFrom; fn parse_long_redis_input<'a>(input: &'a str) -> RedisMsg<'a> { if let RedisParseOutput::Msg(msg) = RedisParseOutput::try_from(input).unwrap() { assert_eq!(msg.timeline_txt, "timeline:1"); msg } else { panic!() } } fn parse_to_timeline(msg: RedisMsg) -> Timeline { let trimmed_tl_txt = &msg.timeline_txt["timeline:".len()..]; let tl = Timeline::from_redis_text(trimmed_tl_txt, &mut LruCache::new(1000)).unwrap(); assert_eq!(tl, Timeline(User(1), Federated, All)); tl } fn parse_to_checked_event(msg: RedisMsg) -> Event { Event::TypeSafe(serde_json::from_str(msg.event_txt).unwrap()) } fn parse_to_dyn_event(msg: RedisMsg) -> String { let event: Event = Event::Dynamic(serde_json::from_str(msg.event_txt).unwrap()); event.to_json_string() } fn redis_msg_to_event_string(msg: RedisMsg) -> String { msg.event_txt.to_string() } fn string_to_checked_event(event_txt: &String) -> Event { Event::TypeSafe(serde_json::from_str(event_txt).unwrap()) } fn criterion_benchmark(c: &mut Criterion) { let input = ONE_MESSAGE_FOR_THE_USER_TIMLINE_FROM_REDIS; let mut group = c.benchmark_group("Parse redis RESP array"); // group.bench_function("parse redis input to RedisMsg", |b| { // b.iter(|| black_box(parse_long_redis_input(input))) // }); let msg = parse_long_redis_input(input); // group.bench_function("parse RedisMsg to Timeline", |b| { // b.iter(|| black_box(parse_to_timeline(msg.clone()))) // }); group.bench_function("parse RedisMsg -> DynamicEvent -> JSON string", |b| { b.iter(|| black_box(parse_to_dyn_event(msg.clone()))) }); group.bench_function("parse RedisMsg -> CheckedEvent", |b| { b.iter(|| black_box(parse_to_checked_event(msg.clone()))) }); group.bench_function("parse RedisMsg -> String -> CheckedEvent", |b| { b.iter(|| { let txt = black_box(redis_msg_to_event_string(msg.clone())); black_box(string_to_checked_event(&txt)); }) }); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches); const ONE_MESSAGE_FOR_THE_USER_TIMLINE_FROM_REDIS: &str = "*3\r\n$7\r\nmessage\r\n$10\r\ntimeline:1\r\n$3790\r\n{\"event\":\"update\",\"payload\":{\"id\":\"102775370117886890\",\"created_at\":\"2019-09-11T18:42:19.000Z\",\"in_reply_to_id\":null,\"in_reply_to_account_id\":null,\"sensitive\":false,\"spoiler_text\":\"\",\"visibility\":\"unlisted\",\"language\":\"en\",\"uri\":\"https://mastodon.host/users/federationbot/statuses/102775346916917099\",\"url\":\"https://mastodon.host/@federationbot/102775346916917099\",\"replies_count\":0,\"reblogs_count\":0,\"favourites_count\":0,\"favourited\":false,\"reblogged\":false,\"muted\":false,\"content\":\"

Trending tags:
#neverforget
#4styles
#newpipe
#uber
#mercredifiction

\",\"reblog\":null,\"account\":{\"id\":\"78\",\"username\":\"federationbot\",\"acct\":\"federationbot@mastodon.host\",\"display_name\":\"Federation Bot\",\"locked\":false,\"bot\":false,\"created_at\":\"2019-09-10T15:04:25.559Z\",\"note\":\"

Hello, I am mastodon.host official semi bot.

Follow me if you want to have some updates on the view of the fediverse from here ( I only post unlisted ).

I also randomly boost one of my followers toot every hour !

If you don\'t feel confortable with me following you, tell me: unfollow and I\'ll do it :)

If you want me to follow you, just tell me follow !

If you want automatic follow for new users on your instance and you are an instance admin, contact me !

Other commands are private :)

\",\"url\":\"https://mastodon.host/@federationbot\",\"avatar\":\"https://instance.codesections.com/system/accounts/avatars/000/000/078/original/d9e2be5398629cf8.jpeg?1568127863\",\"avatar_static\":\"https://instance.codesections.com/system/accounts/avatars/000/000/078/original/d9e2be5398629cf8.jpeg?1568127863\",\"header\":\"https://instance.codesections.com/headers/original/missing.png\",\"header_static\":\"https://instance.codesections.com/headers/original/missing.png\",\"followers_count\":16636,\"following_count\":179532,\"statuses_count\":50554,\"emojis\":[],\"fields\":[{\"name\":\"More stats\",\"value\":\"https://mastodon.host/stats.html\",\"verified_at\":null},{\"name\":\"More infos\",\"value\":\"https://mastodon.host/about/more\",\"verified_at\":null},{\"name\":\"Owner/Friend\",\"value\":\"@gled\",\"verified_at\":null}]},\"media_attachments\":[],\"mentions\":[],\"tags\":[{\"name\":\"4styles\",\"url\":\"https://instance.codesections.com/tags/4styles\"},{\"name\":\"neverforget\",\"url\":\"https://instance.codesections.com/tags/neverforget\"},{\"name\":\"mercredifiction\",\"url\":\"https://instance.codesections.com/tags/mercredifiction\"},{\"name\":\"uber\",\"url\":\"https://instance.codesections.com/tags/uber\"},{\"name\":\"newpipe\",\"url\":\"https://instance.codesections.com/tags/newpipe\"}],\"emojis\":[],\"card\":null,\"poll\":null},\"queued_at\":1568227693541}\r\n";