package Ġorg . ro bo lectric . util Ċ Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġjava . io . IOException Ċ import Ġorg . junit . Assert Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ import Ġorg . ro bo lectric . plugin api . perf . Metric Ċ Ċ @ RunWith ( JUnit 4 :: class ) Ċ class ĠPer f Stats Collector Test Ġ{ ĊĠ Ġprivate Ġlateinit Ġvar Ġfake Clock : ĠFake Clock ĊĠ Ġprivate Ġlateinit Ġvar Ġcollector : ĠPer f Stats Collector ĊĊĠ Ġ@ Before ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġfake Clock Ġ= ĠFake Clock () ĊĠĠĠ Ġcollector Ġ= ĠPer f Stats Collector ( fake Clock ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Measure Elapsed Time For Events () Ġ{ ĊĠĠĠ Ġval Ġfirst Event Ġ= Ġcollector . start Event (" first Ġevent ") ĊĠĠĠ Ġfake Clock . delay ( 2 0 ) ĊĠĠĠ Ġfirst Event . finished () ĊĠĠĠ Ġval Ġmetrics Ġ= Ġcollector . metrics ĊĠĠĠ ĠassertThat ( metrics ). contains Exactly ( Metric (" first Ġevent ", Ġ 1 , Ġ 2 0 , Ġtrue )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Measure Elapsed Time For Repeated Events () Ġ{ ĊĠĠĠ Ġval Ġfirst Event Ġ= Ġcollector . start Event (" repeat able Ġevent ") ĊĠĠĠ Ġfake Clock . delay ( 2 0 ) ĊĠĠĠ Ġfirst Event . finished () ĊĠĠĠ Ġval Ġsecond Event Ġ= Ġcollector . start Event (" repeat able Ġevent ") ĊĠĠĠ Ġfake Clock . delay ( 2 0 ) ĊĠĠĠ Ġsecond Event . finished () ĊĠĠĠ Ġval Ġthird Event Ġ= Ġcollector . start Event (" repeat able Ġevent ") ĊĠĠĠ Ġfake Clock . delay ( 2 0 ) ĊĠĠĠ Ġthird Event . finished () ĊĠĠĠ Ġval Ġmetrics Ġ= Ġcollector . metrics ĊĠĠĠ ĠassertThat ( metrics ). contains Exactly ( Metric (" repeat able Ġevent ", Ġ 3 , Ġ 6 0 , Ġtrue )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Run And Measure Successful Callable () Ġ{ ĊĠĠĠ ĠassertThat ( ĊĠĠĠĠĠĠĠ Ġcollector . measure < String , ĠRuntimeException >(" event ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġfake Clock . delay ( 1 0 ) ĊĠĠĠĠĠĠĠĠĠ Ġ" return Ġvalue " ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ. isEqualTo (" return Ġvalue ") ĊĠĠĠ Ġval Ġmetrics Ġ= Ġcollector . metrics ĊĠĠĠ ĠassertThat ( metrics ). contains Exactly ( Metric (" event ", Ġ 1 , Ġ 1 0 , Ġtrue )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Run And Measure Exception Throw ing Callable () Ġ{ ĊĠĠĠ Ġcollector . measure < String , ĠRuntimeException >(" event ") Ġ{ ĊĠĠĠĠĠ Ġfake Clock . delay ( 1 0 ) ĊĠĠĠĠĠ Ġ" return Ġvalue " ĊĠĠĠ Ġ} ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġcollector . measure < Any , ĠRuntimeException >(" event ") Ġ{ ĊĠĠĠĠĠĠĠ Ġfake Clock . delay ( 5 ) ĊĠĠĠĠĠĠĠ Ġthrow ĠRuntimeException (" fake ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ ĠAssert . fail (" should Ġhave Ġthrown ") ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠRuntimeException ) Ġ{ ĊĠĠĠĠĠ ĠassertThat ( e . message ). isEqualTo (" fake ") ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġmetrics Ġ= Ġcollector . metrics ĊĠĠĠ ĠassertThat ( metrics ). contains At Least ( Metric (" event ", Ġ 1 , Ġ 1 0 , Ġtrue ), ĠMetric (" event ", Ġ 1 , Ġ 5 , Ġfalse )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġshould Run And Measure Checked Exception () Ġ{ ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġcollector . measure < Any , ĠIOException >(" event ") Ġ{ ĊĠĠĠĠĠĠĠ Ġfake Clock . delay ( 5 ) ĊĠĠĠĠĠĠĠ Ġthrow ĠIOException (" fake ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ ĠAssert . fail (" should Ġhave Ġthrown ") ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠIOException ) Ġ{ ĊĠĠĠĠĠ ĠassertThat ( e . message ). isEqualTo (" fake ") ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġmetrics Ġ= Ġcollector . metrics ĊĠĠĠ ĠassertThat ( metrics ). contains ( Metric (" event ", Ġ 1 , Ġ 5 , Ġfalse )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġreset _ should Clear All Metadata And Metrics () Ġ{ ĊĠĠĠ Ġcollector . put Metadata ( String :: class . java , Ġ" metadata ") ĊĠĠĠ Ġcollector . start Event (" event "). finished () ĊĠĠĠ Ġcollector . reset () ĊĠĠĠ ĠassertThat ( collector . metadata . get ( String :: class . java )). isNull () ĊĠĠĠ ĠassertThat ( collector . metrics ). isEmpty () ĊĠ Ġ} ĊĊĠ Ġprivate Ġclass ĠFake Clock Ġ: ĠClock Ġ{ ĊĠĠĠ Ġprivate Ġvar Ġtime Ns Ġ= Ġ 0 ĊĠĠĠ Ġoverride Ġfun Ġnano Time (): ĠLong Ġ{ ĊĠĠĠĠĠ Ġreturn Ġtime Ns . to Long () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġdelay ( ms : ĠInt ) Ġ{ ĊĠĠĠĠĠ Ġtime Ns Ġ+= Ġms ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