Skip to content

Commit

Permalink
Merge pull request #46 from lykia-rs/feature/coverage
Browse files Browse the repository at this point in the history
fix: Script updates and benchmarks
  • Loading branch information
can-keklik authored Dec 8, 2024
2 parents fd5d299 + 4579582 commit 39d682a
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 33 deletions.
7 changes: 7 additions & 0 deletions lykiadb-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
71 changes: 71 additions & 0 deletions lykiadb-lang/benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 3 additions & 3 deletions lykiadb-server/benches/scripts/fib.ly
Original file line number Diff line number Diff line change
@@ -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);
$fib(35);
2 changes: 1 addition & 1 deletion lykiadb-shell/examples/compute.lyql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var $a = {};
$a.test = 100;
print($a);
io::print($a);
12 changes: 6 additions & 6 deletions lykiadb-shell/examples/fib.lyql
Original file line number Diff line number Diff line change
@@ -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);
io::print(Benchmark::fib(35) == 9227465);
io::print("elapsed (native):", time::clock() - $start_rs);
18 changes: 9 additions & 9 deletions lykiadb-shell/examples/fn.lyql
Original file line number Diff line number Diff line change
@@ -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."));
}
10 changes: 5 additions & 5 deletions lykiadb-shell/examples/hof.lyql
Original file line number Diff line number Diff line change
@@ -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();
8 changes: 4 additions & 4 deletions lykiadb-shell/examples/objects.lyql
Original file line number Diff line number Diff line change
Expand Up @@ -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));

4 changes: 2 additions & 2 deletions lykiadb-shell/examples/scan_err.lyql
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lykiadb-shell/examples/sql.lyql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
io::print($p);
io::print($q);
io::print($r);

0 comments on commit 39d682a

Please sign in to comment.