本文整理汇总了Golang中testing.AllocsPerRun函数的典型用法代码示例。如果您正苦于以下问题:Golang AllocsPerRun函数的具体用法?Golang AllocsPerRun怎么用?Golang AllocsPerRun使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AllocsPerRun函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCountDecodeMallocs
func TestCountDecodeMallocs(t *testing.T) {
if testing.Short() {
t.Skip("skipping malloc count in short mode")
}
if runtime.GOMAXPROCS(0) > 1 {
t.Skip("skipping; GOMAXPROCS>1")
}
const N = 1000
var buf bytes.Buffer
enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
// Fill the buffer with enough to decode
testing.AllocsPerRun(N, func() {
err := enc.Encode(bench)
if err != nil {
t.Fatal("encode:", err)
}
})
dec := NewDecoder(&buf)
allocs := testing.AllocsPerRun(N, func() {
*bench = Bench{}
err := dec.Decode(&bench)
if err != nil {
t.Fatal("decode:", err)
}
})
if allocs != 3 {
t.Fatalf("mallocs per decode of type Bench: %v; wanted 3\n", allocs)
}
}
示例2: TestAlloc
// TestAlloc tests that some mapping methods should not cause any allocation.
func TestAlloc(t *testing.T) {
dst := make([]byte, 256) // big enough to hold any result
src := []byte(txtNonASCII)
for i, f := range []func() Caser{
func() Caser { return Upper(language.Und) },
func() Caser { return Lower(language.Und) },
func() Caser { return Title(language.Und) },
} {
var c Caser
v := testing.AllocsPerRun(2, func() {
c = f()
})
if v > 1 {
// TODO: Right now only Upper has 1 allocation. Special-case Lower
// and Title as well to have less allocations for the root locale.
t.Skipf("%d:init: number of allocs was %f; want 0", i, v)
}
v = testing.AllocsPerRun(2, func() {
c.Transform(dst, src, true)
})
if v > 0 {
t.Errorf("%d:transform: number of allocs was %f; want 0", i, v)
}
}
}
示例3: TestCountDecodeMallocs
func TestCountDecodeMallocs(t *testing.T) {
const N = 1000
var buf bytes.Buffer
enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
// Fill the buffer with enough to decode
testing.AllocsPerRun(N, func() {
err := enc.Encode(bench)
if err != nil {
t.Fatal("encode:", err)
}
})
dec := NewDecoder(&buf)
allocs := testing.AllocsPerRun(N, func() {
*bench = Bench{}
err := dec.Decode(&bench)
if err != nil {
t.Fatal("decode:", err)
}
})
fmt.Printf("mallocs per decode of type Bench: %v\n", allocs)
}
示例4: TestNumericWeighterAlloc
func TestNumericWeighterAlloc(t *testing.T) {
buf := make([]Elem, 100)
w := NewNumericWeighter(numWeighter)
s := "1234567890a"
nNormal := testing.AllocsPerRun(3, func() { numWeighter.AppendNextString(buf, s) })
nNumeric := testing.AllocsPerRun(3, func() { w.AppendNextString(buf, s) })
if n := nNumeric - nNormal; n > 0 {
t.Errorf("got %f; want 0", n)
}
}
示例5: TestRawBytesAllocs
// Tests that assigning to RawBytes doesn't allocate (and also works).
func TestRawBytesAllocs(t *testing.T) {
buf := make(RawBytes, 10)
test := func(name string, in interface{}, want string) {
if err := convertAssign(&buf, in); err != nil {
t.Fatalf("%s: convertAssign = %v", name, err)
}
match := len(buf) == len(want)
if match {
for i, b := range buf {
if want[i] != b {
match = false
break
}
}
}
if !match {
t.Fatalf("%s: got %q (len %d); want %q (len %d)", name, buf, len(buf), want, len(want))
}
}
n := testing.AllocsPerRun(100, func() {
test("uint64", uint64(12345678), "12345678")
test("uint32", uint32(1234), "1234")
test("uint16", uint16(12), "12")
test("uint8", uint8(1), "1")
test("uint", uint(123), "123")
test("int", int(123), "123")
test("int8", int8(1), "1")
test("int16", int16(12), "12")
test("int32", int32(1234), "1234")
test("int64", int64(12345678), "12345678")
test("float32", float32(1.5), "1.5")
test("float64", float64(64), "64")
test("bool", false, "false")
})
// The numbers below are only valid for 64-bit interface word sizes,
// and gc. With 32-bit words there are more convT2E allocs, and
// with gccgo, only pointers currently go in interface data.
// So only care on amd64 gc for now.
measureAllocs := runtime.GOARCH == "amd64" && runtime.Compiler == "gc"
if n > 0.5 && measureAllocs {
t.Fatalf("allocs = %v; want 0", n)
}
// This one involves a convT2E allocation, string -> interface{}
n = testing.AllocsPerRun(100, func() {
test("string", "foo", "foo")
})
if n > 1.5 && measureAllocs {
t.Fatalf("allocs = %v; want max 1", n)
}
}
示例6: main
func main() {
nf := testing.AllocsPerRun(100, F)
ng := testing.AllocsPerRun(100, G)
if int(nf) != 1 {
fmt.Printf("AllocsPerRun(100, F) = %v, want 1\n", nf)
os.Exit(1)
}
if int(ng) != 0 {
fmt.Printf("AllocsPerRun(100, G) = %v, want 0\n", ng)
os.Exit(1)
}
}
示例7: main
func main() {
nf := testing.AllocsPerRun(100, F)
ng := testing.AllocsPerRun(100, G)
if int(nf) > 1 {
fmt.Printf("AllocsPerRun(100, F) = %v, want 1\n", nf)
os.Exit(1)
}
if int(ng) != 0 && (runtime.Compiler != "gccgo" || int(ng) != 1) {
fmt.Printf("AllocsPerRun(100, G) = %v, want 0\n", ng)
os.Exit(1)
}
}
示例8: TestString_ZeroAllocs
func TestString_ZeroAllocs(t *testing.T) {
v := "jumped over the lazy dog"
b := make([]byte, len(v)+1)
assert.Zero(t, testing.AllocsPerRun(1, func() { PutString(b, v) }))
assert.Zero(t, testing.AllocsPerRun(1, func() { String(b) }))
//Note that while the String function requires zero allocations,
//the decoded string can still escape...
var x string
assert.Equal(t, 1.0, testing.AllocsPerRun(1, func() {
s := String(b)
x = s
}))
assert.Equal(t, v, x)
}
示例9: TestChunkReaderAllocs
func TestChunkReaderAllocs(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
var buf bytes.Buffer
w := NewChunkedWriter(&buf)
a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
w.Write(a)
w.Write(b)
w.Write(c)
w.Close()
readBuf := make([]byte, len(a)+len(b)+len(c)+1)
byter := bytes.NewReader(buf.Bytes())
bufr := bufio.NewReader(byter)
mallocs := testing.AllocsPerRun(100, func() {
byter.Seek(0, io.SeekStart)
bufr.Reset(byter)
r := NewChunkedReader(bufr)
n, err := io.ReadFull(r, readBuf)
if n != len(readBuf)-1 {
t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
}
if err != io.ErrUnexpectedEOF {
t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
}
})
if mallocs > 1.5 {
t.Errorf("mallocs = %v; want 1", mallocs)
}
}
示例10: TestClean
func TestClean(t *testing.T) {
for _, test := range cleantests {
if s := Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
}
if s := Clean(test.result); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
}
}
if runtime.GOMAXPROCS(0) > 1 {
t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
return
}
t.Log("Skipping AllocsPerRun for gccgo")
return
for _, test := range cleantests {
allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })
if allocs > 0 {
t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
}
}
}
示例11: TestClean
func TestClean(t *testing.T) {
tests := cleantests
if runtime.GOOS == "windows" {
for i := range tests {
tests[i].result = filepath.FromSlash(tests[i].result)
}
tests = append(tests, wincleantests...)
}
for _, test := range tests {
if s := filepath.Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
}
if s := filepath.Clean(test.result); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
}
}
if testing.Short() {
t.Skip("skipping malloc count in short mode")
}
if runtime.GOMAXPROCS(0) > 1 {
t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
return
}
for _, test := range tests {
allocs := testing.AllocsPerRun(100, func() { filepath.Clean(test.result) })
if allocs > 0 {
t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
}
}
}
示例12: TestAllocsPerRun
func TestAllocsPerRun(t *testing.T) {
for _, tt := range allocsPerRunTests {
if allocs := testing.AllocsPerRun(100, tt.fn); allocs != tt.allocs {
t.Errorf("AllocsPerRun(100, %s) = %v, want %v", tt.name, allocs, tt.allocs)
}
}
}
示例13: TestReplaceIllFormedAlloc
func TestReplaceIllFormedAlloc(t *testing.T) {
if n := testing.AllocsPerRun(3, func() {
ReplaceIllFormed().Transform(nil, nil, false)
}); n > 0 {
t.Errorf("got %f; want 0", n)
}
}
示例14: TestGarbageCreation
func TestGarbageCreation(t *testing.T) {
// TestMultipleResultsQuery needs to work as expected
dependOn(t, TestMultipleResultsQuery)
// Test intervals
intervals := IntervalSlice{
{4, 15, "First"},
{50, 72, "Second"},
{34, 90, "Third"},
{34, 45, "Fourth"},
{34, 40, "Fifth"},
{34, 34, "Sixth"},
{34, 45, "Seventh"},
}
stab := setup(t, intervals)
allocs := testing.AllocsPerRun(1000, func() {
results, err := stab.Intersect(42)
if err != nil {
t.Fatal("Error during alloc run: ", err)
}
if results == nil {
t.Fatal("Got 'nil' results during alloc run")
}
})
t.Log("Allocs per run (avg): ", allocs)
if allocs > 2.1 {
t.Fatal("Too many allocs, be sure to disable logging for real builds")
}
}
示例15: TestMapAlloc
func TestMapAlloc(t *testing.T) {
if n := testing.AllocsPerRun(3, func() {
Map(idem).Transform(nil, nil, false)
}); n > 0 {
t.Errorf("got %f; want 0", n)
}
}