Skip to content

Commit a13346f

Browse files
benchmark timer on custom files
1 parent b641f2c commit a13346f

File tree

7 files changed

+101
-44
lines changed

7 files changed

+101
-44
lines changed

MyCode/benchmark/main.go

+51-26
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
67
"time"
78

9+
"github.com/Aditya-Chowdhary/Monkey-Interpreter/ast"
810
"github.com/Aditya-Chowdhary/Monkey-Interpreter/compiler"
911
"github.com/Aditya-Chowdhary/Monkey-Interpreter/evaluator"
12+
"github.com/Aditya-Chowdhary/Monkey-Interpreter/file"
1013
"github.com/Aditya-Chowdhary/Monkey-Interpreter/lexer"
1114
"github.com/Aditya-Chowdhary/Monkey-Interpreter/object"
1215
"github.com/Aditya-Chowdhary/Monkey-Interpreter/parser"
1316
"github.com/Aditya-Chowdhary/Monkey-Interpreter/vm"
1417
)
1518

16-
var engine = flag.String("engine", "vm", "use `vm` or `eval`")
19+
var engine = flag.String("engine", "", "use `vm` or `eval`. Leave blank to return comparison")
1720

1821
var input = `
1922
let fibonacci = fn(x) {
@@ -33,43 +36,65 @@ var input = `
3336
func main() {
3437
flag.Parse()
3538

36-
var duration time.Duration
37-
var result object.Object
39+
if flag.Arg(0) != "" {
40+
code, err := file.ReadCode(flag.Arg(0))
41+
if err != nil {
42+
fmt.Println(err)
43+
os.Exit(1)
44+
}
45+
input = code
46+
}
3847

3948
l := lexer.New(input)
4049
p := parser.New(l)
4150
program := p.ParseProgram()
4251

4352
if *engine == "vm" {
44-
comp := compiler.New()
45-
err := comp.Compile(program)
46-
if err != nil {
47-
fmt.Printf("compiler error: %s", err)
48-
return
49-
}
53+
timeVM(program)
54+
} else if *engine == "eval" {
55+
timeEval(program)
56+
} else {
57+
timeEval(program)
58+
timeVM(program)
59+
}
60+
}
5061

51-
machine := vm.New(comp.Bytecode())
62+
func timeVM(program *ast.Program) {
63+
comp := compiler.New()
64+
err := comp.Compile(program)
65+
if err != nil {
66+
fmt.Printf("compiler error: %s", err)
67+
return
68+
}
5269

53-
start := time.Now()
70+
machine := vm.New(comp.Bytecode())
5471

55-
err = machine.Run()
56-
if err != nil {
57-
fmt.Printf("vm error: %s", err)
58-
return
59-
}
72+
start := time.Now()
6073

61-
duration = time.Since(start)
62-
result = machine.LastPoppedStackElem()
63-
} else if *engine == "eval" {
64-
env := object.NewEnvironment()
65-
start := time.Now()
66-
result = evaluator.Eval(program, env)
67-
duration = time.Since(start)
74+
err = machine.Run()
75+
if err != nil {
76+
fmt.Printf("vm error: %s", err)
77+
return
6878
}
6979

70-
fmt.Printf(
71-
"engine=%s, result=%s, duration=%s\n",
72-
*engine,
80+
duration := time.Since(start)
81+
result := machine.LastPoppedStackElem()
82+
83+
printResult("vm", result, duration)
84+
}
85+
86+
func timeEval(program *ast.Program) {
87+
env := object.NewEnvironment()
88+
start := time.Now()
89+
result := evaluator.Eval(program, env)
90+
duration := time.Since(start)
91+
92+
printResult("eval", result, duration)
93+
}
94+
95+
func printResult(engine string, result object.Object, duration time.Duration) {
96+
fmt.Printf("--> engine=%s, result=%s, duration=%s\n",
97+
engine,
7398
result.Inspect(),
7499
duration)
75100
}

MyCode/build/benchmark.exe

2.36 MB
Binary file not shown.

MyCode/build/monkey.exe

2.31 MB
Binary file not shown.

MyCode/file/file.go

+12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ package file
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/Aditya-Chowdhary/Monkey-Interpreter/compiler"
89
"github.com/Aditya-Chowdhary/Monkey-Interpreter/lexer"
910
"github.com/Aditya-Chowdhary/Monkey-Interpreter/parser"
1011
"github.com/Aditya-Chowdhary/Monkey-Interpreter/vm"
1112
)
1213

