From 4579582e7537f0007a1226e6e4428df26b06571c Mon Sep 17 00:00:00 2001 From: Vedat Can Keklik Date: Sun, 8 Dec 2024 22:45:17 +0300 Subject: [PATCH] fix: Script updates and benchmarks --- lykiadb-lang/Cargo.toml | 7 +++ lykiadb-lang/benches/parsing.rs | 71 +++++++++++++++++++++++++++ lykiadb-server/benches/scripts/fib.ly | 6 +-- lykiadb-shell/examples/compute.lyql | 2 +- lykiadb-shell/examples/fib.lyql | 12 ++--- lykiadb-shell/examples/fn.lyql | 18 +++---- lykiadb-shell/examples/hof.lyql | 10 ++-- lykiadb-shell/examples/objects.lyql | 8 +-- lykiadb-shell/examples/scan_err.lyql | 4 +- lykiadb-shell/examples/sql.lyql | 6 +-- 10 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 lykiadb-lang/benches/parsing.rs diff --git a/lykiadb-lang/Cargo.toml b/lykiadb-lang/Cargo.toml index f668365..57e8e53 100644 --- a/lykiadb-lang/Cargo.toml +++ b/lykiadb-lang/Cargo.toml @@ -11,3 +11,10 @@ serde = { version = "1.0.188", features=["derive", "rc"] } serde_json = "1.0.105" assert-json-diff = "2.0.2" derivative = "2.2.0" + +[dev-dependencies] +criterion = { version = "0.4", features = ["html_reports"] } + +[[bench]] +name = "parsing" +harness = false diff --git a/lykiadb-lang/benches/parsing.rs b/lykiadb-lang/benches/parsing.rs new file mode 100644 index 0000000..9b7c861 --- /dev/null +++ b/lykiadb-lang/benches/parsing.rs @@ -0,0 +1,71 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use lykiadb_lang::{parser::{program::Program, resolver::Resolver, Parser}, tokenizer::scanner::Scanner, Locals, Scopes}; +use rustc_hash::FxHashMap; + +pub struct ParserBenchmark { + scopes: Scopes, + locals: Locals, +} + + +impl ParserBenchmark { + pub fn new() -> ParserBenchmark { + ParserBenchmark { + scopes: vec![], + locals: FxHashMap::default(), + } + } + + pub fn process(&mut self, source: &str) -> Program { + let tokens = Scanner::scan(source).unwrap(); + let mut program = Parser::parse(&tokens).unwrap(); + let mut resolver = Resolver::new(self.scopes.clone(), &program, Some(self.locals.clone())); + let (scopes, locals) = resolver.resolve().unwrap(); + + self.scopes = scopes; + self.locals.clone_from(&locals); + program.set_locals(self.locals.clone()); + + program + } +} + +fn runtime() { + let content: String = " + SELECT * FROM books b + INNER JOIN + ( + categories c + INNER JOIN + publishers AS p + ON b.category_id = c.id + ) + ON b.publisher_id = p.id + WHERE p.name = 'Springer' + UNION + SELECT * FROM books + INTERSECT + SELECT * FROM books + EXCEPT + SELECT * FROM books; + + ".to_string(); + let mut parser = black_box(ParserBenchmark::new()); + black_box(parser.process(black_box(&content))); +} + +fn bench(c: &mut Criterion) { + let mut group = c.benchmark_group("sample-size-example"); + group.bench_function("2-way join", |b| { + b.iter(|| runtime()) + }); + group.finish(); +} + +criterion_group! { + name = benches; + config = Criterion::default(); + targets = bench +} + +criterion_main!(benches); diff --git a/lykiadb-server/benches/scripts/fib.ly b/lykiadb-server/benches/scripts/fib.ly index fbf732f..ec2a144 100644 --- a/lykiadb-server/benches/scripts/fib.ly +++ b/lykiadb-server/benches/scripts/fib.ly @@ -1,7 +1,7 @@ // Define recursive Fibonacci function -function fib($n) { +function $fib($n) { if ($n < 2) return $n; - return fib($n - 2) + fib($n - 1); + return $fib($n - 2) + $fib($n - 1); }; -fib(35); \ No newline at end of file +$fib(35); \ No newline at end of file diff --git a/lykiadb-shell/examples/compute.lyql b/lykiadb-shell/examples/compute.lyql index cffd97a..5af2eeb 100644 --- a/lykiadb-shell/examples/compute.lyql +++ b/lykiadb-shell/examples/compute.lyql @@ -1,3 +1,3 @@ var $a = {}; $a.test = 100; -print($a); \ No newline at end of file +io::print($a); \ No newline at end of file diff --git a/lykiadb-shell/examples/fib.lyql b/lykiadb-shell/examples/fib.lyql index 272fd99..6445617 100644 --- a/lykiadb-shell/examples/fib.lyql +++ b/lykiadb-shell/examples/fib.lyql @@ -1,13 +1,13 @@ // Define recursive Fibonacci function -function fib($n) { +function $fib($n) { if ($n < 2) return $n; - return fib($n - 2) + fib($n - 1); + return $fib($n - 2) + $fib($n - 1); }; var $start_ly = time::clock(); -print(fib(35) == 9227465); -print("elapsed (user defined):", time::clock() - $start_ly); +io::print($fib(35) == 9227465); +io::print("elapsed (user defined):", time::clock() - $start_ly); var $start_rs = time::clock(); -print(Benchmark.fib(35) == 9227465); -print("elapsed (native):", time::clock() - $start_rs); \ No newline at end of file +io::print(Benchmark::fib(35) == 9227465); +io::print("elapsed (native):", time::clock() - $start_rs); \ No newline at end of file diff --git a/lykiadb-shell/examples/fn.lyql b/lykiadb-shell/examples/fn.lyql index 90f2177..49f4488 100644 --- a/lykiadb-shell/examples/fn.lyql +++ b/lykiadb-shell/examples/fn.lyql @@ -1,20 +1,20 @@ -function helloWorld ($message) { - print("Hello world!", $message); +function $hello_world($message) { + io::print("Hello world!", $message); { { return "and returning from here."; { - print("inner"); - print("inner"); - print("inner"); + io::print("inner"); + io::print("inner"); + io::print("inner"); } - print("outer"); - print("outer"); - print("outer"); + io::print("outer"); + io::print("outer"); + io::print("outer"); } } }; for (var $i = 0; $i < 10; $i = $i + 1) { - print(helloWorld("My name is Lykia.")); + io::print($hello_world("My name is Lykia.")); } diff --git a/lykiadb-shell/examples/hof.lyql b/lykiadb-shell/examples/hof.lyql index 85be41e..7f859b1 100644 --- a/lykiadb-shell/examples/hof.lyql +++ b/lykiadb-shell/examples/hof.lyql @@ -1,12 +1,12 @@ -function makeCounter() { +function $make_counter() { var $i = 0; - function count() { + function $count() { $i = $i + 1; - print($i); + io::print($i); }; - return count; + return $count; }; -var $count = makeCounter(); +var $count = $make_counter(); $count(); $count(); \ No newline at end of file diff --git a/lykiadb-shell/examples/objects.lyql b/lykiadb-shell/examples/objects.lyql index b5f5725..08f993e 100644 --- a/lykiadb-shell/examples/objects.lyql +++ b/lykiadb-shell/examples/objects.lyql @@ -12,8 +12,8 @@ var $calc = { return $a / $b; }, }; -print($calc.add(4, 5)); -print($calc.sub(4, 5)); -print($calc.mul(4, 5)); -print($calc.div(4, 5)); +io::print($calc.add(4, 5)); +io::print($calc.sub(4, 5)); +io::print($calc.mul(4, 5)); +io::print($calc.div(4, 5)); diff --git a/lykiadb-shell/examples/scan_err.lyql b/lykiadb-shell/examples/scan_err.lyql index abfe9c8..c617f84 100644 --- a/lykiadb-shell/examples/scan_err.lyql +++ b/lykiadb-shell/examples/scan_err.lyql @@ -1,7 +1,7 @@ // Define recursive Fibonacci function -function fib($n) { +function $fib($n) { if ($n < 2) return $n; - return fib($n - 2) + fib($n - 1); + return $fib($n - 2) + $fib($n - 1); }; 117E \ No newline at end of file diff --git a/lykiadb-shell/examples/sql.lyql b/lykiadb-shell/examples/sql.lyql index 199f6a4..f67c265 100644 --- a/lykiadb-shell/examples/sql.lyql +++ b/lykiadb-shell/examples/sql.lyql @@ -8,6 +8,6 @@ var $q = SELECT * FROM users where id != 5; var $r = SELECT "darkness" as my_old_friend; -print($p); -print($q); -print($r); \ No newline at end of file +io::print($p); +io::print($q); +io::print($r); \ No newline at end of file