From 5849285ef77fde604a5f274b55940bcef8ef39ea Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Tue, 7 May 2024 23:15:06 -0400 Subject: [PATCH 1/8] started time.py. right now it is a copied over test.py with modified arguments. --- caiman-test/time.py | 311 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 caiman-test/time.py diff --git a/caiman-test/time.py b/caiman-test/time.py new file mode 100644 index 00000000..183385e7 --- /dev/null +++ b/caiman-test/time.py @@ -0,0 +1,311 @@ +#!/usr/bin/env python3 +import argparse +import subprocess +from pathlib import Path +from itertools import chain +from sys import stderr +from dataclasses import dataclass +from shutil import rmtree +import os + + +# stupid hack to build a test file for each pair of results +def rust_diff(file1: Path, file2: Path): + return f""" +#[test] +fn compare() -> Result<(), String> {{ + use file_diff::{{diff_files}}; + use std::fs::{{File}}; + + let mut file1 = match File::open(r##"{file1}"##) {{ + Ok(f) => f, + Err(e) => panic!("{{}}", e), + }}; + let mut file2 = match File::open(r##"{file2}"##) {{ + Ok(f) => f, + Err(e) => panic!("{{}}", e), + }}; + + crate::expect_returned!(true, Some(diff_files(&mut file1, &mut file2))); +}} + """ + +def help(): + print("Help function has not been implemented yet.") + +def eprint(*args, **kwargs): + print(*args, file=stderr, **kwargs) + + +class Colorizer: + def cyan(s: str) -> str: + return f"\033[36m{s}\033[39m" + + def yellow(s: str) -> str: + return f"\033[93m{s}\033[39m" + + def red(s: str) -> str: + return f"\033[31m{s}\033[39m" + + def grey(s: str) -> str: + return f"\033[90m{s}\033[39m" + + +COLOR_INFO = Colorizer.cyan("[info]") +COLOR_WARN = Colorizer.yellow("[warn]") +COLOR_FAIL = Colorizer.red("[fail]") + +"""Pads the start of each line in `s` with `pad`.""" + + +def pad_lines(s: str, pad: str) -> str: + return pad + s.replace("\n", f"\n{pad}") + + +class Compiler: + def __init__(self, test_dir): + manifest_path = test_dir / ".." / "Cargo.toml" + args = [ + "cargo", + "build", + "--manifest-path", + manifest_path, + "--features", + "build-binary", + ] + rv = subprocess.run(args) + if rv.returncode != 0: + eprint(f"{COLOR_WARN} using previous caimanc") + self.test_dir = test_dir + + def _compiler_path(self) -> Path: + return self.test_dir / ".." / "target" / "debug" / "caimanc" + + def compile( + self, input: Path, output: Path, explicate_only: bool = False + ) -> subprocess.CompletedProcess: + args = [self._compiler_path(), "--input", input, "--output", output] + [ + "--explicate_only" + ] * explicate_only + return subprocess.run( + args, capture_output=True, encoding="utf8", cwd=input.parent + ) + + +class HighLevelCaiman: + def __init__(self, test_dir): + args = ["cargo", "build", "--all"] + rv = subprocess.run(args) + if rv.returncode != 0: + eprint(f"{COLOR_WARN} using previous high-level-caiman") + self.test_dir = test_dir + + def _compiler_path(self) -> Path: + return self.test_dir / ".." / "target" / "debug" / "hlc" + + def compile(self, input: Path, output: Path) -> subprocess.CompletedProcess: + args = [self._compiler_path(), "-o", output, input] + return subprocess.run( + args, capture_output=True, encoding="utf8", cwd=input.parent + ) + + +@dataclass +class ProcessStatistics: + """Successfully compiled inputs which are associated with a test Rust file.""" + + linked: int + """Successfully compiled inputs.""" + compiled: int + """Inputs which caimanc failed to compile.""" + failures: int + + """Total number of files processed.""" + + def total(self) -> int: + return self.compiled + self.failures + + +def compiler_error( + rv: subprocess.CompletedProcess, + relativized: Path, + quiet: bool, + ps: ProcessStatistics, +) -> bool: + if rv.returncode != 0: + eprint(f" {Colorizer.red('fail:')} {relativized}") + if not quiet: + msg = pad_lines(rv.stderr, f" {Colorizer.red('|')} ") + print(msg, file=stderr) + ps.failures += 1 + return True # return if there was an error + return False + + +# returns num failed, num succeeded +def process_inputs( + compiler: Compiler, + hlc: HighLevelCaiman, + test_dir: Path, + inputs, + quiet: bool, +) -> ProcessStatistics: + lf = (test_dir / "src" / "lib.rs").open(mode="w") + lf.write("pub mod util;\n") + ps = ProcessStatistics(0, 0, 0) + if not inputs: + inputs = chain( + test_dir.rglob("*test.cair"), + test_dir.rglob("*test.ron"), + test_dir.rglob("*test.caiman"), + test_dir.rglob("*test.cm"), + ) + for input in inputs: + if f"{input}".find("turnt") != -1: + continue + relativized = input.absolute().relative_to(test_dir) + output = test_dir / "src" / (input.stem + ".rs") + + input_str = str(input) # cause I wanna do direct string manipulations + if input_str.endswith("test.cair"): + baseline_name = ( + input.name[: input.name.find("_")] + "_baseline.cair" + ) + baseline = input.parent / baseline_name + if baseline.is_file(): + # we compile here for explication only + test_out = output.with_suffix(".txt") + rv = compiler.compile(input.absolute(), test_out, True) + if compiler_error(rv, relativized, quiet, ps): + continue + + baseline_out = Path( + str(output).replace("test.rs", "baseline.txt") + ) # to explication thing + rv = compiler.compile(baseline.absolute(), baseline_out, True) + if compiler_error(rv, relativized, quiet, ps): + continue + + eprint(Colorizer.grey(f" pass: {relativized}")) + lf.write(f"mod {input.stem};\n") + ps.compiled += 1 + + of = output.open(mode="w", encoding="utf8") + of.write(rust_diff(test_out, baseline_out)) + of.close() + + ps.linked += 1 + + continue + + input_compiler = hlc if input_str.endswith(".cm") else compiler + rv = input_compiler.compile(input.absolute(), output) + + if rv.returncode == 0: + eprint(Colorizer.grey(f" pass: {relativized}")) + if not quiet and rv.stderr: + msg = pad_lines(rv.stderr, f" {Colorizer.grey('|')} ") + print(msg, file=stderr) + else: + eprint(f" {Colorizer.red('fail:')} {relativized}") + if not quiet: + msg = pad_lines(rv.stderr, f" {Colorizer.red('|')} ") + print(msg, file=stderr) + ps.failures += 1 + continue + + lf.write(f"mod {input.stem};\n") + ps.compiled += 1 + + test_file = input.with_suffix(".rs") + if not test_file.exists(): + if not quiet: + eprint(Colorizer.grey(" | no Rust test file provided")) + else: + input_rs = Path("..") / relativized.with_suffix(".rs") + of = output.open(mode="a", encoding="utf8") + of.write(f'\ninclude!(r##"{input_rs}"##);\n') + of.close() + ps.linked += 1 + + lf.close() + return ps + + +def build(test_dir: Path, inputs, quiet: bool): + eprint(f"{COLOR_INFO} building caimanc") + c = Compiler(test_dir) + + eprint(f"{COLOR_INFO} building high-level-caiman") + hlc = HighLevelCaiman(test_dir) + + eprint(f"{COLOR_INFO} compiling Caiman source files") + ps = process_inputs(c, hlc, test_dir, inputs, quiet) + if ps.failures > 0: + eprint( + f"{COLOR_WARN} {ps.failures}/{ps.total()} files failed to compile" + ) + return ps.failures + + +def run(test_dir: Path, inputs): + eprint(f"{COLOR_INFO} running Cargo tests") + manifest_path = test_dir / "Cargo.toml" + args = ["cargo", "test", "--manifest-path", manifest_path, "--"] + args += [Path(input).stem for input in inputs] + return subprocess.run(args).returncode + + +def clean(test_dir: Path): + for log in test_dir.rglob("*.log"): + log.unlink() + for dbg in test_dir.rglob("*.debug"): + dbg.unlink() + gen_dir = test_dir / "src" + for f in gen_dir.iterdir(): + if f.is_dir(): + rmtree(f, ignore_errors=True) + elif f.name != "util.rs": + f.unlink() + lf = (gen_dir / "lib.rs").open(mode="w") + lf.write("pub mod util;\n") + + +def main(): + test_dir = Path(__file__).resolve().parent + parser = argparse.ArgumentParser( + description="Caiman Test Suite", fromfile_prefix_chars="@" + ) + parser.add_argument("command", choices=["compile", "run"]) + parser.add_argument( + "-q", "--quiet", action="store_true", help="Suppress extra info." + ) + parser.add_argument( + "files", nargs="*", help="If specified, only build/test these file(s)." + ) + parser.add_argument("NUM_ITERS", choices=["1", "2"]) + args = parser.parse_args() + inputs = [] + for file in args.files: + if os.path.isdir(file): + for path, _, filenames in os.walk(file): + for filename in filenames: + fpath = Path(filename) + if fpath.suffix != ".rs" and fpath.stem.endswith("_test"): + inputs.append(Path(path) / fpath) + else: + inputs.append(Path(file)) + if args.command == "run": + ret = build(test_dir, inputs, args.quiet) + ret |= run(test_dir, inputs) + exit(ret) + elif args.command == "build": + build(test_dir, inputs, args.quiet) + elif args.command == "clean": + clean(test_dir) + else: + eprint("Unknown subcommand. Accepted: run, build, clean") + + +if __name__ == "__main__": + main() From b2cdac4af6ea3a57dcc7e2e82b77f129805a0082 Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 00:57:47 -0400 Subject: [PATCH 2/8] running and compilation both work now and you can use the script to compile and run specified filenames that live anywhere in the directory. num_iters and printing to csv yet to be implemented. --- caiman-test/time.py | 160 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 142 insertions(+), 18 deletions(-) diff --git a/caiman-test/time.py b/caiman-test/time.py index 183385e7..78d3634e 100644 --- a/caiman-test/time.py +++ b/caiman-test/time.py @@ -7,6 +7,8 @@ from dataclasses import dataclass from shutil import rmtree import os +import time +import csv # stupid hack to build a test file for each pair of results @@ -232,9 +234,11 @@ def process_inputs( return ps +#function for building caiman and compiling it def build(test_dir: Path, inputs, quiet: bool): eprint(f"{COLOR_INFO} building caimanc") c = Compiler(test_dir) + print("ALKSDJFADKS") eprint(f"{COLOR_INFO} building high-level-caiman") hlc = HighLevelCaiman(test_dir) @@ -248,12 +252,70 @@ def build(test_dir: Path, inputs, quiet: bool): return ps.failures +#function that compiles a .cair source file +def compile(test_dir: Path, file): + #prints colored messages, fancy + eprint(f"{COLOR_INFO} compiling Caiman file with Cargo run") + + #change directory to /caiman main directory. + os.chdir("../") + + #change the filename to have "./caiman-test/ in front of it" + f = "./caiman-test/basics/" + file + + #arguments for running + args = ["cargo", "run", "--", "--input"] + args += [f] + + #time this part. start time: + start = time.time() + + #get the output + r2 = subprocess.Popen(args, stdout=subprocess.PIPE) + #end time + end = time.time() + + #read output into a variable + output = r2.stdout.read() + + #runtime to return + t = end - start + + #if running was not a success, raise an exception + if r2.returncode is not None: + raise Exception(f"compiling caiman file {file} failed! failed with error code{r2.returncode}") + #else, return the runtime and output. + return [t, output] + + +#function that runs a file def run(test_dir: Path, inputs): - eprint(f"{COLOR_INFO} running Cargo tests") + #prints colored messages, fancy + eprint(f"{COLOR_INFO} running Caiman file with cargo test") + + #gets the cargo.toml manifest_path = test_dir / "Cargo.toml" + + #arguments for running args = ["cargo", "test", "--manifest-path", manifest_path, "--"] args += [Path(input).stem for input in inputs] - return subprocess.run(args).returncode + + #time this part. start time: + start = time.time() + + #process to generate stuff with cargo test: + r = subprocess.run(args).returncode + + #end time + end = time.time() + #runtime to return + t = end - start + + #if running was not a success, raise an exception + if r != 0: + raise Exception("running caiman file {filename} failed!".format(filename = inputs[i])) + #else, return the runtime + return t def clean(test_dir: Path): @@ -270,23 +332,64 @@ def clean(test_dir: Path): lf = (gen_dir / "lib.rs").open(mode="w") lf.write("pub mod util;\n") +def write_to_csv(filename, num_iters, command){ + #generate the csv name you will be writing to + csv_name = filename.split(.)[0] + command + ".csv" + + #find the file in the dummy directory. if file does not exist, create the file. + +} def main(): + #makes the test directory, in order to get access to the cargo.toml test_dir = Path(__file__).resolve().parent + + #some options that apply to the entire parser parser = argparse.ArgumentParser( - description="Caiman Test Suite", fromfile_prefix_chars="@" + prog="Timing script", + description="Times a single file's compilation and runtime", + fromfile_prefix_chars="@" ) - parser.add_argument("command", choices=["compile", "run"]) + + #positional argument 1: command type + parser.add_argument( + "command", + choices=["compile", "run"], + help="Choose your command: compile or run" + ) + + #might not include this one? parser.add_argument( - "-q", "--quiet", action="store_true", help="Suppress extra info." + "-q", "--quiet", + action="store_true", + help="Suppress extra info." ) + + #positional argument 2: filename parser.add_argument( - "files", nargs="*", help="If specified, only build/test these file(s)." + "file", + help="The file to compile or run. Directories are not accepted and will cause the script to break." ) - parser.add_argument("NUM_ITERS", choices=["1", "2"]) + + ''' + #positional argument 3: number of iterations + parser.add_argument( + "NUM_ITERS", + type=int, + help="number of iterations" + ) + ''' + + #parse arguments args = parser.parse_args() + + #print("LAKSDJFLASDKFJASDLKFJKLA") + + #if the file specified is a directory then we walk through it + #if it's just a file we add it to inputs inputs = [] - for file in args.files: + ''' + for file in args.file: if os.path.isdir(file): for path, _, filenames in os.walk(file): for filename in filenames: @@ -294,17 +397,38 @@ def main(): if fpath.suffix != ".rs" and fpath.stem.endswith("_test"): inputs.append(Path(path) / fpath) else: - inputs.append(Path(file)) - if args.command == "run": - ret = build(test_dir, inputs, args.quiet) - ret |= run(test_dir, inputs) - exit(ret) - elif args.command == "build": - build(test_dir, inputs, args.quiet) - elif args.command == "clean": - clean(test_dir) + ''' + #gets the filename from args + filename = args.file.split("/")[-1] + #print(filename) + inputs.append(filename) + + #control flow for running and compiling + + #compile command. + if args.command == "compile": + #store the compile time in a compile-time variable. + compile_info = compile(test_dir, filename) + + #if success, print to console and store compile time. + print("Successfully compiled file {f}".format(f=filename)) + compile_time = compile_info[0] + print("Compile time was {c}".format(c=compile_time)) + + #run + elif args.command == "run": + #assume Caiman is already built. First, compile the file. + compile(test_dir, filename) + + #next, run it. Runtime is stored in this variable. + runtime = run(test_dir, filename) + + print("Successfully ran file {f}".format(f=filename)) + print("Runtime was {r}".format(r=runtime)) + + #unknown else: - eprint("Unknown subcommand. Accepted: run, build, clean") + eprint("Unknown subcommand. Accepted: run, compile") if __name__ == "__main__": From 144a3b6ead9a4d53c2c2fbf18412b49dc9f8b843 Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 02:10:59 -0400 Subject: [PATCH 3/8] num_iters works and it prints data to a csv. --- .../test_data/external_test_compile.csv | 3 + caiman-test/test_data/external_test_run.csv | 3 + caiman-test/test_data/random.csv | 0 caiman-test/time.py | 58 +++++++++++++++---- 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 caiman-test/test_data/external_test_compile.csv create mode 100644 caiman-test/test_data/external_test_run.csv create mode 100644 caiman-test/test_data/random.csv diff --git a/caiman-test/test_data/external_test_compile.csv b/caiman-test/test_data/external_test_compile.csv new file mode 100644 index 00000000..89932fc9 --- /dev/null +++ b/caiman-test/test_data/external_test_compile.csv @@ -0,0 +1,3 @@ +0 0.002360105514526367 +1 0.0028128623962402344 +2 0.0031938552856445312 diff --git a/caiman-test/test_data/external_test_run.csv b/caiman-test/test_data/external_test_run.csv new file mode 100644 index 00000000..302e2184 --- /dev/null +++ b/caiman-test/test_data/external_test_run.csv @@ -0,0 +1,3 @@ +0 0.10759186744689941 +1 0.12284088134765625 +2 0.11043906211853027 diff --git a/caiman-test/test_data/random.csv b/caiman-test/test_data/random.csv new file mode 100644 index 00000000..e69de29b diff --git a/caiman-test/time.py b/caiman-test/time.py index 78d3634e..9a1c9918 100644 --- a/caiman-test/time.py +++ b/caiman-test/time.py @@ -252,14 +252,11 @@ def build(test_dir: Path, inputs, quiet: bool): return ps.failures -#function that compiles a .cair source file +#function that compiles a .cair source file. Assumes you are in ./caiman already, so please change working directory manually. def compile(test_dir: Path, file): #prints colored messages, fancy eprint(f"{COLOR_INFO} compiling Caiman file with Cargo run") - #change directory to /caiman main directory. - os.chdir("../") - #change the filename to have "./caiman-test/ in front of it" f = "./caiman-test/basics/" + file @@ -332,13 +329,44 @@ def clean(test_dir: Path): lf = (gen_dir / "lib.rs").open(mode="w") lf.write("pub mod util;\n") -def write_to_csv(filename, num_iters, command){ +''' +Writes data to a csv. This function is called from the assumed directory caiman-test. Please first switch to caiman-test +in caller function before calling write_to_csv. +''' +def write_to_csv(test_dir, filename, num_iters, command): + #generate the csv name you will be writing to - csv_name = filename.split(.)[0] + command + ".csv" + csv_name = filename.split(".")[0] + "_" + command + ".csv" #find the file in the dummy directory. if file does not exist, create the file. + f = open("./test_data/"+csv_name, "w") + + #create a writer for the csv + caimanwriter = csv.writer(f, delimiter=' ', quotechar= '|', quoting=csv.QUOTE_MINIMAL) + + + #for the number of iterations, run the specified command and write the resulting runtime to the csv + t = 0 + + #change working directory based on if it's run or compile: compile needs to be in /caiman. compile flag will be 0 + c = 0 + if command == "compile": + os.chdir("../") + c = 1 + + for i in range(num_iters): + #actually do the command + if command == "compile": + t = compile(test_dir, filename)[0] + if command == "run": + t = run(test_dir, filename) + #write the time to f (the csv) + caimanwriter.writerow([i, t]) + + #change working directory back to caiman-test if it got changed + if c == 1: + os.chdir("./caiman-test") -} def main(): #makes the test directory, in order to get access to the cargo.toml @@ -371,14 +399,14 @@ def main(): help="The file to compile or run. Directories are not accepted and will cause the script to break." ) - ''' + #positional argument 3: number of iterations parser.add_argument( "NUM_ITERS", type=int, help="number of iterations" ) - ''' + #parse arguments args = parser.parse_args() @@ -407,24 +435,34 @@ def main(): #compile command. if args.command == "compile": + #change working directory to caiman + os.chdir("../") #store the compile time in a compile-time variable. compile_info = compile(test_dir, filename) + #change working directory back to caiman-test + os.chdir("./caiman-test") #if success, print to console and store compile time. print("Successfully compiled file {f}".format(f=filename)) compile_time = compile_info[0] print("Compile time was {c}".format(c=compile_time)) + write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) + #run elif args.command == "run": - #assume Caiman is already built. First, compile the file. + #assume Caiman is already built. First, compile the file. You must be in /caiman. + os.chdir("../") compile(test_dir, filename) + os.chdir("./caiman-test") #next, run it. Runtime is stored in this variable. runtime = run(test_dir, filename) print("Successfully ran file {f}".format(f=filename)) print("Runtime was {r}".format(r=runtime)) + + write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) #unknown else: From d71c71a401f32bfbd0d0fe7d7c322ea3ade050bc Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 03:08:51 -0400 Subject: [PATCH 4/8] added some more comments and removed unused comments --- caiman-test/time.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/caiman-test/time.py b/caiman-test/time.py index 9a1c9918..70891afa 100644 --- a/caiman-test/time.py +++ b/caiman-test/time.py @@ -251,8 +251,11 @@ def build(test_dir: Path, inputs, quiet: bool): ) return ps.failures - -#function that compiles a .cair source file. Assumes you are in ./caiman already, so please change working directory manually. +''' +function that compiles a .cair source file. +Assumes you are in ./caiman already, so please change working directory manually +before and after you call this function. +''' def compile(test_dir: Path, file): #prints colored messages, fancy eprint(f"{COLOR_INFO} compiling Caiman file with Cargo run") @@ -284,8 +287,11 @@ def compile(test_dir: Path, file): #else, return the runtime and output. return [t, output] - -#function that runs a file +''' +function that runs a file +Assumes you are running from /caiman-test, so usually changing the directory with +os.chdir(d) is not needed. +''' def run(test_dir: Path, inputs): #prints colored messages, fancy eprint(f"{COLOR_INFO} running Caiman file with cargo test") @@ -385,21 +391,17 @@ def main(): choices=["compile", "run"], help="Choose your command: compile or run" ) - #might not include this one? parser.add_argument( "-q", "--quiet", action="store_true", help="Suppress extra info." ) - #positional argument 2: filename parser.add_argument( "file", help="The file to compile or run. Directories are not accepted and will cause the script to break." - ) - - + ) #positional argument 3: number of iterations parser.add_argument( "NUM_ITERS", @@ -407,13 +409,9 @@ def main(): help="number of iterations" ) - #parse arguments args = parser.parse_args() - #print("LAKSDJFLASDKFJASDLKFJKLA") - - #if the file specified is a directory then we walk through it #if it's just a file we add it to inputs inputs = [] ''' @@ -447,6 +445,7 @@ def main(): compile_time = compile_info[0] print("Compile time was {c}".format(c=compile_time)) + #compilation didn't throw an error, so we write data based on number of iterations to a CSV file. write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) #run @@ -462,6 +461,7 @@ def main(): print("Successfully ran file {f}".format(f=filename)) print("Runtime was {r}".format(r=runtime)) + #compiling/running didn't throw an error, so we write data based on number of iterations to a CSV file. write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) #unknown From 2159a3c8594108792a65340700ab92fa8b41af54 Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 16:33:10 -0400 Subject: [PATCH 5/8] debugged a bit --- caiman-test/time.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/caiman-test/time.py b/caiman-test/time.py index 70891afa..05297a94 100644 --- a/caiman-test/time.py +++ b/caiman-test/time.py @@ -32,9 +32,6 @@ def rust_diff(file1: Path, file2: Path): }} """ -def help(): - print("Help function has not been implemented yet.") - def eprint(*args, **kwargs): print(*args, file=stderr, **kwargs) @@ -238,7 +235,6 @@ def process_inputs( def build(test_dir: Path, inputs, quiet: bool): eprint(f"{COLOR_INFO} building caimanc") c = Compiler(test_dir) - print("ALKSDJFADKS") eprint(f"{COLOR_INFO} building high-level-caiman") hlc = HighLevelCaiman(test_dir) @@ -378,6 +374,9 @@ def main(): #makes the test directory, in order to get access to the cargo.toml test_dir = Path(__file__).resolve().parent + #change working directory to caiman-test + os.chdir(test_dir) + #some options that apply to the entire parser parser = argparse.ArgumentParser( prog="Timing script", @@ -435,9 +434,11 @@ def main(): if args.command == "compile": #change working directory to caiman os.chdir("../") + #store the compile time in a compile-time variable. compile_info = compile(test_dir, filename) #change working directory back to caiman-test + os.chdir("./caiman-test") #if success, print to console and store compile time. From 52c2d7e4b8999f2906953857ccfb48ca427008ab Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 16:36:19 -0400 Subject: [PATCH 6/8] added csv to gitignore --- .gitignore | 1 + caiman-test/test_data/external_test_compile.csv | 6 +++--- caiman-test/test_data/external_test_run.csv | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index e29995c5..e56845d8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ caiman-test/src/bin/* caiman-test/src/* !caiman-test/src/util.rs **/parse/parser.rs +*.csv # IDE *.sublime-project diff --git a/caiman-test/test_data/external_test_compile.csv b/caiman-test/test_data/external_test_compile.csv index 89932fc9..e13d3a2c 100644 --- a/caiman-test/test_data/external_test_compile.csv +++ b/caiman-test/test_data/external_test_compile.csv @@ -1,3 +1,3 @@ -0 0.002360105514526367 -1 0.0028128623962402344 -2 0.0031938552856445312 +0 0.0016350746154785156 +1 0.002177000045776367 +2 0.0026769638061523438 diff --git a/caiman-test/test_data/external_test_run.csv b/caiman-test/test_data/external_test_run.csv index 302e2184..8df44301 100644 --- a/caiman-test/test_data/external_test_run.csv +++ b/caiman-test/test_data/external_test_run.csv @@ -1,3 +1,3 @@ -0 0.10759186744689941 -1 0.12284088134765625 -2 0.11043906211853027 +0 0.1091148853302002 +1 0.10519909858703613 +2 0.10447001457214355 From 98119344db43542daddc284491586455899ca071 Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Fri, 17 May 2024 20:35:51 -0400 Subject: [PATCH 7/8] fixed last bug, now time.py can be used from anywhere in the caiman directory. --- caiman-test/time.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/caiman-test/time.py b/caiman-test/time.py index 05297a94..b6401dbf 100644 --- a/caiman-test/time.py +++ b/caiman-test/time.py @@ -257,7 +257,8 @@ def compile(test_dir: Path, file): eprint(f"{COLOR_INFO} compiling Caiman file with Cargo run") #change the filename to have "./caiman-test/ in front of it" - f = "./caiman-test/basics/" + file + #f = "./caiman-test/basics/" + file + f = file #arguments for running args = ["cargo", "run", "--", "--input"] @@ -335,9 +336,11 @@ def clean(test_dir: Path): Writes data to a csv. This function is called from the assumed directory caiman-test. Please first switch to caiman-test in caller function before calling write_to_csv. ''' -def write_to_csv(test_dir, filename, num_iters, command): +def write_to_csv(test_dir, path_to_file, num_iters, command): #generate the csv name you will be writing to + #first cut the file path into the file name + filename = path_to_file.split('/')[-1] csv_name = filename.split(".")[0] + "_" + command + ".csv" #find the file in the dummy directory. if file does not exist, create the file. @@ -359,9 +362,9 @@ def write_to_csv(test_dir, filename, num_iters, command): for i in range(num_iters): #actually do the command if command == "compile": - t = compile(test_dir, filename)[0] + t = compile(test_dir, path_to_file) if command == "run": - t = run(test_dir, filename) + t = run(test_dir, path_to_file) #write the time to f (the csv) caimanwriter.writerow([i, t]) @@ -371,12 +374,9 @@ def write_to_csv(test_dir, filename, num_iters, command): def main(): - #makes the test directory, in order to get access to the cargo.toml + #this directory is caiman-test since this line gets the directory time.py lives in test_dir = Path(__file__).resolve().parent - #change working directory to caiman-test - os.chdir(test_dir) - #some options that apply to the entire parser parser = argparse.ArgumentParser( prog="Timing script", @@ -422,48 +422,54 @@ def main(): if fpath.suffix != ".rs" and fpath.stem.endswith("_test"): inputs.append(Path(path) / fpath) else: - ''' #gets the filename from args filename = args.file.split("/")[-1] #print(filename) inputs.append(filename) + ''' + #generates the file path by adding it to the working directory + wd = os.getcwd() + file_path = wd + "/" + args.file + + #add the file path to inputs + inputs.append(file_path) #control flow for running and compiling #compile command. if args.command == "compile": #change working directory to caiman - os.chdir("../") + os.chdir(test_dir.parent) #store the compile time in a compile-time variable. - compile_info = compile(test_dir, filename) + compile_info = compile(test_dir, file_path) #change working directory back to caiman-test os.chdir("./caiman-test") #if success, print to console and store compile time. - print("Successfully compiled file {f}".format(f=filename)) + print("Successfully compiled file {f}".format(f=file_path)) compile_time = compile_info[0] print("Compile time was {c}".format(c=compile_time)) #compilation didn't throw an error, so we write data based on number of iterations to a CSV file. - write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) + write_to_csv(test_dir, file_path, args.NUM_ITERS, args.command) #run elif args.command == "run": #assume Caiman is already built. First, compile the file. You must be in /caiman. - os.chdir("../") - compile(test_dir, filename) + os.chdir(test_dir.parent) + compile(test_dir, file_path) os.chdir("./caiman-test") #next, run it. Runtime is stored in this variable. - runtime = run(test_dir, filename) + runtime = run(test_dir, file_path) - print("Successfully ran file {f}".format(f=filename)) + print("Successfully ran file {f}".format(f=file_path)) print("Runtime was {r}".format(r=runtime)) #compiling/running didn't throw an error, so we write data based on number of iterations to a CSV file. - write_to_csv(test_dir, filename, args.NUM_ITERS, args.command) + write_to_csv(test_dir, file_path, args.NUM_ITERS, args.command) #unknown else: From 85f4d9c8abe55ecde2202b1ff989868eac70c2aa Mon Sep 17 00:00:00 2001 From: Meredith Meiyin Hu Date: Mon, 16 Sep 2024 02:12:59 -0400 Subject: [PATCH 8/8] removed csv files from commit --- caiman-test/test_data/external_test_compile.csv | 3 --- caiman-test/test_data/external_test_run.csv | 3 --- caiman-test/test_data/random.csv | 0 3 files changed, 6 deletions(-) delete mode 100644 caiman-test/test_data/external_test_compile.csv delete mode 100644 caiman-test/test_data/external_test_run.csv delete mode 100644 caiman-test/test_data/random.csv diff --git a/caiman-test/test_data/external_test_compile.csv b/caiman-test/test_data/external_test_compile.csv deleted file mode 100644 index e13d3a2c..00000000 --- a/caiman-test/test_data/external_test_compile.csv +++ /dev/null @@ -1,3 +0,0 @@ -0 0.0016350746154785156 -1 0.002177000045776367 -2 0.0026769638061523438 diff --git a/caiman-test/test_data/external_test_run.csv b/caiman-test/test_data/external_test_run.csv deleted file mode 100644 index 8df44301..00000000 --- a/caiman-test/test_data/external_test_run.csv +++ /dev/null @@ -1,3 +0,0 @@ -0 0.1091148853302002 -1 0.10519909858703613 -2 0.10447001457214355 diff --git a/caiman-test/test_data/random.csv b/caiman-test/test_data/random.csv deleted file mode 100644 index e69de29b..00000000