Skip to content

Commit a800e3e

Browse files
committed
Finalize story for libtest
One of the last remaining crates to have a stabilization story in the standard distribution is the libtest library. This library currently provides the backing infrastructure and test harness used when rustc generates a test execuable via the `--test` command line flag. It's well known that libtest is not the end-all-be-all of testing frameworks. It is intentionally minimal and is currently quite conservative in its scope of what it tries to accomplish as well as what it implements. A testament to this is the fact that one very rarely writes `extern crate test`, and it almost means that the stabilization story need not be considered for the crate at all! The benchmarking feature of the compiler, however, is quite useful and is one of the sole reasons for using `extern crate test`. When benchmarking, there are a few primary interfaces to the libtest library that are used: * `test::Bencher` - the type for a benchmarking harness. * `test::Bencher::iter` - a member function used to run a benchmark. * `test::black_box` - a useful function to hinder optimizations and prevent a value from being optimized away. These three pieces of information are the primary targets for the stabilization in this commit. The rest of the testing infrastructure, while still quite useful to some projects, is not in scope for stabilization at 1.0 and will remain `#[experimental]` for now. The benchmarking pieces have been moved to a new `rustc_bench` crate which will be part of the standard distribution. In order to write a benchmark one will need to import the crate via `extern crate rustc_bench` and otherwise all usage remains the same. The purpose of this crate is to provide a clear area for these benchmarking utilities as well as provide a clear name that it is *only* intended for use via the compiler `#[bench]` attribute. The current interface is quite minimal with only what's necessary as `#[stable]`. It is most certainly a desire for the compiler to support other testing frameworks other than the standard libtest. This form of infrastructure (be it a plugin or a separate interface) is out of scope for 1.0, however, and this commit does not attempt to resolve it. Due to the removal of benchmarking items from libtest, this is a breaking change. To update, rewrite imports of `extern crate test` to `extern crate rustc_bench` and then rewrite all `use` statements as well. The public interface of the `Bencher` struct has not changed. [breaking-change]
1 parent 9e4e524 commit a800e3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+239
-201
lines changed

mk/crates.mk

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
TARGET_CRATES := libc std flate arena term \
5353
serialize getopts collections test rand \
5454
log regex graphviz core rbml alloc \
55-
unicode
55+
unicode rustc_bench
5656
RUSTC_CRATES := rustc rustc_typeck rustc_borrowck rustc_resolve rustc_driver \
5757
rustc_trans rustc_back rustc_llvm
5858
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
@@ -90,11 +90,13 @@ DEPS_term := std log
9090
DEPS_getopts := std
9191
DEPS_collections := core alloc unicode
9292
DEPS_num := std
93-
DEPS_test := std getopts serialize rbml term regex native:rust_test_helpers
93+
DEPS_test := std getopts serialize rbml term regex native:rust_test_helpers \
94+
rustc_bench
9495
DEPS_rand := core
9596
DEPS_log := std regex
9697
DEPS_regex := std
9798
DEPS_fmt_macros = std
99+
DEPS_rustc_bench := std
98100

99101
TOOL_DEPS_compiletest := test getopts
100102
TOOL_DEPS_rustdoc := rustdoc

src/doc/guide-testing.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
% The Rust Testing Guide
22

