<s> import platform <EOL> import subprocess <EOL> from unittest import skipIf <EOL> from uuid import uuid4 <EOL> from ipaddr import IPAddress <EOL> from zope . interface . verify import verifyClass <EOL> from twisted . internet . task import Clock <EOL> from twisted . internet . threads import deferToThread <EOL> from flocker . apiclient import FakeFlockerClient , Node <EOL> from flocker . testtools import TestCase , AsyncTestCase <EOL> from benchmark . cluster import BenchmarkCluster <EOL> from benchmark . _interfaces import IMetric <EOL> from benchmark . metrics . cputime import ( <EOL> WALLCLOCK_LABEL , CPUTime , CPUParser , get_node_init_process_name , <EOL> get_node_cpu_times , compute_change , _GET_INIT_PROCESS_NAME_COMMAND , <EOL> ) <EOL> _pid_1_name = subprocess . check_output ( _GET_INIT_PROCESS_NAME_COMMAND ) . strip ( ) <EOL> on_linux = skipIf ( platform . system ( ) != '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CPUParseTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_blank_line ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = CPUParser ( Clock ( ) ) <EOL> parser . lineReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( parser . result , { WALLCLOCK_LABEL : <NUM_LIT:0> } ) <EOL> def test_nodays ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = CPUParser ( Clock ( ) ) <EOL> parser . lineReceived ( '<STR_LIT>' ) <EOL> expected_cputime = ( <NUM_LIT:12> * <NUM_LIT> + <NUM_LIT> ) * <NUM_LIT> + <NUM_LIT> <EOL> self . assertEqual ( <EOL> parser . result , { '<STR_LIT>' : expected_cputime , WALLCLOCK_LABEL : <NUM_LIT:0> } <EOL> ) <EOL> def test_days ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = CPUParser ( Clock ( ) ) <EOL> parser . lineReceived ( '<STR_LIT>' ) <EOL> expected_cputime = ( ( <NUM_LIT:5> * <NUM_LIT> + <NUM_LIT:12> ) * <NUM_LIT> + <NUM_LIT> ) * <NUM_LIT> + <NUM_LIT> <EOL> self . assertEqual ( <EOL> parser . result , { '<STR_LIT>' : expected_cputime , WALLCLOCK_LABEL : <NUM_LIT:0> } <EOL> ) <EOL> def test_unexpected_line ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = CPUParser ( Clock ( ) ) <EOL> exception = self . assertRaises ( <EOL> ValueError , <EOL> parser . lineReceived , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( exception . args [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> def test_unexpected_parse ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = CPUParser ( Clock ( ) ) <EOL> exception = self . assertRaises ( <EOL> ValueError , <EOL> parser . lineReceived , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( exception . args [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> class _LocalRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> def _run ( self , command_args , handle_stdout ) : <EOL> output = subprocess . check_output ( command_args ) <EOL> for line in output . split ( '<STR_LIT:\n>' ) : <EOL> handle_stdout ( line ) <EOL> def run ( self , node , command_args , handle_stdout ) : <EOL> return deferToThread ( self . _run , command_args , handle_stdout ) <EOL> class GetNodeInitProcessNameTests ( AsyncTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_get_node_init_process_name ( self ) : <EOL> """<STR_LIT>""" <EOL> d = get_node_init_process_name ( <EOL> _LocalRunner ( ) , <EOL> Node ( uuid = uuid4 ( ) , public_address = IPAddress ( '<STR_LIT>' ) ) , <EOL> ) <EOL> def check ( result ) : <EOL> self . assertEqual ( result , _pid_1_name ) <EOL> d . addCallback ( check ) <EOL> return d <EOL> class GetNodeCPUTimeTests ( AsyncTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ on_linux <EOL> def test_get_node_cpu_times ( self ) : <EOL> """<STR_LIT>""" <EOL> d = get_node_cpu_times ( <EOL> Clock ( ) , <EOL> _LocalRunner ( ) , <EOL> Node ( uuid = uuid4 ( ) , public_address = IPAddress ( '<STR_LIT>' ) ) , <EOL> _pid_1_name , <EOL> [ _pid_1_name ] , <EOL> ) <EOL> def check ( result ) : <EOL> self . assertEqual ( <EOL> result . keys ( ) , [ _pid_1_name , WALLCLOCK_LABEL ] <EOL> ) <EOL> d . addCallback ( check ) <EOL> return d <EOL> @ on_linux <EOL> def test_no_such_process ( self ) : <EOL> """<STR_LIT>""" <EOL> d = get_node_cpu_times ( <EOL> Clock ( ) , <EOL> _LocalRunner ( ) , <EOL> Node ( uuid = uuid4 ( ) , public_address = IPAddress ( '<STR_LIT>' ) ) , <EOL> _pid_1_name , <EOL> [ '<STR_LIT>' ] , <EOL> ) <EOL> d . addCallback ( self . assertEqual , { WALLCLOCK_LABEL : <NUM_LIT:0.0> } ) <EOL> return d <EOL> class ComputeChangesTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_compute_change ( self ) : <EOL> """<STR_LIT>""" <EOL> labels = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> before = [ { '<STR_LIT:foo>' : <NUM_LIT:3> , '<STR_LIT:bar>' : <NUM_LIT:5> } , { '<STR_LIT:foo>' : <NUM_LIT:10> , '<STR_LIT:bar>' : <NUM_LIT:2> } ] <EOL> after = [ { '<STR_LIT:foo>' : <NUM_LIT:4> , '<STR_LIT:bar>' : <NUM_LIT:5> } , { '<STR_LIT:foo>' : <NUM_LIT:12> , '<STR_LIT:bar>' : <NUM_LIT:5> } ] <EOL> result = compute_change ( labels , before , after ) <EOL> self . assertEqual ( result , { <EOL> '<STR_LIT>' : { '<STR_LIT:foo>' : <NUM_LIT:1> , '<STR_LIT:bar>' : <NUM_LIT:0> } , <EOL> '<STR_LIT>' : { '<STR_LIT:foo>' : <NUM_LIT:2> , '<STR_LIT:bar>' : <NUM_LIT:3> } , <EOL> } ) <EOL> def test_compute_change_new_proc ( self ) : <EOL> """<STR_LIT>""" <EOL> labels = [ '<STR_LIT>' ] <EOL> before = [ { '<STR_LIT:foo>' : <NUM_LIT> } ] <EOL> after = [ { '<STR_LIT:foo>' : <NUM_LIT> , '<STR_LIT:bar>' : <NUM_LIT:5> } ] <EOL> result = compute_change ( labels , before , after ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , result [ '<STR_LIT>' ] ) <EOL> def test_compute_change_lost_proc ( self ) : <EOL> """<STR_LIT>""" <EOL> labels = [ '<STR_LIT>' ] <EOL> before = [ { '<STR_LIT:foo>' : <NUM_LIT> , '<STR_LIT:bar>' : <NUM_LIT:5> } ] <EOL> after = [ { '<STR_LIT:foo>' : <NUM_LIT> } ] <EOL> result = compute_change ( labels , before , after ) <EOL> self . assertNotIn ( '<STR_LIT:bar>' , result [ '<STR_LIT>' ] ) <EOL> class CPUTimeTests ( AsyncTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_implements_IMetric ( self ) : <EOL> """<STR_LIT>""" <EOL> verifyClass ( IMetric , CPUTime ) <EOL> @ on_linux <EOL> def test_cpu_time ( self ) : <EOL> """<STR_LIT>""" <EOL> clock = Clock ( ) <EOL> node1 = Node ( uuid = uuid4 ( ) , public_address = IPAddress ( '<STR_LIT>' ) ) <EOL> node2 = Node ( uuid = uuid4 ( ) , public_address = IPAddress ( '<STR_LIT>' ) ) <EOL> metric = CPUTime ( <EOL> clock , <EOL> BenchmarkCluster ( <EOL> IPAddress ( '<STR_LIT>' ) , <EOL> lambda reactor : FakeFlockerClient ( [ node1 , node2 ] ) , <EOL> { } , <EOL> None , <EOL> ) , <EOL> _LocalRunner ( ) , <EOL> processes = [ _pid_1_name ] <EOL> ) <EOL> d = metric . measure ( lambda : clock . advance ( <NUM_LIT:5> ) ) <EOL> def remove_process_times ( node_cpu_times ) : <EOL> for process_times in node_cpu_times . values ( ) : <EOL> if process_times : <EOL> for process in process_times : <EOL> if process != WALLCLOCK_LABEL : <EOL> process_times [ process ] = <NUM_LIT:0> <EOL> return node_cpu_times <EOL> d . addCallback ( remove_process_times ) <EOL> def check ( result ) : <EOL> self . assertEqual ( <EOL> result , <EOL> { <EOL> '<STR_LIT>' : { _pid_1_name : <NUM_LIT:0> , WALLCLOCK_LABEL : <NUM_LIT:5> } , <EOL> '<STR_LIT>' : { _pid_1_name : <NUM_LIT:0> , WALLCLOCK_LABEL : <NUM_LIT:5> } <EOL> } <EOL> ) <EOL> d . addCallback ( check ) <EOL> return d </s>
