Skip to content

Commit

Permalink
Add FilterAndSortCandidates benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
micbou committed Aug 25, 2017
1 parent 0a8f633 commit 6ed6803
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cpp/ycm/PythonSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef PYTHONSUPPORT_H_KWGFEX0V
#define PYTHONSUPPORT_H_KWGFEX0V

#include "DLLDefines.h"

#include <boost/python.hpp>

namespace YouCompleteMe {
Expand All @@ -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 );
Expand Down
40 changes: 40 additions & 0 deletions cpp/ycm/benchmarks/BenchUtils.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#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
32 changes: 32 additions & 0 deletions cpp/ycm/benchmarks/BenchUtils.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#ifndef BENCHUTILS_H_7UY2GEP1
#define BENCHUTILS_H_7UY2GEP1

#include <string>
#include <vector>

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 */
16 changes: 5 additions & 11 deletions cpp/ycm/benchmarks/IdentifierCompleter_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with ycmd. If not, see <http://www.gnu.org/licenses/>.

#include "benchmark/benchmark_api.h"
#include "BenchUtils.h"
#include "CandidateRepository.h"
#include "IdentifierCompleter.h"

Expand All @@ -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() )
Expand All @@ -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 )
Expand Down
100 changes: 100 additions & 0 deletions cpp/ycm/benchmarks/PythonSupport_bench.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#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 <iostream>
#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

0 comments on commit 6ed6803

Please sign in to comment.