Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 80a177b

Browse files
committed
updated benchmark script
1 parent 801a8d3 commit 80a177b

File tree

7 files changed

+156
-2
lines changed

7 files changed

+156
-2
lines changed

.depend

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ tests/runner: tests/test.cpp tests/../test_helper.hpp tests/../bytes.hpp \
7171
tests/../shared_byte_list.hpp tests/../parser_result.hpp \
7272
tests/../api.hpp
7373

74+
benchmark/cpp-parser: benchmark/benchmark.cpp benchmark/rb_filelist.hpp \
75+
benchmark/../lib-ruby-parser.hpp
76+

benchmark/benchmark.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <chrono>
4+
5+
#include "rb_filelist.hpp"
6+
#include "../lib-ruby-parser.hpp"
7+
8+
int main()
9+
{
10+
char *filelist_path = std::getenv("FILELIST_PATH");
11+
if (filelist_path == nullptr)
12+
{
13+
std::cerr << "\nNo FILELIST_PATH provided.\n\nUsage: FILELIST_PATH=path/to/filelist ./path/to/exe\n";
14+
return 1;
15+
}
16+
17+
FileList filelist(filelist_path);
18+
19+
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
20+
21+
for (size_t i = 0; i < filelist.files.size(); i++)
22+
{
23+
FileToParse file = std::move(filelist.files[i]);
24+
25+
lib_ruby_parser::ParserOptions options(
26+
std::move(file.filepath),
27+
lib_ruby_parser::MaybeDecoder::None(),
28+
lib_ruby_parser::MaybeTokenRewriter::None(),
29+
false);
30+
lib_ruby_parser::ByteList input = std::move(file.content);
31+
32+
lib_ruby_parser::parse(
33+
std::move(options),
34+
std::move(input));
35+
}
36+
37+
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
38+
39+
std::cout << double(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count()) / 1000000000 << "[s]" << std::endl;
40+
41+
return 0;
42+
}

benchmark/build.mk

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
benchmark/cpp-parser: $(STATIC_LIB) lib-ruby-parser.hpp benchmark/benchmark.cpp
2+
$(call build_cxx_exe,benchmark/benchmark.cpp $(STATIC_LIB),benchmark/cpp-parser)
3+
4+
define download_latest_bench_asset
5+
ASSET_NAME=$(1) SAVE_AS=$(2) ruby benchmark/download_asset.rb
6+
endef
7+
8+
benchmark/download:
9+
@echo "Downloading repos.zip..."
10+
$(call download_latest_bench_asset,repos.zip,benchmark/repos.zip)
11+
@echo "Unpacking repos.zip..."
12+
unzip --help
13+
ls -l benchmark/repos.zip
14+
unzip -q benchmark/repos.zip -d benchmark
15+
16+
@echo "Downloading Rust parser..."
17+
$(call download_latest_bench_asset,$(BENCHMARK_RUNNER_ASSET_NAME),benchmark/rust-parser)
18+
chmod +x benchmark/rust-parser
19+
20+
@echo "Downloading Ruby parser..."
21+
$(call download_latest_bench_asset,ruby-parser.rb,benchmark/ruby-parser.rb)
22+
23+
BENCHMARK_ASSETS = \
24+
benchmark/repos \
25+
benchmark/cpp-parser \
26+
benchmark/filelist \
27+
benchmark/repos.zip \
28+
benchmark/ruby-parser.rb \
29+
benchmark/rust-parser \
30+
benchmark/stats
31+
32+
benchmark/clear:
33+
rm -rf $(BENCHMARK_ASSETS)
34+
35+
define run_benchmark
36+
cd benchmark && FILELIST_PATH=filelist $(1)
37+
endef
38+
39+
benchmark/compare: benchmark/cpp-parser
40+
$(call run_benchmark, ./rust-parser)
41+
$(call run_benchmark, ruby ruby-parser.rb)
42+
$(call run_benchmark, ./cpp-parser)
43+
44+
BENCHMARK_RECORDING = $(TARGET).benchmark-out
45+
benchmark/record: benchmark/cpp-parser
46+
echo "Rust:" > $(BENCHMARK_RECORDING)
47+
$(call run_benchmark, ./rust-parser >> ../$(BENCHMARK_RECORDING))
48+
echo "Ruby:" >> $(BENCHMARK_RECORDING)
49+
$(call run_benchmark, ruby ruby-parser.rb >> ../$(BENCHMARK_RECORDING))
50+
echo "C:" >> $(BENCHMARK_RECORDING)
51+
$(call run_benchmark, ./cpp-parser >> ../$(BENCHMARK_RECORDING))

benchmark/cpp-parser

1.61 MB
Binary file not shown.

benchmark/download_asset.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'open-uri'
2+
require 'json'
3+
4+
def abort(message)
5+
$stderr.puts("Error: #{message}\n\nAborting.")
6+
exit(1)
7+
end
8+
9+
asset_name = ENV.fetch('ASSET_NAME') { abort 'ASSET_NAME env var must be provided.' }
10+
asset_url = "https://github.com/lib-ruby-parser/bench/releases/download/v0.0.1/#{asset_name}"
11+
save_as = ENV.fetch('SAVE_AS') { abort 'SAVE_AS env var must be provided' }
12+
13+
File.open(save_as, 'wb') do |file|
14+
URI.open(asset_url, 'rb') do |asset|
15+
file.write(asset.read)
16+
end
17+
end

benchmark/rb_filelist.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <fstream>
2+
#include <vector>
3+
#include <string>
4+
#include <streambuf>
5+
#include "../lib-ruby-parser.hpp"
6+
7+
class FileToParse
8+
{
9+
public:
10+
lib_ruby_parser::String filepath = lib_ruby_parser::String(nullptr, 0, 0);
11+
lib_ruby_parser::ByteList content = lib_ruby_parser::ByteList(nullptr, 0, 0);
12+
13+
FileToParse(std::string filepath_)
14+
{
15+
filepath = lib_ruby_parser::String::Copied(
16+
filepath_.c_str());
17+
18+
std::ifstream f(filepath_);
19+
std::string content_s = std::string((std::istreambuf_iterator<char>(f)),
20+
std::istreambuf_iterator<char>());
21+
content = lib_ruby_parser::ByteList::Copied(
22+
content_s.c_str(),
23+
content_s.length());
24+
}
25+
};
26+
27+
class FileList
28+
{
29+
public:
30+
std::vector<FileToParse> files;
31+
32+
FileList(char *filelist_path)
33+
{
34+
std::ifstream f(filelist_path);
35+
std::string line;
36+
while (std::getline(f, line))
37+
{
38+
files.push_back(FileToParse(line));
39+
}
40+
}
41+
};

scripts/update-depend.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ done
1515
$CC -MT "tests/runner" -MM "tests/test.cpp" >> .depend
1616
echo "" >> .depend
1717

18-
# $CC -MT "benchmark/c-parser" -MM "benchmark/benchmark.cpp" >> .depend
19-
# echo "" >> .depend
18+
$CC -MT "benchmark/cpp-parser" -MM "benchmark/benchmark.cpp" >> .depend
19+
echo "" >> .depend

0 commit comments

Comments
 (0)