<s> import math <EOL> from random_words import RandomWords <EOL> from st2reactor . container . hash_partitioner import HashPartitioner , Range <EOL> from st2tests import config <EOL> from st2tests import DbTestCase <EOL> from st2tests . fixturesloader import FixturesLoader <EOL> PACK = '<STR_LIT>' <EOL> FIXTURES_1 = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> class HashPartitionerTest ( DbTestCase ) : <EOL> models = None <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( HashPartitionerTest , cls ) . setUpClass ( ) <EOL> cls . models = FixturesLoader ( ) . save_fixtures_to_db ( <EOL> fixtures_pack = PACK , fixtures_dict = FIXTURES_1 ) <EOL> config . parse_args ( ) <EOL> def test_full_range_hash_partitioner ( self ) : <EOL> partitioner = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sensors = partitioner . get_sensors ( ) <EOL> self . assertEqual ( len ( sensors ) , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def test_multi_range_hash_partitioner ( self ) : <EOL> range_third = int ( Range . RANGE_MAX_VALUE / <NUM_LIT:3> ) <EOL> range_two_third = range_third * <NUM_LIT:2> <EOL> hash_ranges = '<STR_LIT>' . format ( <EOL> range_third = range_third , range_two_third = range_two_third ) <EOL> partitioner = HashPartitioner ( '<STR_LIT>' , hash_ranges ) <EOL> sensors = partitioner . get_sensors ( ) <EOL> self . assertEqual ( len ( sensors ) , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def test_split_range_hash_partitioner ( self ) : <EOL> range_mid = int ( Range . RANGE_MAX_VALUE / <NUM_LIT:2> ) <EOL> partitioner = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' % range_mid ) <EOL> sensors1 = partitioner . get_sensors ( ) <EOL> partitioner = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' % range_mid ) <EOL> sensors2 = partitioner . get_sensors ( ) <EOL> self . assertEqual ( len ( sensors1 ) + len ( sensors2 ) , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def test_hash_effectiveness ( self ) : <EOL> range_third = int ( Range . RANGE_MAX_VALUE / <NUM_LIT:3> ) <EOL> partitioner1 = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' % range_third ) <EOL> partitioner2 = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' % ( range_third , range_third + range_third ) ) <EOL> partitioner3 = HashPartitioner ( '<STR_LIT>' , '<STR_LIT>' % ( range_third + range_third ) ) <EOL> refs_count = <NUM_LIT:1000> <EOL> refs = self . _generate_refs ( count = refs_count ) <EOL> p1_count = <NUM_LIT:0> <EOL> p2_count = <NUM_LIT:0> <EOL> p3_count = <NUM_LIT:0> <EOL> for ref in refs : <EOL> if partitioner1 . _is_in_hash_range ( ref ) : <EOL> p1_count += <NUM_LIT:1> <EOL> if partitioner2 . _is_in_hash_range ( ref ) : <EOL> p2_count += <NUM_LIT:1> <EOL> if partitioner3 . _is_in_hash_range ( ref ) : <EOL> p3_count += <NUM_LIT:1> <EOL> self . assertEqual ( p1_count + p2_count + p3_count , refs_count , <EOL> '<STR_LIT>' ) <EOL> mean = refs_count / <NUM_LIT:3> <EOL> variance = float ( ( p1_count - mean ) ** <NUM_LIT:2> + ( p1_count - mean ) ** <NUM_LIT:2> + ( p3_count - mean ) ** <NUM_LIT:2> ) / <NUM_LIT:3> <EOL> sd = math . sqrt ( variance ) <EOL> self . assertTrue ( sd / mean <= <NUM_LIT> , '<STR_LIT>' ) <EOL> def _generate_refs ( self , count = <NUM_LIT:10> ) : <EOL> random_word_count = int ( math . sqrt ( count ) ) + <NUM_LIT:1> <EOL> words = RandomWords ( ) . random_words ( count = random_word_count ) <EOL> x_index = <NUM_LIT:0> <EOL> y_index = <NUM_LIT:0> <EOL> while count > <NUM_LIT:0> : <EOL> yield '<STR_LIT>' % ( words [ x_index ] , words [ y_index ] ) <EOL> if y_index < len ( words ) - <NUM_LIT:1> : <EOL> y_index += <NUM_LIT:1> <EOL> else : <EOL> x_index += <NUM_LIT:1> <EOL> y_index = <NUM_LIT:0> <EOL> count -= <NUM_LIT:1> <EOL> return </s>
