Skip to content

Commit ecc4a1a

Browse files
committed
Added timed test for JSON
1 parent 52fda56 commit ecc4a1a

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
*.exe
99
*.native
1010
*.byte
11-
_build
11+
ocamlprof.dump
12+
_build

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ https://opam.ocaml.org/doc/Packaging.html
44
https://github.com/ocaml/opam-repository
55

66
https://ocaml.org/learn/tutorials/performance_and_profiling.html
7+
https://stackoverflow.com/questions/9061421/running-time-in-ocaml
8+
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html
9+
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Arg.html
710

811
http://numeraljs.com/
912
http://www.json-generator.com/

json/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
.PHONY: all clean token json lexer parser test
2-
.PHONY: build_parser debug_parser interfaces
2+
.PHONY: build_parser debug_parser interfaces time_test
33

44
all: interfaces build_parser
55
make test | wc
6+
make time_test
67

78
test:
89
./run_json_test.sh
910

11+
time_test:
12+
./run_time_test.sh
13+
1014
debug_parser: build_parser
1115
ocamldebug ./test_json_parser.byte
1216

json/run_time_test.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash -x
2+
3+
ocamlopt -p -ccopt -O2 -o time_test_json \
4+
json.ml lexer.ml parser.ml time_test_json.ml \
5+
&& ./time_test_json test_tiny.json \
6+
&& ./time_test_json test_small.json \
7+
&& echo Extracting test_medium.json.gz \
8+
&& time gunzip < test_medium.json.gz | ./time_test_json

json/time_test_json.ml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let parse_in_chan in_chan () =
2+
Parser.parse_top_level @@ Lexer.lex @@ Stream.of_channel @@ in_chan
3+
4+
let parse_file file () =
5+
parse_in_chan @@ open_in file
6+
7+
let time f =
8+
let t = Sys.time () in
9+
let _ = f () in
10+
Printf.printf "\nExecution time: %fs\n" (Sys.time() -. t)
11+
12+
let usage () =
13+
Printf.printf "1) Run: %s < input.json\n" Sys.argv.(0) ;
14+
Printf.printf "2) Run: %s input.json\n" Sys.argv.(0)
15+
16+
let () =
17+
match Array.length Sys.argv with
18+
| 1 ->
19+
Printf.printf "Parsing JSON from STDIN\n";
20+
time @@ parse_in_chan stdin
21+
| 2 ->
22+
let file = Sys.argv.(1) in
23+
Printf.printf "Parsing JSON from the file %s\n" file;
24+
time @@ parse_file file
25+
| _ -> usage ()

0 commit comments

Comments
 (0)