<s> import time <EOL> import unittest <EOL> from nose . tools import assert_true <EOL> from robotide . namespace import Namespace <EOL> from robotide . controller import Project <EOL> from resources import MessageRecordingLoadObserver , FakeSettings <EOL> from datafilereader import TESTCASEFILE_WITH_EVERYTHING , KW1000_TESTCASEFILE , KW2000_TESTCASEFILE , KW3000_TESTCASEFILE , KW4000_TESTCASEFILE <EOL> from robotide . spec . librarymanager import LibraryManager <EOL> class TestNamespacePerformance ( unittest . TestCase ) : <EOL> SAFETY_MARGIN = <NUM_LIT> <EOL> RELEVANT_B_RELATIVE_TO_C = <NUM_LIT> <EOL> def test_user_keyword_find_performance ( self ) : <EOL> self . _test_keyword_find_performance ( '<STR_LIT>' ) <EOL> def test_library_keyword_find_performance ( self ) : <EOL> self . _test_keyword_find_performance ( '<STR_LIT>' ) <EOL> def _test_keyword_find_performance ( self , find_function_name ) : <EOL> times = <NUM_LIT> <EOL> end_time = self . _execute_keyword_find_function_n_times ( find_function_name , times ) <EOL> assert_true ( end_time < <NUM_LIT:0.5> , '<STR_LIT>' % ( times , end_time ) ) <EOL> def _FLICKERS_measure_user_keyword_find_performance ( self ) : <EOL> times = <NUM_LIT:1000> <EOL> kw1000_result = self . _execute_keyword_find_function_n_times ( '<STR_LIT>' , times , KW1000_TESTCASEFILE ) <EOL> kw2000_result = self . _execute_keyword_find_function_n_times ( '<STR_LIT>' , times , KW2000_TESTCASEFILE ) <EOL> kw3000_result = self . _execute_keyword_find_function_n_times ( '<STR_LIT>' , times , KW3000_TESTCASEFILE ) <EOL> a , b , c = self . _calculate_power2_estimate_constants ( kw1000_result , kw2000_result , kw3000_result ) <EOL> assert_true ( b > c or ( c <= <NUM_LIT:0> ) , <EOL> '<STR_LIT>' % ( kw1000_result , kw2000_result , kw3000_result ) ) <EOL> if c > <NUM_LIT:0> and ( b <= <NUM_LIT:0> or c / b > self . RELEVANT_B_RELATIVE_TO_C ) : <EOL> kw4000_result = self . _execute_keyword_find_function_n_times ( '<STR_LIT>' , times , KW4000_TESTCASEFILE ) <EOL> self . _verify_that_power2_estimate_overestimates ( a , b , c , kw1000_result , kw2000_result , kw3000_result , kw4000_result ) <EOL> def _verify_that_power2_estimate_overestimates ( self , a , b , c , kw1000_result , kw2000_result , kw3000_result , kw4000_result ) : <EOL> def power2estimate ( kw_amount ) : <EOL> x = kw_amount / <NUM_LIT:1000> <EOL> return a + b * x + c * x ** <NUM_LIT:2> <EOL> assert_true ( power2estimate ( <NUM_LIT> ) * self . SAFETY_MARGIN > kw4000_result , <EOL> '<STR_LIT>' % ( kw1000_result , kw2000_result , kw3000_result , kw4000_result ) ) <EOL> def _calculate_power2_estimate_constants ( self , kw1000_result , kw2000_result , kw3000_result ) : <EOL> c = ( kw3000_result - kw1000_result - <NUM_LIT:2> * ( kw2000_result - kw1000_result ) ) / <NUM_LIT:2> <EOL> b = kw2000_result - kw1000_result - <NUM_LIT:3> * c <EOL> a = kw1000_result - c - b <EOL> return a , b , c <EOL> def _load ( self , testcasefile ) : <EOL> ns = Namespace ( FakeSettings ( ) ) <EOL> library_manager = LibraryManager ( '<STR_LIT>' ) <EOL> library_manager . create_database ( ) <EOL> project = Project ( ns , settings = ns . _settings , library_manager = library_manager ) <EOL> project . load_datafile ( testcasefile , <EOL> MessageRecordingLoadObserver ( ) ) <EOL> return ns , project . _controller . data , library_manager <EOL> def _execute_keyword_find_function_n_times ( self , function , n , filename = TESTCASEFILE_WITH_EVERYTHING ) : <EOL> ns , testcasefile , library_manager = self . _load ( filename ) <EOL> try : <EOL> func = getattr ( ns , function ) <EOL> func ( testcasefile , '<STR_LIT>' ) <EOL> start_time = time . time ( ) <EOL> for i in range ( n ) : <EOL> func ( testcasefile , '<STR_LIT>' % i ) <EOL> return time . time ( ) - start_time <EOL> finally : <EOL> library_manager . stop ( ) </s>
