SHARE
TWEET
Untitled
a guest
Dec 13th, 2018
291
Never
- use filebuffer::FileBuffer;
- use serde_derive::Deserialize;
- use serde_derive::Serialize;
- use std::env;
- use std::str;
- fn main() {
- let args: Vec<String> = env::args().collect();
- let mut log = Log::new();
- log.import(&args[1]);
- println!("{}", log.report());
- }
- #[derive(Debug)]
- struct Log {
- name: String,
- interval: String, // In milliseconds
- stats: (u32, u32), // Average LiveObjects, HeapAlloc in KiB
- entries: Vec<LogEntry>,
- }
- impl Log {
- fn new() -> Log {
- Log {
- name: String::new(),
- interval: String::new(),
- stats: (0, 0),
- entries: Vec::new(),
- }
- }
- fn import(&mut self, filename: &str) {
- let fbuffer = FileBuffer::open(&filename).expect("Failed to open file.");
- let iter = fbuffer.split(|byte| *byte == b'\n');
- let mut set_m = false;
- self.entries.reserve({
- let size_hint = iter.size_hint();
- let mut size = 0;
- if let Some(x) = size_hint.1 {
- size = x
- }
- size
- });
- for b_line in iter {
- let byte_len = b_line.len();
- if byte_len > 1 {
- let line = str::from_utf8(&b_line[..byte_len]).unwrap();
- let raw: LogEntryRaw = serde_json::from_str(line).unwrap();
- // self.entries.push(LogEntry::from_raw(&raw));
- // let raw: LogEntryRaw = serde_json::from_slice(b_line).unwrap();
- if !set_m {
- let misc = raw.get_meta();
- self.name = misc.0;
- self.interval = misc.1;
- set_m = true;
- }
- self.entries.push(LogEntry::from_raw(&raw));
- }
- }
- let num_entries: u128 = self.entries.len() as u128;
- let mut total_lo: u128 = 0;
- let mut total_ha: u128 = 0;
- for entry in &self.entries {
- total_lo += u128::from(entry.liveobjects);
- total_ha += u128::from(entry.heapalloc);
- }
- self.stats.0 = (total_lo / num_entries) as u32;
- self.stats.1 = (total_ha / num_entries) as u32;
- }
- fn report(&self) -> String {
- let mut ebuf: Vec<u8> = Vec::new();
- ebuf.reserve(self.entries.len() * 72);
- ebuf.extend_from_slice(b"Name: ");
- ebuf.extend_from_slice(self.name.as_bytes());
- ebuf.extend_from_slice(b"\nInterval: ");
- ebuf.extend_from_slice(self.interval.as_bytes());
- ebuf.extend_from_slice(b"ms\n\n");
- ebuf.extend_from_slice(b"Average LiveObjects: ");
- ebuf.extend_from_slice(self.stats.0.to_string().as_bytes());
- ebuf.extend_from_slice(b"\nAverage HeapAlloc: ");
- ebuf.extend_from_slice(self.stats.1.to_string().as_bytes());
- ebuf.extend_from_slice(b"KiB\n\n");
- ebuf.extend_from_slice(b"Entries: \n\n");
- for entry in &self.entries {
- ebuf.extend_from_slice(b"LiveObjects: ");
- ebuf.extend_from_slice(entry.liveobjects.to_string().as_bytes());
- ebuf.extend_from_slice(b", HeapAlloc: ");
- ebuf.extend_from_slice(entry.heapalloc.to_string().as_bytes());
- ebuf.extend_from_slice(b"KiB, GCAmount: ");
- ebuf.extend_from_slice(entry.gc_amount.to_string().as_bytes());
- ebuf.extend_from_slice(b", Goroutines: ");
- ebuf.extend_from_slice(entry.num_goroutine.to_string().as_bytes());
- ebuf.extend_from_slice(b"\n");
- }
- String::from_utf8(ebuf).unwrap()
- }
- }
- #[derive(Debug)]
- struct LogEntry {
- liveobjects: u32,
- heapalloc: u32, // In KiB
- gc_amount: u32,
- num_goroutine: u32,
- }
- impl LogEntry {
- #[inline(always)]
- fn from_raw(raw_entry: &LogEntryRaw) -> LogEntry {
- LogEntry {
- liveobjects: raw_entry.LiveObjects,
- heapalloc: (raw_entry.Alloc / 1024) as u32, // Bytes to KiB
- gc_amount: raw_entry.NumGC,
- num_goroutine: raw_entry.NumGoroutine,
- }
- }
- }
- #[allow(non_snake_case)]
- #[derive(Serialize, Deserialize, Debug)]
- struct LogEntryRaw {
- Name: String,
- Interval: u32,
- LiveObjects: u32,
- Alloc: u64,
- NumGC: u32,
- NumGoroutine: u32,
- }
- impl LogEntryRaw {
- fn get_meta(&self) -> (String, String) {
- (self.Name.clone(), self.Interval.to_string())
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.