14+
func ReadCode(filepath string) (string, error) {
15+
fileContent, err := os.ReadFile(filepath)
16+
if err != nil {
17+
return "", fmt.Errorf("error: Invalid file name")
18+
}
19+
if !strings.HasSuffix(filepath, ".mky") {
20+
return "", fmt.Errorf("error: Invalid file name")
21+
}
22+
return string(fileContent), nil
23+
}
24+
1325
func RunCode(fileCode string) {
1426
l := lexer.New(fileCode)
1527
p := parser.New(l)

MyCode/main.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,24 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"os/user"
7-
"strings"
86

97
"github.com/Aditya-Chowdhary/Monkey-Interpreter/file"
108
"github.com/Aditya-Chowdhary/Monkey-Interpreter/repl"
119
)
1210

1311
func main() {
1412
if len(os.Args) == 1 {
15-
user, err := user.Current()
16-
if err != nil {
17-
panic(err)
18-
}
19-
fmt.Printf("Hello %s! This is the Monkey Programming Language!\n", user.Username)
20-
fmt.Printf("Feel free to type in commands!\n")
2113
repl.Start(os.Stdin, os.Stdout)
2214
} else if len(os.Args) == 2 {
2315
fileName := os.Args[1]
2416

25-
fileContent, err := os.ReadFile(fileName)
17+
fileContent, err := file.ReadCode(fileName)
2618
if err != nil {
27-
fmt.Println("Error: Invalid file name")
19+
fmt.Println(err)
2820
os.Exit(1)
2921
}
30-
if !strings.HasSuffix(fileName, ".mky") {
31-
fmt.Println("Error: Invalid file name")
32-
os.Exit(1)
33-
}
34-
file.RunCode(string(fileContent))
22+
file.RunCode(fileContent)
23+
3524
} else {
3625
fmt.Println("Error: Too many arguments!")
3726
fmt.Println(`Usage: "./monkey [file-path]"`)

MyCode/repl/repl.go

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7+
"os/user"
78

89
"github.com/Aditya-Chowdhary/Monkey-Interpreter/compiler"
910
"github.com/Aditya-Chowdhary/Monkey-Interpreter/lexer"
@@ -15,6 +16,13 @@ import (
1516
const PROMPT = ">> "
1617

1718
func Start(in io.Reader, out io.Writer) {
19+
user, err := user.Current()
20+
if err != nil {
21+
panic(err)
22+
}
23+
fmt.Printf("Hello %s! This is the Monkey Programming Language!\n", user.Username)
24+
fmt.Printf("Feel free to type in commands!\n")
25+
1826
scanner := bufio.NewScanner(in)
1927

2028
constants := []object.Object{}

README.md

+26-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,38 @@ This is a project to write an interpreter and a compiler for a made up language
1212
3. To run the REPL, run the command `./build/monkey`.
1313
4. Alternatively, create a file with extension `.mky` and run the command `./build/monkey [file]` to compile and run the file
1414

15+
### Windows
16+
17+
1. Clone the repository
18+
2. Run the command `cd .\MyCode\` to change directories.
19+
3. To run the REPL, run the command `.\build\monkey`.
20+
4. Alternatively, create a file with extension `.mky` and run the command `.\build\monkey [file]` to compile and run the file
21+
1522
A pre-written file `test.mky` is also present in the repository
1623

24+
1725
# Running benchmarks
1826

1927
### Linux
2028
1. Clone the repository
2129
2. Run the command `cd MyCode/` to change directories.
22-
3. To run using compiler and vm, run the command `./build/fibonacci -engine=vm`
23-
4. To run using interpreter, run the command `./build/fibonacci -engine=eval`
30+
3. Run command `.\build\benchmark -engine=vm|eval|[blank] [file-path]`
31+
1. To run using compiler and vm, `-engine = vm`
32+
2. To run using interpreter, `-engine = eval`
33+
3. To perform comparison between the two, ignore the `-engine` flag
34+
4. To perform benchmark testing on a custom file, include the file-path of the file. (Extension = `.mky`)
35+
5. Do not specify the file path before the `-engine` flag
36+
2437

25-
- Currently the benchmark is based on a pre-written fibonacci recursion program.
38+
### Windows
39+
1. Clone the repository
40+
2. Run the command `cd .\MyCode\` to change directories.
41+
3. Run command `.\build\benchmark -engine=vm|eval|[blank] [file-path]`
42+
1. To run using compiler and vm, `-engine = vm`
43+
2. To run using interpreter, `-engine = eval`
44+
3. To perform comparison between the two, ignore the `-engine` flag
45+
4. To perform benchmark testing on a custom file, include the file-path of the file. (Extension = `.mky`)
46+
5. Do not specify the file path before the `-engine` flag
47+
48+
- The default benchmark is based on a pre-written fibonacci recursion program.
2649
- The fibonacci number can be changed by going into `./benchmark/main.go Ln 30`, and editing the number in the function call.

0 commit comments

Comments
 (0)