Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 291 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. use filebuffer::FileBuffer;
  2. use serde_derive::Deserialize;
  3. use serde_derive::Serialize;
  4. use std::env;
  5. use std::str;
  6.  
  7. fn main() {
  8.     let args: Vec<String> = env::args().collect();
  9.     let mut log = Log::new();
  10.     log.import(&args[1]);
  11.     println!("{}", log.report());
  12. }
  13.  
  14. #[derive(Debug)]
  15. struct Log {
  16.     name: String,
  17.     interval: String,  // In milliseconds
  18.     stats: (u32, u32), // Average LiveObjects, HeapAlloc in KiB
  19.     entries: Vec<LogEntry>,
  20. }
  21.  
  22. impl Log {
  23.     fn new() -> Log {
  24.         Log {
  25.             name: String::new(),
  26.             interval: String::new(),
  27.             stats: (0, 0),
  28.             entries: Vec::new(),
  29.         }
  30.     }
  31.  
  32.     fn import(&mut self, filename: &str) {
  33.         let fbuffer = FileBuffer::open(&filename).expect("Failed to open file.");
  34.         let iter = fbuffer.split(|byte| *byte == b'\n');
  35.         let mut set_m = false;
  36.  
  37.         self.entries.reserve({
  38.             let size_hint = iter.size_hint();
  39.             let mut size = 0;
  40.             if let Some(x) = size_hint.1 {
  41.                 size = x
  42.             }
  43.  
  44.             size
  45.         });
  46.  
  47.         for b_line in iter {
  48.             let byte_len = b_line.len();
  49.             if byte_len > 1 {
  50.                 let line = str::from_utf8(&b_line[..byte_len]).unwrap();
  51.                 let raw: LogEntryRaw = serde_json::from_str(line).unwrap();
  52.                 // self.entries.push(LogEntry::from_raw(&raw));
  53.  
  54.                 // let raw: LogEntryRaw = serde_json::from_slice(b_line).unwrap();
  55.                 if !set_m {
  56.                     let misc = raw.get_meta();
  57.                     self.name = misc.0;
  58.                     self.interval = misc.1;
  59.                     set_m = true;
  60.                 }
  61.                 self.entries.push(LogEntry::from_raw(&raw));
  62.             }
  63.         }
  64.  
  65.         let num_entries: u128 = self.entries.len() as u128;
  66.         let mut total_lo: u128 = 0;
  67.         let mut total_ha: u128 = 0;
  68.  
  69.         for entry in &self.entries {
  70.             total_lo += u128::from(entry.liveobjects);
  71.             total_ha += u128::from(entry.heapalloc);
  72.         }
  73.  
  74.         self.stats.0 = (total_lo / num_entries) as u32;
  75.         self.stats.1 = (total_ha / num_entries) as u32;
  76.     }
  77.  
  78.     fn report(&self) -> String {
  79.         let mut ebuf: Vec<u8> = Vec::new();
  80.         ebuf.reserve(self.entries.len() * 72);
  81.  
  82.         ebuf.extend_from_slice(b"Name: ");
  83.         ebuf.extend_from_slice(self.name.as_bytes());
  84.  
  85.         ebuf.extend_from_slice(b"\nInterval: ");
  86.         ebuf.extend_from_slice(self.interval.as_bytes());
  87.         ebuf.extend_from_slice(b"ms\n\n");
  88.  
  89.         ebuf.extend_from_slice(b"Average LiveObjects: ");
  90.         ebuf.extend_from_slice(self.stats.0.to_string().as_bytes());
  91.  
  92.         ebuf.extend_from_slice(b"\nAverage HeapAlloc: ");
  93.         ebuf.extend_from_slice(self.stats.1.to_string().as_bytes());
  94.         ebuf.extend_from_slice(b"KiB\n\n");
  95.  
  96.         ebuf.extend_from_slice(b"Entries: \n\n");
  97.  
  98.         for entry in &self.entries {
  99.             ebuf.extend_from_slice(b"LiveObjects: ");
  100.             ebuf.extend_from_slice(entry.liveobjects.to_string().as_bytes());
  101.  
  102.             ebuf.extend_from_slice(b", HeapAlloc: ");
  103.             ebuf.extend_from_slice(entry.heapalloc.to_string().as_bytes());
  104.  
  105.             ebuf.extend_from_slice(b"KiB, GCAmount: ");
  106.             ebuf.extend_from_slice(entry.gc_amount.to_string().as_bytes());
  107.  
  108.             ebuf.extend_from_slice(b", Goroutines: ");
  109.             ebuf.extend_from_slice(entry.num_goroutine.to_string().as_bytes());
  110.  
  111.             ebuf.extend_from_slice(b"\n");
  112.         }
  113.  
  114.         String::from_utf8(ebuf).unwrap()
  115.     }
  116. }
  117.  
  118. #[derive(Debug)]
  119. struct LogEntry {
  120.     liveobjects: u32,
  121.     heapalloc: u32, // In KiB
  122.     gc_amount: u32,
  123.     num_goroutine: u32,
  124. }
  125.  
  126. impl LogEntry {
  127.     #[inline(always)]
  128.     fn from_raw(raw_entry: &LogEntryRaw) -> LogEntry {
  129.         LogEntry {
  130.             liveobjects: raw_entry.LiveObjects,
  131.             heapalloc: (raw_entry.Alloc / 1024) as u32, // Bytes to KiB
  132.             gc_amount: raw_entry.NumGC,
  133.             num_goroutine: raw_entry.NumGoroutine,
  134.         }
  135.     }
  136. }
  137.  
  138. #[allow(non_snake_case)]
  139. #[derive(Serialize, Deserialize, Debug)]
  140. struct LogEntryRaw {
  141.     Name: String,
  142.     Interval: u32,
  143.     LiveObjects: u32,
  144.     Alloc: u64,
  145.     NumGC: u32,
  146.     NumGoroutine: u32,
  147. }
  148.  
  149. impl LogEntryRaw {
  150.     fn get_meta(&self) -> (String, String) {
  151.         (self.Name.clone(), self.Interval.to_string())
  152.     }
  153. }
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. OK, I Understand
Pastebin PRO 'CHRISTMAS SPECIAL'!
Get 60% OFF Pastebin PRO accounts!
 
Top