Skip to content

[wip] improve lua performance #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
use hlua::{Lua, LuaError};
use hlua::{Lua, LuaError, AnyLuaValue};
use std::collections::BTreeMap;

pub fn show_log_entry(log_entry: &BTreeMap<String, String>, filter_expr: &str, implicit_return: bool) -> Result<bool, LuaError> {
let mut lua = Lua::new();
lua.openlibs();

for (key, value) in log_entry {
lua.set(key.to_owned(), value.to_owned());
pub struct Filter<'filter> {
lua: Lua<'filter>
}

impl<'filter> Filter<'filter> {

pub fn new() -> Filter<'filter> {
let mut lua = Lua::new();
lua.openlibs();
Filter{ lua: lua }
}

if implicit_return {
lua.execute(&format!("return {};", filter_expr))
} else {
lua.execute(filter_expr)
pub fn show_log_entry(&mut self, log_entry: &BTreeMap<String, String>, filter_expr: &str, implicit_return: bool) -> Result<bool, LuaError> {

for (key, value) in log_entry {
self.lua.set(key.to_owned(), value.to_owned());
}

let result: Result<bool, LuaError> = if implicit_return {
self.lua.execute(&format!("return {};", filter_expr))
} else {
self.lua.execute(filter_expr)
};

for key in log_entry.keys() {
self.lua.set(key.to_owned(), AnyLuaValue::LuaNil);
}

result
}
}

Expand All @@ -32,54 +50,62 @@ mod tests {
#[test]
fn allow_all() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, "true", true).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, "true", true).unwrap());
}

#[test]
fn deny_all() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(false, show_log_entry(&log_entry, "false", true).unwrap());
let mut filter = Filter::new();
assert_eq!(false, filter.show_log_entry(&log_entry, "false", true).unwrap());
}

#[test]
fn filter_process() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, r#"process == "rust""#, true).unwrap());
assert_eq!(false, show_log_entry(&log_entry, r#"process == "meep""#, true).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, r#"process == "rust""#, true).unwrap());
assert_eq!(false, filter.show_log_entry(&log_entry, r#"process == "meep""#, true).unwrap());
}

#[test]
fn filter_logical_operators() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, r#"process == "rust" and fu == "bower""#, true).unwrap());
assert_eq!(true, show_log_entry(&log_entry, r#"process == "rust" or fu == "bauer""#, true).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, r#"process == "rust" and fu == "bower""#, true).unwrap());
assert_eq!(true, filter.show_log_entry(&log_entry, r#"process == "rust" or fu == "bauer""#, true).unwrap());
}

#[test]
fn filter_contains() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, r#"string.find(message, "something") ~= nil"#, true).unwrap());
assert_eq!(false, show_log_entry(&log_entry, r#"string.find(message, "bla") ~= nil"#, true).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, r#"string.find(message, "something") ~= nil"#, true).unwrap());
assert_eq!(false, filter.show_log_entry(&log_entry, r#"string.find(message, "bla") ~= nil"#, true).unwrap());
}

#[test]
fn filter_regex() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, r#"string.find(fu, "bow.*") ~= nil"#, true).unwrap());
assert_eq!(false, show_log_entry(&log_entry, r#"string.find(fu, "bow.*sd") ~= nil"#, true).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, r#"string.find(fu, "bow.*") ~= nil"#, true).unwrap());
assert_eq!(false, filter.show_log_entry(&log_entry, r#"string.find(fu, "bow.*sd") ~= nil"#, true).unwrap());
}

#[test]
fn unknown_variable() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(false, show_log_entry(&log_entry, r#"sdkfjsdfjsf ~= nil and string.find(sdkfjsdfjsf, "bow.*") ~= nil"#, true).unwrap());
let mut filter = Filter::new();
assert_eq!(false, filter.show_log_entry(&log_entry, r#"sdkfjsdfjsf ~= nil and string.find(sdkfjsdfjsf, "bow.*") ~= nil"#, true).unwrap());
}

#[test]
fn no_implicit_return() {
let log_entry: BTreeMap<String, String> = test_log_entry();
assert_eq!(true, show_log_entry(&log_entry, r#"if 3 > 2 then return true else return false end"#, false).unwrap());
assert_eq!(false, show_log_entry(&log_entry, r#"if 1 > 2 then return true else return false end"#, false).unwrap());
let mut filter = Filter::new();
assert_eq!(true, filter.show_log_entry(&log_entry, r#"if 3 > 2 then return true else return false end"#, false).unwrap());
assert_eq!(false, filter.show_log_entry(&log_entry, r#"if 1 > 2 then return true else return false end"#, false).unwrap());
}

}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fn main() {
let implicit_return = !matches.is_present("no-implicit-filter-return-statement");
let maybe_filter = matches.value_of("filter");

let mut lua_filter = filter::Filter::new();

let stdin = io::stdin();
let reader = stdin.lock();
for line in reader.lines() {
Expand All @@ -42,7 +44,7 @@ fn main() {
Ok(Value::Object(log_entry)) => {
let string_log_entry = &extract_string_values(&log_entry);
if let Some(filter) = maybe_filter {
match filter::show_log_entry(string_log_entry, filter, implicit_return) {
match lua_filter.show_log_entry(string_log_entry, filter, implicit_return) {
Ok(true) => log::print_log_line(&mut io::stdout(), string_log_entry, &additional_values, dump_all),
Ok(false) => (),
Err(e) => {
Expand Down