33
> Program testing can be a very effective way to show the presence of bugs, but
4-
> it is hopelessly inadequate for showing their absence.
4+
> it is hopelessly inadequate for showing their absence.
55
>
66
> Edsger W. Dijkstra, "The Humble Programmer" (1972)
77
@@ -310,7 +310,7 @@ extern crate adder;
310310
#[test]
311311
fn it_works() {
312312
assert_eq(4, adder::add_two(2));
313-
}
313+
}
314314
```
315315

316316
This looks similar to our previous tests, but slightly different. We now have
@@ -442,7 +442,7 @@ code. Let's make our `src/lib.rs` look like this (comments elided):
442442
```{rust,ignore}
443443
#![feature(globs)]
444444
445-
extern crate test;
445+
extern crate rustc_bench;
446446
447447
pub fn add_two(a: i32) -> i32 {
448448
a + 2
@@ -451,7 +451,7 @@ pub fn add_two(a: i32) -> i32 {
451451
#[cfg(test)]
452452
mod tests {
453453
use super::*;
454-
use test::Bencher;
454+
use rustc_bench::Bencher;
455455
456456
#[test]
457457
fn it_works() {
@@ -512,8 +512,8 @@ compiler might recognize that some calculation has no external effects and
512512
remove it entirely.
513513

514514
```{rust,ignore}
515-
extern crate test;
516-
use test::Bencher;
515+
extern crate rustc_bench;
516+
use rustc_bench::Bencher;
517517
518518
#[bench]
519519
fn bench_xor_1000_ints(b: &mut Bencher) {
@@ -547,20 +547,19 @@ b.iter(|| {
547547
});
548548
```
549549

550-
Or, the other option is to call the generic `test::black_box` function, which
551-
is an opaque "black box" to the optimizer and so forces it to consider any
552-
argument as used.
550+
Or, the other option is to call the generic `rustc_bench::black_box` function,
551+
which is an opaque "black box" to the optimizer and so forces it to consider
552+
any argument as used.
553553

554554
```rust
555-
extern crate test;
556-
555+
extern crate rustc_bench;
557556
# fn main() {
558557
# struct X;
559558
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
560559
b.iter(|| {
561560
let mut n = 1000_u32;
562561

563-
test::black_box(&mut n); // pretend to modify `n`
562+
rustc_bench::black_box(&mut n); // pretend to modify `n`
564563

565564
range(0, n).fold(0, |a, b| a ^ b)
566565
})

src/liballoc/heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ mod imp {
370370

371371
#[cfg(test)]
372372
mod test {
373-
extern crate test;
374-
use self::test::Bencher;
373+
extern crate rustc_bench;
374+
use self::rustc_bench::Bencher;
375375
use core::ptr::PtrExt;
376376
use heap;
377377

src/libarena/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ impl<T> Drop for TypedArena<T> {
507507

508508
#[cfg(test)]
509509
mod tests {
510-
extern crate test;
511-
use self::test::Bencher;
510+
extern crate rustc_bench;
511+
use self::rustc_bench::Bencher;
512512
use super::{Arena, TypedArena};
513513

514514
#[allow(dead_code)]

src/libcollections/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use prelude::*;
1212
use std::rand;
1313
use std::rand::Rng;
14-
use test::{Bencher, black_box};
14+
use rustc_bench::{Bencher, black_box};
1515

1616
pub fn insert_rand_n<M, I, R>(n: uint,
1717
map: &mut M,

src/libcollections/bit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ mod bitv_bench {
25072507
use std::rand;
25082508
use std::rand::Rng;
25092509
use std::u32;
2510-
use test::{Bencher, black_box};
2510+
use rustc_bench::{Bencher, black_box};
25112511

25122512
use super::Bitv;
25132513

@@ -2526,7 +2526,7 @@ mod bitv_bench {
25262526
for _ in range(0u, 100) {
25272527
bitv |= 1 << ((r.next_u32() as uint) % u32::BITS);
25282528
}
2529-
black_box(&bitv)
2529+
black_box(&bitv);
25302530
});
25312531
}
25322532

@@ -2538,7 +2538,7 @@ mod bitv_bench {
25382538
for _ in range(0u, 100) {
25392539
bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
25402540
}
2541-
black_box(&bitv)
2541+
black_box(&bitv);
25422542
});
25432543
}
25442544

@@ -3002,7 +3002,7 @@ mod bitv_set_bench {
30023002
use std::rand;
30033003
use std::rand::Rng;
30043004
use std::u32;
3005-
use test::{Bencher, black_box};
3005+
use rustc_bench::{Bencher, black_box};
30063006

30073007
use super::{Bitv, BitvSet};
30083008

src/libcollections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ mod test {
16141614
mod bench {
16151615
use prelude::*;
16161616
use std::rand::{weak_rng, Rng};
1617-
use test::{Bencher, black_box};
1617+
use rustc_bench::{Bencher, black_box};
16181618

16191619
use super::BTreeMap;
16201620
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};

src/libcollections/dlist.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,7 @@ mod tests {
690690
use std::rand;
691691
use std::hash;
692692
use std::thread::Thread;
693-
use test::Bencher;
694-
use test;
693+
use rustc_bench::Bencher;
695694

696695
use super::{DList, Node};
697696

@@ -1101,31 +1100,31 @@ mod tests {
11011100
}
11021101

11031102
#[bench]
1104-
fn bench_collect_into(b: &mut test::Bencher) {
1103+
fn bench_collect_into(b: &mut Bencher) {
11051104
let v = &[0i; 64];
11061105
b.iter(|| {
11071106
let _: DList<int> = v.iter().map(|x| *x).collect();
11081107
})
11091108
}
11101109

11111110
#[bench]
1112-
fn bench_push_front(b: &mut test::Bencher) {
1111+
fn bench_push_front(b: &mut Bencher) {
11131112
let mut m: DList<int> = DList::new();
11141113
b.iter(|| {
11151114
m.push_front(0);
11161115
})
11171116
}
11181117

11191118
#[bench]
1120-
fn bench_push_back(b: &mut test::Bencher) {
1119+
fn bench_push_back(b: &mut Bencher) {
11211120
let mut m: DList<int> = DList::new();
11221121
b.iter(|| {
11231122
m.push_back(0);
11241123
})
11251124
}
11261125

11271126
#[bench]
1128-
fn bench_push_back_pop_back(b: &mut test::Bencher) {
1127+
fn bench_push_back_pop_back(b: &mut Bencher) {
11291128
let mut m: DList<int> = DList::new();
11301129
b.iter(|| {
11311130
m.push_back(0);
@@ -1134,7 +1133,7 @@ mod tests {
11341133
}
11351134

11361135
#[bench]
1137-
fn bench_push_front_pop_front(b: &mut test::Bencher) {
1136+
fn bench_push_front_pop_front(b: &mut Bencher) {
11381137
let mut m: DList<int> = DList::new();
11391138
b.iter(|| {
11401139
m.push_front(0);
@@ -1143,31 +1142,31 @@ mod tests {
11431142
}
11441143

11451144
#[bench]
1146-
fn bench_iter(b: &mut test::Bencher) {
1145+
fn bench_iter(b: &mut Bencher) {
11471146
let v = &[0i; 128];
11481147
let m: DList<int> = v.iter().map(|&x|x).collect();
11491148
b.iter(|| {
11501149
assert!(m.iter().count() == 128);
11511150
})
11521151
}
11531152
#[bench]
1154-
fn bench_iter_mut(b: &mut test::Bencher) {
1153+
fn bench_iter_mut(b: &mut Bencher) {
11551154
let v = &[0i; 128];
11561155
let mut m: DList<int> = v.iter().map(|&x|x).collect();
11571156
b.iter(|| {
11581157
assert!(m.iter_mut().count() == 128);
11591158
})
11601159
}
11611160
#[bench]
1162-
fn bench_iter_rev(b: &mut test::Bencher) {
1161+
fn bench_iter_rev(b: &mut Bencher) {
11631162
let v = &[0i; 128];
11641163
let m: DList<int> = v.iter().map(|&x|x).collect();
11651164
b.iter(|| {
11661165
assert!(m.iter().rev().count() == 128);
11671166
})
11681167
}
11691168
#[bench]
1170-
fn bench_iter_mut_rev(b: &mut test::Bencher) {
1169+
fn bench_iter_mut_rev(b: &mut Bencher) {
11711170
let v = &[0i; 128];
11721171
let mut m: DList<int> = v.iter().map(|&x|x).collect();
11731172
b.iter(|| {

src/libcollections/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern crate core;
3333
extern crate unicode;
3434
extern crate alloc;
3535

36-
#[cfg(test)] extern crate test;
36+
#[cfg(test)] extern crate rustc_bench;
3737
#[cfg(test)] #[macro_use] extern crate std;
3838
#[cfg(test)] #[macro_use] extern crate log;
3939

src/libcollections/ring_buf.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,7 @@ mod tests {
16321632
use core::iter;
16331633
use std::fmt::Show;
16341634
use std::hash;
1635-
use test::Bencher;
1636-
use test;
1635+
use rustc_bench::{Bencher, black_box};
16371636

16381637
use super::RingBuf;
16391638

@@ -1750,15 +1749,15 @@ mod tests {
17501749
}
17511750

17521751
#[bench]
1753-
fn bench_new(b: &mut test::Bencher) {
1752+
fn bench_new(b: &mut Bencher) {
17541753
b.iter(|| {
17551754
let ring: RingBuf<u64> = RingBuf::new();
1756-
test::black_box(ring);
1755+
black_box(ring);
17571756
})
17581757
}
17591758

17601759
#[bench]
1761-
fn bench_push_back_100(b: &mut test::Bencher) {
1760+
fn bench_push_back_100(b: &mut Bencher) {
17621761
let mut deq = RingBuf::with_capacity(101);
17631762
b.iter(|| {
17641763
for i in range(0i, 100) {
@@ -1770,7 +1769,7 @@ mod tests {
17701769
}
17711770

17721771
#[bench]
1773-
fn bench_push_front_100(b: &mut test::Bencher) {
1772+
fn bench_push_front_100(b: &mut Bencher) {
17741773
let mut deq = RingBuf::with_capacity(101);
17751774
b.iter(|| {
17761775
for i in range(0i, 100) {
@@ -1782,65 +1781,65 @@ mod tests {
17821781
}
17831782

17841783
#[bench]
1785-
fn bench_pop_back_100(b: &mut test::Bencher) {
1784+
fn bench_pop_back_100(b: &mut Bencher) {
17861785
let mut deq: RingBuf<int> = RingBuf::with_capacity(101);
17871786

17881787
b.iter(|| {
17891788
deq.head = 100;
17901789
deq.tail = 0;
17911790
while !deq.is_empty() {
1792-
test::black_box(deq.pop_back());
1791+
black_box(deq.pop_back());
17931792
}
17941793
})
17951794
}
17961795

17971796
#[bench]
1798-
fn bench_pop_front_100(b: &mut test::Bencher) {
1797+
fn bench_pop_front_100(b: &mut Bencher) {
17991798
let mut deq: RingBuf<int> = RingBuf::with_capacity(101);
18001799

18011800
b.iter(|| {
18021801
deq.head = 100;
18031802
deq.tail = 0;
18041803
while !deq.is_empty() {
1805-
test::black_box(deq.pop_front());
1804+
black_box(deq.pop_front());
18061805
}
18071806
})
18081807
}
18091808

18101809
#[bench]
1811-
fn bench_grow_1025(b: &mut test::Bencher) {
1810+
fn bench_grow_1025(b: &mut Bencher) {
18121811
b.iter(|| {
18131812
let mut deq = RingBuf::new();
18141813
for i in range(0i, 1025) {
18151814
deq.push_front(i);
18161815
}
1817-
test::black_box(deq);
1816+
black_box(deq);
18181817
})
18191818
}
18201819

18211820
#[bench]
1822-
fn bench_iter_1000(b: &mut test::Bencher) {
1821+
fn bench_iter_1000(b: &mut Bencher) {
18231822
let ring: RingBuf<int> = range(0i, 1000).collect();
18241823

18251824
b.iter(|| {
18261825
let mut sum = 0;
18271826
for &i in ring.iter() {
18281827
sum += i;
18291828
}
1830-
test::black_box(sum);
1829+
black_box(sum);
18311830
})
18321831
}
18331832

18341833
#[bench]
1835-
fn bench_mut_iter_1000(b: &mut test::Bencher) {
1834+
fn bench_mut_iter_1000(b: &mut Bencher) {
18361835
let mut ring: RingBuf<int> = range(0i, 1000).collect();
18371836

18381837
b.iter(|| {
18391838
let mut sum = 0;
18401839
for i in ring.iter_mut() {
18411840
sum += *i;
18421841
}
1843-
test::black_box(sum);
1842+
black_box(sum);
18441843
})
18451844
}
18461845

src/libcollections/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2730,7 +2730,7 @@ mod bench {
27302730
use core::ptr;
27312731
use core::iter::repeat;
27322732
use std::rand::{weak_rng, Rng};
2733-
use test::{Bencher, black_box};
2733+
use rustc_bench::{Bencher, black_box};
27342734

27352735
#[bench]
27362736
fn iterator(b: &mut Bencher) {

0 commit comments

Comments
 (0)