From 5dd19df626dc905373fac2c62662f450376da3b5 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 10 Dec 2015 15:47:27 -0500 Subject: [PATCH] benchmarks: Added benchmarks for C, Go, Ruby, Python and Nit Signed-off-by: Lucas Bajolet --- benchmarks/json/scripts/c_parser.c | 48 +++++++++++++++++++++ benchmarks/json/scripts/json_ext.rb | 4 ++ benchmarks/json/scripts/json_parse.go | 20 +++++++++ benchmarks/json/scripts/json_pure.rb | 4 ++ benchmarks/json/scripts/nit_adhoc_utf_noropes.nit | 18 ++++++++ benchmarks/json/scripts/nit_adhoc_utf_ropes.nit | 21 +++++++++ benchmarks/json/scripts/nitcc_parser.nit | 18 ++++++++ benchmarks/json/scripts/python.py | 5 +++ 8 files changed, 138 insertions(+) create mode 100644 benchmarks/json/scripts/c_parser.c create mode 100644 benchmarks/json/scripts/json_ext.rb create mode 100644 benchmarks/json/scripts/json_parse.go create mode 100644 benchmarks/json/scripts/json_pure.rb create mode 100644 benchmarks/json/scripts/nit_adhoc_utf_noropes.nit create mode 100644 benchmarks/json/scripts/nit_adhoc_utf_ropes.nit create mode 100644 benchmarks/json/scripts/nitcc_parser.nit create mode 100644 benchmarks/json/scripts/python.py diff --git a/benchmarks/json/scripts/c_parser.c b/benchmarks/json/scripts/c_parser.c new file mode 100644 index 0000000..dfaf745 --- /dev/null +++ b/benchmarks/json/scripts/c_parser.c @@ -0,0 +1,48 @@ +#include "ujdecode.c" +#include +#include +#include +#include +#include +#include +#include + +// Gets the byte size of a file +int file_byte_size(char* path) +{ + int f = open(path, O_RDONLY); + if(f == -1) + return -1; + struct stat s; + int ln = 0; + if(!fstat(f, &s)) + ln = s.st_size; + close(f); + return ln; +} + +int main(int argc, char** argv) +{ + if(argc == 1) { + printf("Usage: ./c_parser file\n"); + exit(1); + } + + int fl_sz = file_byte_size(argv[1]); + char* input = malloc(fl_sz); + + FILE* ifl = fopen(argv[1], "r"); + if(ifl == NULL) { + printf("Error: cannot read file %s, are you sure permissions are set correctly ?\n", argv[1]); + exit(2); + } + int rd = fread(input, 1, fl_sz, ifl); + assert(rd == fl_sz); + + void *state; + + UJObject obj = UJDecode(input, fl_sz, NULL, &state); + + free(input); + UJFree(state); +} diff --git a/benchmarks/json/scripts/json_ext.rb b/benchmarks/json/scripts/json_ext.rb new file mode 100644 index 0000000..5c87062 --- /dev/null +++ b/benchmarks/json/scripts/json_ext.rb @@ -0,0 +1,4 @@ +require 'json/ext' + +txt = IO.read(ARGV.first) +my_hash = JSON.parse(txt) diff --git a/benchmarks/json/scripts/json_parse.go b/benchmarks/json/scripts/json_parse.go new file mode 100644 index 0000000..949a908 --- /dev/null +++ b/benchmarks/json/scripts/json_parse.go @@ -0,0 +1,20 @@ +package main + +import "io/ioutil" +import "encoding/json" +import "os" +import "fmt" + +func main() { + if len(os.Args) == 1 { + fmt.Println("Usage ./json_parse file") + os.Exit(-1) + } + dat, err := ioutil.ReadFile(os.Args[1]) + if err != nil { panic(err) } + + var obj interface{} + + jsonerr := json.Unmarshal(dat, &obj) + if jsonerr != nil { panic(jsonerr) } +} diff --git a/benchmarks/json/scripts/json_pure.rb b/benchmarks/json/scripts/json_pure.rb new file mode 100644 index 0000000..2630cfd --- /dev/null +++ b/benchmarks/json/scripts/json_pure.rb @@ -0,0 +1,4 @@ +require 'json/pure' + +txt = IO.read(ARGV.first) +my_hash = JSON.parse(txt) diff --git a/benchmarks/json/scripts/nit_adhoc_utf_noropes.nit b/benchmarks/json/scripts/nit_adhoc_utf_noropes.nit new file mode 100644 index 0000000..025a2e4 --- /dev/null +++ b/benchmarks/json/scripts/nit_adhoc_utf_noropes.nit @@ -0,0 +1,18 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json::string_parser + +var text = args.first.to_path.read_all_bytes.to_s +var json = text.parse_json diff --git a/benchmarks/json/scripts/nit_adhoc_utf_ropes.nit b/benchmarks/json/scripts/nit_adhoc_utf_ropes.nit new file mode 100644 index 0000000..75e1046 --- /dev/null +++ b/benchmarks/json/scripts/nit_adhoc_utf_ropes.nit @@ -0,0 +1,21 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json::string_parser + +var f = new FileReader.open(args.first) + +var text = f.read_all +f.close +var json = text.parse_json diff --git a/benchmarks/json/scripts/nitcc_parser.nit b/benchmarks/json/scripts/nitcc_parser.nit new file mode 100644 index 0000000..f09b746 --- /dev/null +++ b/benchmarks/json/scripts/nitcc_parser.nit @@ -0,0 +1,18 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json + +var text = args.first.to_path.read_all +var json = text.parse_json diff --git a/benchmarks/json/scripts/python.py b/benchmarks/json/scripts/python.py new file mode 100644 index 0000000..b8417e4 --- /dev/null +++ b/benchmarks/json/scripts/python.py @@ -0,0 +1,5 @@ +import sys +import json + +json_data=open(sys.argv[1]).read() +data = json.loads(json_data) -- 1.7.9.5