@@ -887,10 +887,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
887
887
. map ( |t| t. desc . name . as_slice ( ) . len ( ) )
888
888
. unwrap_or ( 0 ) ;
889
889
890
- let is_multithreaded = match opts. test_threads {
891
- Some ( n) => n > 1 ,
892
- None => get_concurrency ( ) > 1 ,
893
- } ;
890
+ let is_multithreaded = opts. test_threads . unwrap_or_else ( get_concurrency) > 1 ;
894
891
895
892
let mut out: Box < OutputFormatter > = match opts. format {
896
893
OutputFormat :: Pretty => Box :: new ( PrettyFormatter :: new (
@@ -1014,6 +1011,15 @@ pub enum TestEvent {
1014
1011
1015
1012
pub type MonitorMsg = ( TestDesc , TestResult , Vec < u8 > ) ;
1016
1013
1014
+ struct Sink ( Arc < Mutex < Vec < u8 > > > ) ;
1015
+ impl Write for Sink {
1016
+ fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
1017
+ Write :: write ( & mut * self . 0 . lock ( ) . unwrap ( ) , data)
1018
+ }
1019
+ fn flush ( & mut self ) -> io:: Result < ( ) > {
1020
+ Ok ( ( ) )
1021
+ }
1022
+ }
1017
1023
1018
1024
pub fn run_tests < F > ( opts : & TestOpts , tests : Vec < TestDescAndFn > , mut callback : F ) -> io:: Result < ( ) >
1019
1025
where
@@ -1051,10 +1057,7 @@ where
1051
1057
_ => false ,
1052
1058
} ) ;
1053
1059
1054
- let concurrency = match opts. test_threads {
1055
- Some ( n) => n,
1056
- None => get_concurrency ( ) ,
1057
- } ;
1060
+ let concurrency = opts. test_threads . unwrap_or_else ( get_concurrency) ;
1058
1061
1059
1062
let mut remaining = filtered_tests;
1060
1063
remaining. reverse ( ) ;
@@ -1384,16 +1387,6 @@ pub fn run_test(
1384
1387
monitor_ch : Sender < MonitorMsg > ,
1385
1388
nocapture : bool ,
1386
1389
testfn : Box < FnBox ( ) + Send > ) {
1387
- struct Sink ( Arc < Mutex < Vec < u8 > > > ) ;
1388
- impl Write for Sink {
1389
- fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
1390
- Write :: write ( & mut * self . 0 . lock ( ) . unwrap ( ) , data)
1391
- }
1392
- fn flush ( & mut self ) -> io:: Result < ( ) > {
1393
- Ok ( ( ) )
1394
- }
1395
- }
1396
-
1397
1390
// Buffer for capturing standard I/O
1398
1391
let data = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
1399
1392
let data2 = data. clone ( ) ;
@@ -1438,24 +1431,27 @@ pub fn run_test(
1438
1431
1439
1432
match testfn {
1440
1433
DynBenchFn ( bencher) => {
1441
- let bs = :: bench:: benchmark ( |harness| bencher. run ( harness) ) ;
1442
- monitor_ch. send ( ( desc, TrBench ( bs) , Vec :: new ( ) ) ) . unwrap ( ) ;
1443
- return ;
1434
+ :: bench:: benchmark ( desc,
1435
+ monitor_ch,
1436
+ opts. nocapture ,
1437
+ |harness| bencher. run ( harness) ) ;
1444
1438
}
1445
1439
StaticBenchFn ( benchfn) => {
1446
- let bs = :: bench:: benchmark ( |harness| ( benchfn. clone ( ) ) ( harness) ) ;
1447
- monitor_ch. send ( ( desc, TrBench ( bs) , Vec :: new ( ) ) ) . unwrap ( ) ;
1448
- return ;
1440
+ :: bench:: benchmark ( desc,
1441
+ monitor_ch,
1442
+ opts. nocapture ,
1443
+ |harness| ( benchfn. clone ( ) ) ( harness) ) ;
1449
1444
}
1450
1445
DynTestFn ( f) => {
1451
1446
let cb = move || {
1452
1447
__rust_begin_short_backtrace ( f)
1453
1448
} ;
1454
1449
run_test_inner ( desc, monitor_ch, opts. nocapture , Box :: new ( cb) )
1455
1450
}
1456
- StaticTestFn ( f) =>
1451
+ StaticTestFn ( f) => {
1457
1452
run_test_inner ( desc, monitor_ch, opts. nocapture ,
1458
- Box :: new ( move || __rust_begin_short_backtrace ( f) ) ) ,
1453
+ Box :: new ( move || __rust_begin_short_backtrace ( f) ) )
1454
+ }
1459
1455
}
1460
1456
}
1461
1457
@@ -1655,11 +1651,14 @@ where
1655
1651
}
1656
1652
1657
1653
pub mod bench {
1654
+ use std:: panic:: { catch_unwind, AssertUnwindSafe } ;
1658
1655
use std:: cmp;
1656
+ use std:: io;
1657
+ use std:: sync:: { Arc , Mutex } ;
1659
1658
use stats;
1660
- use super :: { Bencher , BenchSamples , BenchMode } ;
1659
+ use super :: { Bencher , BenchSamples , BenchMode , Sink , MonitorMsg , TestDesc , Sender , TestResult } ;
1661
1660
1662
- pub fn benchmark < F > ( f : F ) -> BenchSamples
1661
+ pub fn benchmark < F > ( desc : TestDesc , monitor_ch : Sender < MonitorMsg > , nocapture : bool , f : F )
1663
1662
where
1664
1663
F : FnMut ( & mut Bencher ) ,
1665
1664
{
@@ -1669,26 +1668,53 @@ pub mod bench {
1669
1668
bytes : 0 ,
1670
1669
} ;
1671
1670
1672
- return match bs. bench ( f) {
1673
- Some ( ns_iter_summ) => {
1671
+ let data = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
1672
+ let data2 = data. clone ( ) ;
1673
+
1674
+ let oldio = if !nocapture {
1675
+ Some ( (
1676
+ io:: set_print ( Some ( Box :: new ( Sink ( data2. clone ( ) ) ) ) ) ,
1677
+ io:: set_panic ( Some ( Box :: new ( Sink ( data2) ) ) ) ,
1678
+ ) )
1679
+ } else {
1680
+ None
1681
+ } ;
1682
+
1683
+ let result = catch_unwind ( AssertUnwindSafe ( || bs. bench ( f) ) ) ;
1684
+
1685
+ if let Some ( ( printio, panicio) ) = oldio {
1686
+ io:: set_print ( printio) ;
1687
+ io:: set_panic ( panicio) ;
1688
+ } ;
1689
+
1690
+ let test_result = match result { //bs.bench(f) {
1691
+ Ok ( Some ( ns_iter_summ) ) => {
1674
1692
let ns_iter = cmp:: max ( ns_iter_summ. median as u64 , 1 ) ;
1675
1693
let mb_s = bs. bytes * 1000 / ns_iter;
1676
1694
1677
- BenchSamples {
1695
+ let bs = BenchSamples {
1678
1696
ns_iter_summ,
1679
1697
mb_s : mb_s as usize ,
1680
- }
1698
+ } ;
1699
+ TestResult :: TrBench ( bs)
1681
1700
}
1682
- None => {
1701
+ Ok ( None ) => {
1683
1702
// iter not called, so no data.
1684
1703
// FIXME: error in this case?
1685
1704
let samples: & mut [ f64 ] = & mut [ 0.0_f64 ; 1 ] ;
1686
- BenchSamples {
1705
+ let bs = BenchSamples {
1687
1706
ns_iter_summ : stats:: Summary :: new ( samples) ,
1688
1707
mb_s : 0 ,
1689
- }
1708
+ } ;
1709
+ TestResult :: TrBench ( bs)
1710
+ }
1711
+ Err ( _) => {
1712
+ TestResult :: TrFailed
1690
1713
}
1691
1714
} ;
1715
+
1716
+ let stdout = data. lock ( ) . unwrap ( ) . to_vec ( ) ;
1717
+ monitor_ch. send ( ( desc, test_result, stdout) ) . unwrap ( ) ;
1692
1718
}
1693
1719
1694
1720
pub fn run_once < F > ( f : F )
@@ -2067,14 +2093,42 @@ mod tests {
2067
2093
#[ test]
2068
2094
pub fn test_bench_no_iter ( ) {
2069
2095
fn f ( _: & mut Bencher ) { }
2070
- bench:: benchmark ( f) ;
2096
+
2097
+ let ( tx, rx) = channel ( ) ;
2098
+
2099
+ let desc = TestDesc {
2100
+ name : StaticTestName ( "f" ) ,
2101
+ ignore : false ,
2102
+ should_panic : ShouldPanic :: No ,
2103
+ allow_fail : false ,
2104
+ } ;
2105
+
2106
+ :: bench:: benchmark ( desc,
2107
+ tx,
2108
+ true ,
2109
+ f) ;
2110
+ rx. recv ( ) . unwrap ( ) ;
2071
2111
}
2072
2112
2073
2113
#[ test]
2074
2114
pub fn test_bench_iter ( ) {
2075
2115
fn f ( b : & mut Bencher ) {
2076
2116
b. iter ( || { } )
2077
2117
}
2078
- bench:: benchmark ( f) ;
2118
+
2119
+ let ( tx, rx) = channel ( ) ;
2120
+
2121
+ let desc = TestDesc {
2122
+ name : StaticTestName ( "f" ) ,
2123
+ ignore : false ,
2124
+ should_panic : ShouldPanic :: No ,
2125
+ allow_fail : false ,
2126
+ } ;
2127
+
2128
+ :: bench:: benchmark ( desc,
2129
+ tx,
2130
+ true ,
2131
+ f) ;
2132
+ rx. recv ( ) . unwrap ( ) ;
2079
2133
}
2080
2134
}
0 commit comments