本文整理汇总了Golang中testing.B.Fatalf方法的典型用法代码示例。如果您正苦于以下问题:Golang B.Fatalf方法的具体用法?Golang B.Fatalf怎么用?Golang B.Fatalf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类testing.B
的用法示例。
在下文中一共展示了B.Fatalf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkCheckRetryClosesBody
func BenchmarkCheckRetryClosesBody(t *testing.B) {
count := 0
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
count++
if count%3 == 0 {
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Retry-After", "0")
w.WriteHeader(apierrors.StatusTooManyRequests)
}))
defer testServer.Close()
c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "user", Password: "pass"})
r := c.Verb("POST").
Prefix("foo", "bar").
Suffix("baz").
Timeout(time.Second).
Body([]byte(strings.Repeat("abcd", 1000)))
for i := 0; i < t.N; i++ {
if _, err := r.DoRaw(); err != nil {
t.Fatalf("Unexpected error: %v %#v", err, err)
}
}
}
示例2: BenchmarkServerHijack
func BenchmarkServerHijack(b *testing.B) {
clientsCount := 1000
requestsPerConn := 10000
ch := make(chan struct{}, b.N)
responseBody := []byte("123")
s := &Server{
Handler: func(ctx *RequestCtx) {
ctx.Hijack(func(c net.Conn) {
// emulate server loop :)
err := ServeConn(c, func(ctx *RequestCtx) {
ctx.Success("foobar", responseBody)
registerServedRequest(b, ch)
})
if err != nil {
b.Fatalf("error when serving connection")
}
})
ctx.Success("foobar", responseBody)
registerServedRequest(b, ch)
},
Concurrency: 16 * clientsCount,
}
req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n"
benchmarkServer(b, s, clientsCount, requestsPerConn, req)
verifyRequestsServed(b, ch)
}
示例3: runPutObjectBenchmark
// Benchmark utility functions for ObjectLayer.PutObject().
// Creates Object layer setup ( MakeBucket ) and then runs the PutObject benchmark.
func runPutObjectBenchmark(b *testing.B, obj ObjectLayer, objSize int) {
var err error
// obtains random bucket name.
bucket := getRandomBucketName()
// create bucket.
err = obj.MakeBucket(bucket)
if err != nil {
b.Fatal(err)
}
// get text data generated for number of bytes equal to object size.
textData := generateBytesData(objSize)
// generate md5sum for the generated data.
// md5sum of the data to written is required as input for PutObject.
metadata := make(map[string]string)
metadata["md5Sum"] = getMD5Hash(textData)
sha256sum := ""
// benchmark utility which helps obtain number of allocations and bytes allocated per ops.
b.ReportAllocs()
// the actual benchmark for PutObject starts here. Reset the benchmark timer.
b.ResetTimer()
for i := 0; i < b.N; i++ {
// insert the object.
objInfo, err := obj.PutObject(bucket, "object"+strconv.Itoa(i), int64(len(textData)), bytes.NewBuffer(textData), metadata, sha256sum)
if err != nil {
b.Fatal(err)
}
if objInfo.MD5Sum != metadata["md5Sum"] {
b.Fatalf("Write no: %d: Md5Sum mismatch during object write into the bucket: Expected %s, got %s", i+1, objInfo.MD5Sum, metadata["md5Sum"])
}
}
// Benchmark ends here. Stop timer.
b.StopTimer()
}
示例4: BenchmarkPublishJsonStruct
func BenchmarkPublishJsonStruct(b *testing.B) {
// stop benchmark for set-up
b.StopTimer()
s := test.RunDefaultServer()
defer s.Shutdown()
ec := NewJsonEncodedConn(b)
defer ec.Close()
ch := make(chan bool)
me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"}
me.Children = make(map[string]*person)
me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"}
me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"}
ec.Subscribe("json_struct", func(p *person) {
if !reflect.DeepEqual(p, me) {
b.Fatalf("Did not receive the correct struct response")
}
ch <- true
})
// resume benchmark
b.StartTimer()
for n := 0; n < b.N; n++ {
ec.Publish("json_struct", me)
if e := test.Wait(ch); e != nil {
b.Fatal("Did not receive the message")
}
}
}
示例5: BenchmarkDecodeNotFound
// BenchmarkDecodeNotFound performs a benchmark on how long it takes to decode
// a notfound message with the maximum number of entries.
func BenchmarkDecodeNotFound(b *testing.B) {
// Create a message with the maximum number of entries.
pver := ProtocolVersion
var m MsgNotFound
for i := 0; i < MaxInvPerMsg; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.AddInvVect(NewInvVect(InvTypeBlock, hash))
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
b.Fatalf("MsgNotFound.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgNotFound
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver, LatestEncoding)
}
}
示例6: BenchmarkWriteBoom
func BenchmarkWriteBoom(b *testing.B) {
if *file == "" {
b.Skip("no file specified")
}
b.StopTimer()
br, err := boom.OpenBAM(*file)
if err != nil {
b.Fatalf("Open failed: %v", err)
}
f, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
b.Fatalf("Open failed: %v", err)
}
bw, err := boom.OpenBAMFile(f, "bw", br.Header())
if err != nil {
b.Fatalf("NewWriter failed: %v", err)
}
r, _, err := br.Read()
if err != nil {
b.Fatalf("Read failed: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err = bw.Write(r)
if err != nil {
b.Fatalf("Write failed: %v", err)
}
}
br.Close()
f.Close()
}
示例7: BenchmarkFilterLines
func BenchmarkFilterLines(b *testing.B) {
pattern := "sdk/*/cpp/*/*vars.html"
lines := extractTestLines(b)
var c uint
b.ResetTimer()
for i := 0; i < b.N; i++ {
c = 0
for _, line := range lines {
match, err := filter.Match(pattern, line)
if err != nil {
b.Fatal(err)
}
if match {
c++
}
}
if c != 3 {
b.Fatalf("wrong number of matches: expected 3, got %d", c)
}
}
}
示例8: BenchmarkReceiveRequestNoBuf
func BenchmarkReceiveRequestNoBuf(b *testing.B) {
req := MCRequest{
Opcode: SET,
Cas: 0,
Opaque: 7242,
VBucket: 824,
Extras: []byte{1},
Key: []byte("somekey"),
Body: []byte("somevalue"),
}
data := req.Bytes()
data[0] = REQ_MAGIC
rdr := bytes.NewReader(data)
b.SetBytes(int64(len(data)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
req2 := MCRequest{}
rdr.Seek(0, 0)
_, err := req2.Receive(rdr, nil)
if err != nil {
b.Fatalf("Error receiving: %v", err)
}
}
}
示例9: BenchmarkInsertions
// This benchmark is mostly useful for very manual inspection and
// debugging, so it's off by default.
func BenchmarkInsertions(b *testing.B) {
var prefixes []*net.IPNet
for _, l := range []int{32, 24, 16, 8} {
for n := 0; n < 1000; n++ {
b := l / 8
ip := make([]byte, 4)
for i := 0; i < b; i++ {
ip[i] = byte(rand.Int())
}
prefixes = append(prefixes, &net.IPNet{net.IP(ip), net.CIDRMask(l, 32)})
}
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
db, err := New(":memory:")
if err != nil {
b.Fatal("Cannot create in-memory DB:", err)
}
r := db.Realm("prod")
if err = r.Create(); err != nil {
b.Fatalf("Creating realm: %s", err)
}
for _, p := range prefixes {
r.Prefix(p).Create()
}
db.Close()
}
}
示例10: BenchmarkUsersProd
func BenchmarkUsersProd(b *testing.B) {
prodfile := "./prods/users.prod"
text, err := ioutil.ReadFile(prodfile)
if err != nil {
b.Fatal(err)
}
seed := uint64(time.Now().UnixNano())
s := parsec.NewScanner(text)
root, _ := Y(s)
scope := root.(common.Scope)
nterms := scope["_nonterminals"].(common.NTForms)
scope = BuildContext(scope, seed, "./bags", prodfile)
b.ResetTimer()
out := 0
for i := 0; i < b.N; i++ {
scope = scope.RebuildContext()
if val, ok := EvalForms("root", scope, nterms["s"]).(string); !ok {
b.Fatalf("not string `%v`\n", val)
} else {
out += len(val)
}
}
b.SetBytes(int64(float64(out) / float64(b.N)))
}
示例11: BenchmarkDecodeStruct
func BenchmarkDecodeStruct(b *testing.B) {
type s1 struct {
Atom types.Atom
priv0 int
Uint8 uint8
Uint16 uint16
Uint32 uint32
priv1 string
Byte byte
Int int
priv2 *s1
List []s1
Binary []byte
}
data := []byte{
131, 104, 8, 100, 0, 12, 116, 104, 105, 115, 32, 105, 115, 32, 97, 116, 111,
109, 97, 255, 98, 0, 0, 255, 255, 110, 4, 0, 255, 255, 255, 255, 97, 128, 98,
255, 255, 253, 102, 106, 107, 0, 5, 1, 2, 3, 4, 5,
}
in := bytes.NewBuffer(data)
var v s1
err := Decode(in, &v)
if err != nil {
b.Fatal(err)
} else if l := in.Len(); l != 0 {
b.Fatalf("buffer len %d", l)
}
for i := 0; i < b.N; i++ {
in = bytes.NewBuffer(data)
err = Decode(in, &v)
}
}
示例12: benchmarkEndToEnd
func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) {
once.Do(startServer)
client, err := dial()
if err != nil {
b.Fatal("error dialing:", err)
}
defer client.Close()
// Synchronous calls
args := &Args{7, 8}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
reply := new(Reply)
for pb.Next() {
err := client.Call("Arith.Add", args, reply)
if err != nil {
b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B)
}
}
})
}
示例13: BenchmarkInsertRecursive
func BenchmarkInsertRecursive(b *testing.B) {
b.StopTimer()
// Add 1k nodes to the tree.
const count = 1000
nodes := make([]*remoteNode, 0, count)
for i := 0; i < count; i++ {
rId := make([]byte, 4)
if _, err := rand.Read(rId); err != nil {
b.Fatal("Couldnt produce random numbers for FindClosest:", err)
}
id := string(rId) + ffff
if len(id) != 20 {
b.Fatalf("Random infohash construction error, wrong len: want %d, got %d",
20, len(id))
}
nodes = append(nodes, &remoteNode{id: id})
}
b.StartTimer()
// Each op is adding 1000 nodes to the tree.
for i := 0; i < b.N; i++ {
tree := &nTree{}
for _, r := range nodes {
tree.insert(r)
}
}
}
示例14: BenchmarkLargeQuery
func BenchmarkLargeQuery(b *testing.B) {
intervals := IntervalSlice{
{4, 15, "First"},
{50, 72, "Second"},
{34, 90, "Third"},
{34, 45, "Fourth"},
{34, 40, "Fifth"},
{34, 34, "Sixth"},
{34, 45, "Seventh"},
}
// Make a result for every q
for i := 0; i < 65535; i++ {
intervals = append(intervals, &Interval{uint16(i), uint16(i), i})
}
stab, err := NewIntervalStabber(intervals)
if err != nil {
b.Fatalf("Unable to setup benchmark: ", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
results, err := stab.Intersect(42)
if err != nil || len(results) != 4 {
b.Fatal("Tests failed during benchmark")
}
}
}
示例15: BenchmarkWithHack
func BenchmarkWithHack(b *testing.B) {
client, srv := net.Pipe()
done := make(chan struct{})
req := []byte("GET /foo\nHost: /var/run/docker.sock\nUser-Agent: Docker\n")
read := make([]byte, 4096)
b.SetBytes(int64(len(req) * 30))
l := MalformedHostHeaderOverrideConn{client, true}
go func() {
for {
if _, err := srv.Write(req); err != nil {
srv.Close()
break
}
l.first = true // make sure each subsequent run uses the hack parsing
}
close(done)
}()
for i := 0; i < b.N; i++ {
for i := 0; i < 30; i++ {
if n, err := l.Read(read); err != nil && err != io.EOF {
b.Fatalf("read: %d - %d, err: %v\n%s", n, len(req), err, string(read[:n]))
}
}
}
l.Close()
<-done
}