From 6ed6803d38c53f70ebed03f3b5adf7f01b65a68d Mon Sep 17 00:00:00 2001 From: micbou Date: Thu, 8 Jun 2017 01:47:01 +0200 Subject: [PATCH] Add FilterAndSortCandidates benchmarks --- cpp/ycm/PythonSupport.h | 4 +- cpp/ycm/benchmarks/BenchUtils.cpp | 40 +++++++ cpp/ycm/benchmarks/BenchUtils.h | 32 ++++++ .../benchmarks/IdentifierCompleter_bench.cpp | 16 +-- cpp/ycm/benchmarks/PythonSupport_bench.cpp | 100 ++++++++++++++++++ 5 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 cpp/ycm/benchmarks/BenchUtils.cpp create mode 100644 cpp/ycm/benchmarks/BenchUtils.h create mode 100644 cpp/ycm/benchmarks/PythonSupport_bench.cpp diff --git a/cpp/ycm/PythonSupport.h b/cpp/ycm/PythonSupport.h index 7dcda0a368..40b41d191d 100644 --- a/cpp/ycm/PythonSupport.h +++ b/cpp/ycm/PythonSupport.h @@ -18,6 +18,8 @@ #ifndef PYTHONSUPPORT_H_KWGFEX0V #define PYTHONSUPPORT_H_KWGFEX0V +#include "DLLDefines.h" + #include namespace YouCompleteMe { @@ -26,7 +28,7 @@ namespace YouCompleteMe { /// python list |candidates|, a |candidate_property| on which to filter and sort /// the candidates and a user query, returns a new sorted python list with the /// original objects that survived the filtering. -boost::python::list FilterAndSortCandidates( +YCM_DLL_EXPORT boost::python::list FilterAndSortCandidates( const boost::python::list &candidates, const std::string &candidate_property, const std::string &query ); diff --git a/cpp/ycm/benchmarks/BenchUtils.cpp b/cpp/ycm/benchmarks/BenchUtils.cpp new file mode 100644 index 0000000000..dc36d21f7f --- /dev/null +++ b/cpp/ycm/benchmarks/BenchUtils.cpp @@ -0,0 +1,40 @@ +// Copyright (C) 2017 ycmd contributors +// +// This file is part of ycmd. +// +// ycmd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ycmd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with ycmd. If not, see . + +#include "BenchUtils.h" + +namespace YouCompleteMe { + +std::vector< std::string > GenerateCandidatesWithCommonPrefix( + const std::string prefix, int number ) { + + std::vector< std::string > candidates; + + for ( int i = 0; i < number; ++i ) { + std::string candidate = ""; + int letter = i; + for ( int pos = 0; pos < 5; letter /= 26, ++pos ) { + candidate = std::string( 1, letter % 26 + 'a' ) + candidate; + } + candidate = prefix + candidate; + candidates.push_back( candidate ); + } + + return candidates; +} + +} // namespace YouCompleteMe diff --git a/cpp/ycm/benchmarks/BenchUtils.h b/cpp/ycm/benchmarks/BenchUtils.h new file mode 100644 index 0000000000..f3be129471 --- /dev/null +++ b/cpp/ycm/benchmarks/BenchUtils.h @@ -0,0 +1,32 @@ +// Copyright (C) 2017 ycmd contributores +// +// This file is part of ycmd. +// +// ycmd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ycmd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with ycmd. If not, see . + +#ifndef BENCHUTILS_H_7UY2GEP1 +#define BENCHUTILS_H_7UY2GEP1 + +#include +#include + +namespace YouCompleteMe { + +// Generate a list of |number| candidates of the form |prefix|[a-z]{5}. +std::vector< std::string > GenerateCandidatesWithCommonPrefix( + const std::string prefix, int number ); + +} // namespace YouCompleteMe + +#endif /* end of include guard: BENCHUTILS_H_7UY2GEP1 */ diff --git a/cpp/ycm/benchmarks/IdentifierCompleter_bench.cpp b/cpp/ycm/benchmarks/IdentifierCompleter_bench.cpp index cf92e39c99..47b57e5f93 100644 --- a/cpp/ycm/benchmarks/IdentifierCompleter_bench.cpp +++ b/cpp/ycm/benchmarks/IdentifierCompleter_bench.cpp @@ -16,6 +16,7 @@ // along with ycmd. If not, see . #include "benchmark/benchmark_api.h" +#include "BenchUtils.h" #include "CandidateRepository.h" #include "IdentifierCompleter.h" @@ -31,18 +32,10 @@ class IdentifierCompleterFixture : public benchmark::Fixture { BENCHMARK_DEFINE_F( IdentifierCompleterFixture, CandidatesWithCommonPrefix )( benchmark::State& state ) { - // Generate a list of candidates of the form a_A_a_[a-z]{5}. - std::vector< std::string > candidates; - for ( int i = 0; i < state.range( 0 ); ++i ) { - std::string candidate = ""; - int letter = i; - for ( int pos = 0; pos < 5; letter /= 26, ++pos ) { - candidate = std::string( 1, letter % 26 + 'a' ) + candidate; - } - candidate = "a_A_a_" + candidate; - candidates.push_back( candidate ); - } + std::vector< std::string > candidates; + candidates = GenerateCandidatesWithCommonPrefix( "a_A_a_", + state.range( 0 ) ); IdentifierCompleter completer( candidates ); while ( state.KeepRunning() ) @@ -51,6 +44,7 @@ BENCHMARK_DEFINE_F( IdentifierCompleterFixture, CandidatesWithCommonPrefix )( state.SetComplexityN( state.range( 0 ) ); } + BENCHMARK_REGISTER_F( IdentifierCompleterFixture, CandidatesWithCommonPrefix ) ->RangeMultiplier( 2 ) ->Range( 1, 1 << 16 ) diff --git a/cpp/ycm/benchmarks/PythonSupport_bench.cpp b/cpp/ycm/benchmarks/PythonSupport_bench.cpp new file mode 100644 index 0000000000..b5260cd0c3 --- /dev/null +++ b/cpp/ycm/benchmarks/PythonSupport_bench.cpp @@ -0,0 +1,100 @@ +// Copyright (C) 2017 ycmd contributors +// +// This file is part of ycmd. +// +// ycmd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ycmd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with ycmd. If not, see . + +#include "benchmark/benchmark_api.h" +#include "BenchUtils.h" +#include "CandidateRepository.h" +// iostream is included because of a bug with Python earlier than 2.7.12 +// and 3.5.3 on OSX and FreeBSD. +#include +#include "PythonSupport.h" + +namespace YouCompleteMe { + +class PythonSupportFixture : public benchmark::Fixture { +public: + void SetUp( const benchmark::State& state ) { + CandidateRepository::Instance().ClearCandidates(); + } +}; + + +BENCHMARK_DEFINE_F( PythonSupportFixture, + FilterAndSortUnstoredCandidatesWithCommonPrefix )( + benchmark::State& state ) { + + std::vector< std::string > raw_candidates; + raw_candidates = GenerateCandidatesWithCommonPrefix( "a_A_a_", + state.range( 0 ) ); + + boost::python::list candidates; + for ( auto insertion_text : raw_candidates ) { + boost::python::dict candidate; + candidate[ "insertion_text" ] = insertion_text; + candidates.append( candidate ); + } + + while ( state.KeepRunning() ) { + state.PauseTiming(); + CandidateRepository::Instance().ClearCandidates(); + state.ResumeTiming(); + FilterAndSortCandidates( candidates, "insertion_text", "aA" ); + } + + state.SetComplexityN( state.range( 0 ) ); +} + + +BENCHMARK_DEFINE_F( PythonSupportFixture, + FilterAndSortStoredCandidatesWithCommonPrefix )( + benchmark::State& state ) { + + std::vector< std::string > raw_candidates; + raw_candidates = GenerateCandidatesWithCommonPrefix( "a_A_a_", + state.range( 0 ) ); + + boost::python::list candidates; + for ( auto insertion_text : raw_candidates ) { + boost::python::dict candidate; + candidate[ "insertion_text" ] = insertion_text; + candidates.append( candidate ); + } + + // Store the candidates in the repository. + FilterAndSortCandidates( candidates, "insertion_text", "aA" ); + + while ( state.KeepRunning() ) + FilterAndSortCandidates( candidates, "insertion_text", "aA" ); + + state.SetComplexityN( state.range( 0 ) ); +} + + +BENCHMARK_REGISTER_F( PythonSupportFixture, + FilterAndSortUnstoredCandidatesWithCommonPrefix ) + ->RangeMultiplier( 2 ) + ->Range( 1, 1 << 16 ) + ->Complexity(); + + +BENCHMARK_REGISTER_F( PythonSupportFixture, + FilterAndSortStoredCandidatesWithCommonPrefix ) + ->RangeMultiplier( 2 ) + ->Range( 1, 1 << 16 ) + ->Complexity(); + +} // namespace YouCompleteMe