本文整理匯總了Golang中testing.B.StopTimer方法的典型用法代碼示例。如果您正苦於以下問題:Golang B.StopTimer方法的具體用法?Golang B.StopTimer怎麽用?Golang B.StopTimer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類testing.B
的用法示例。
在下文中一共展示了B.StopTimer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: BenchmarkCacheComplete
func BenchmarkCacheComplete(b *testing.B) {
b.StopTimer()
paths := DefaultPaths()
if len(paths) == 0 {
b.Skip("No default paths available")
}
tests := []string{"mscorlib.dll", "System.dll"}
c := Cache{paths: paths}
for _, test := range tests {
if _, err := c.Load(test); err != nil {
b.Error(err)
}
}
tests2 := []content.Type{
content.Type{Name: content.FullyQualifiedName{Absolute: "net://type/System.String"}},
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, test := range tests2 {
if _, err := c.Complete(&test); err != nil {
b.Error(err)
}
}
}
}
示例2: doBenchmarkDbQuery
func doBenchmarkDbQuery(b *testing.B) {
b.StopTimer()
bas := new(basic)
bas.Name = "Basic"
bas.State = 3
mg, _ := GetMigration()
mg.DropTable(bas)
mg.CreateTableIfNotExists(bas)
q, _ := GetQbs()
q.Save(bas)
closeMigrationAndQbs(mg, q)
b.StartTimer()
db, _ := sql.Open(driver, driverSource)
query := q.Dialect.substituteMarkers("SELECT * FROM basic WHERE id = ?")
for i := 0; i < b.N; i++ {
ba := new(basic)
rows, err := db.Query(query, 1)
if err != nil {
panic(err)
}
rows.Next()
err = rows.Scan(&ba.Id, &ba.Name, &ba.State)
if err != nil {
panic(err)
}
rows.Close()
}
db.Close()
b.StopTimer()
runtime.GC()
stats := new(runtime.MemStats)
runtime.ReadMemStats(stats)
fmt.Printf("alloc:%d, total:%d\n", stats.Alloc, stats.TotalAlloc)
}
示例3: BenchmarkExist
func BenchmarkExist(b *testing.B) {
b.StopTimer()
var d Interface
if opt == "slice" {
d = newMapToSlice()
} else {
d = newMapToMap()
}
k := "A"
for _, v := range testVals {
d.set(k, v)
}
// to make it not biased towards data structures
// with an order, such as slice.
ix := rand.Perm(testN)
b.StartTimer()
b.ReportAllocs()
for _, idx := range ix {
if !d.exist(k, testVals[idx]) {
b.Errorf("%s should have existed!", testVals[idx])
}
}
}
示例4: BenchmarkTopicToChannelPut
func BenchmarkTopicToChannelPut(b *testing.B) {
b.StopTimer()
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
topicName := "bench_topic_to_channel_put" + strconv.Itoa(b.N)
channelName := "bench"
options := NewNSQDOptions()
options.MemQueueSize = int64(b.N)
_, _, nsqd := mustStartNSQD(options)
defer nsqd.Exit()
channel := nsqd.GetTopic(topicName).GetChannel(channelName)
b.StartTimer()
for i := 0; i <= b.N; i++ {
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
topic.PutMessage(msg)
}
for {
if len(channel.memoryMsgChan) == b.N {
break
}
runtime.Gosched()
}
}
示例5: 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")
}
}
}
示例6: BenchmarkClientServer
func BenchmarkClientServer(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
handler, err := NewHandler(func(in Ping, out *Pong) error {
if in.Data != "ping" {
b.Fatalf("expected ping, got %q", in.Data)
}
out.Data = "pong"
return nil
})
if err != nil {
b.Fatalf("NewHandler: %v", err)
}
s := npctest.NewServer(handler)
defer s.Close()
c := NewClient([]string{s.Listener.Addr().String()})
defer c.Close()
b.StartTimer()
for i := 0; i < b.N; i++ {
var pong Pong
err = c.Call(Ping{"ping"}, &pong)
if err != nil {
b.Fatalf("Call: %v", err)
}
if pong.Data != "pong" {
b.Fatalf("expected pong, got %q", pong.Data)
}
}
}
示例7: benchmarkEndToEnd
func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) {
b.StopTimer()
once.Do(startServer)
client, err := dial()
if err != nil {
b.Fatal("error dialing:", err)
}
// Synchronous calls
args := &Args{7, 8}
procs := runtime.GOMAXPROCS(-1)
N := int32(b.N)
var wg sync.WaitGroup
wg.Add(procs)
b.StartTimer()
for p := 0; p < procs; p++ {
go func() {
reply := new(Reply)
for atomic.AddInt32(&N, -1) >= 0 {
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)
}
}
wg.Done()
}()
}
wg.Wait()
}
示例8: bench
func bench(b *testing.B, size int, algo func(Interface), name string) {
b.StopTimer()
data := make(intPairs, size)
x := ^uint32(0)
for i := 0; i < b.N; i++ {
for n := size - 3; n <= size+3; n++ {
for i := 0; i < len(data); i++ {
x += x
x ^= 1
if int32(x) < 0 {
x ^= 0x88888eef
}
data[i].a = int(x % uint32(n/5))
}
data.initB()
b.StartTimer()
algo(data)
b.StopTimer()
if !IsSorted(data) {
b.Errorf("%s did not sort %d ints", name, n)
}
if name == "Stable" && !data.inOrder() {
b.Errorf("%s unstable on %d ints", name, n)
}
}
}
}
示例9: BenchmarkSecretConnection
func BenchmarkSecretConnection(b *testing.B) {
b.StopTimer()
fooSecConn, barSecConn := makeSecretConnPair(b)
fooWriteText := RandStr(dataMaxSize)
// Consume reads from bar's reader
go func() {
readBuffer := make([]byte, dataMaxSize)
for {
_, err := barSecConn.Read(readBuffer)
if err == io.EOF {
return
} else if err != nil {
b.Fatalf("Failed to read from barSecConn: %v", err)
}
}
}()
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := fooSecConn.Write([]byte(fooWriteText))
if err != nil {
b.Fatalf("Failed to write to fooSecConn: %v", err)
}
}
b.StopTimer()
fooSecConn.Close()
//barSecConn.Close() race condition
}
示例10: BenchmarkEncodeRGBOpaque
func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
// Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds()
rnd := rand.New(rand.NewSource(123))
for y := bo.Min.Y; y < bo.Max.Y; y++ {
for x := bo.Min.X; x < bo.Max.X; x++ {
img.Set(x, y, color.RGBA{
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
255})
}
}
if !img.Opaque() {
b.Fatal("expected image to be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
options := &Options{Quality: 90}
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, options)
}
}
示例11: BenchmarkHelloRServer
func BenchmarkHelloRServer(b *testing.B) {
test := func(k *kernel) {
server := httptest.NewServer(k)
defer server.Close()
cli := &http.Client{Transport: &http.Transport{}}
b.ResetTimer()
defer b.StopTimer()
for i := 0; i < b.N; i++ {
body := bytes.NewBuffer([]byte(`{"name": "bigdatabob"}`))
req, err := http.NewRequest("POST", server.URL, body)
if err != nil {
b.Errorf("could not create request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := cli.Do(req)
if err != nil {
b.Errorf("could not make a request: %v", err)
return
}
if resp.StatusCode != http.StatusOK {
b.Errorf("bad response from kernel: %s", resp.Status)
return
}
resp.Body.Close()
}
}
testDeployment(b, integration.RHello, test)
}
示例12: BenchmarkDagmodWrite
func BenchmarkDagmodWrite(b *testing.B) {
b.StopTimer()
dserv, pins := getMockDagServ(b)
_, n := getNode(b, dserv, 0, pins)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wrsize := 4096
dagmod, err := NewDagModifier(ctx, n, dserv, pins, sizeSplitterGen(512))
if err != nil {
b.Fatal(err)
}
buf := make([]byte, b.N*wrsize)
u.NewTimeSeededRand().Read(buf)
b.StartTimer()
b.SetBytes(int64(wrsize))
for i := 0; i < b.N; i++ {
n, err := dagmod.Write(buf[i*wrsize : (i+1)*wrsize])
if err != nil {
b.Fatal(err)
}
if n != wrsize {
b.Fatal("Wrote bad size")
}
}
}
示例13: BenchmarkLanguage
func BenchmarkLanguage(b *testing.B) {
b.StopTimer()
tst := []string{
"language.go",
"testdata/main.go",
}
var d0 []string
for _, s := range tst {
if d, err := ioutil.ReadFile(s); err != nil {
b.Errorf("Couldn't load file %s: %s", s, err)
} else {
d0 = append(d0, string(d))
}
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := range d0 {
lp, err := NewLanguageParser("testdata/Go.tmLanguage", d0[j])
if err != nil {
b.Fatal(err)
return
}
lp.Parse()
}
}
fmt.Println(util.Prof)
}
示例14: runBenchmarkInsert
// runBenchmarkInsert benchmarks inserting count rows into a table.
func runBenchmarkInsert(b *testing.B, db *gosql.DB, count int) {
if _, err := db.Exec(`DROP TABLE IF EXISTS bench.insert`); err != nil {
b.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE bench.insert (k INT PRIMARY KEY)`); err != nil {
b.Fatal(err)
}
defer func() {
if _, err := db.Exec(`DROP TABLE bench.insert`); err != nil {
b.Fatal(err)
}
}()
b.ResetTimer()
var buf bytes.Buffer
val := 0
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(`INSERT INTO bench.insert VALUES `)
for j := 0; j < count; j++ {
if j > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "(%d)", val)
val++
}
if _, err := db.Exec(buf.String()); err != nil {
b.Fatal(err)
}
}
b.StopTimer()
}
示例15: doBenchmarkFind
func doBenchmarkFind(b *testing.B) {
b.StopTimer()
bas := new(basic)
bas.Name = "Basic"
bas.State = 3
mg, _ := GetMigration()
mg.DropTable(bas)
mg.CreateTableIfNotExists(bas)
q, _ := GetQbs()
q.Save(bas)
closeMigrationAndQbs(mg, q)
b.StartTimer()
for i := 0; i < b.N; i++ {
ba := new(basic)
ba.Id = 1
q, _ = GetQbs()
err := q.Find(ba)
if err != nil {
panic(err)
}
q.Close()
}
b.StopTimer()
runtime.GC()
stats := new(runtime.MemStats)
runtime.ReadMemStats(stats)
fmt.Printf("alloc:%d, total:%d\n", stats.Alloc, stats.TotalAlloc)
}