How to use stopTimer method of conn Package

Best Gauge code snippet using conn.stopTimer

sys_test.go

Source:sys_test.go Github

copy

Full Screen

1package main2import (3 "log"4 "net"5 "os"6 "testing"7 "time"8)9const textFile = "hello_world.txt"10const ipport = "127.0.0.1:12345"11func BenchmarkFileOpen(b *testing.B) {12 f := CreateOrDie(textFile)13 f.Close()14 b.ResetTimer()15 for i := 0; i < b.N; i++ {16 os.Open(textFile)17 }18 b.StopTimer()19 RemoveOrDie(textFile)20}21func BenchmarkFileRead(b *testing.B) {22 f := CreateOrDie(textFile)23 f.Write([]byte("Hello World\n"))24 f.Close()25 f = OpenOrDie(textFile)26 b1 := make([]byte, 20)27 b.ResetTimer()28 for i := 0; i < b.N; i++ {29 b.StartTimer()30 f.Read(b1)31 b.StopTimer()32 f.Seek(0, 0) // seek to beginning of file33 }34 f.Close()35 RemoveOrDie(textFile)36}37func BenchmarkFileClose(b *testing.B) {38 f := CreateOrDie(textFile)39 b.ResetTimer()40 for i := 0; i < b.N; i++ {41 f.Close()42 b.StopTimer()43 f = OpenOrDie(textFile)44 b.StartTimer()45 }46 RemoveOrDie(textFile)47}48func BenchmarkFileWrite_NoRemove(b *testing.B) {49 f := CreateOrDie(textFile)50 b.ResetTimer()51 for i := 0; i < b.N; i++ {52 f.Write([]byte("Hello World\n"))53 }54 b.StopTimer()55 f.Close()56 RemoveOrDie(textFile)57}58func BenchmarkFileWrite_RemovePerIter(b *testing.B) {59 f := CreateOrDie(textFile)60 b.ResetTimer()61 for i := 0; i < b.N; i++ {62 b.StartTimer()63 f.Write([]byte("Hello World\n"))64 b.StopTimer()65 f.Close()66 RemoveOrDie(textFile)67 f = CreateOrDie(textFile)68 }69 RemoveOrDie(textFile)70}71func BenchmarkFileFstat(b *testing.B) {72 f := CreateOrDie(textFile)73 b.ResetTimer()74 for i := 0; i < b.N; i++ {75 f.Stat()76 }77 b.StopTimer()78 f.Close()79 RemoveOrDie(textFile)80}81func BenchmarkFileStat(b *testing.B) {82 f := CreateOrDie(textFile)83 f.Close()84 b.ResetTimer()85 for i := 0; i < b.N; i++ {86 os.Stat(textFile)87 }88 b.StopTimer()89 RemoveOrDie(textFile)90}91func BenchmarkFileLstat(b *testing.B) {92 f := CreateOrDie(textFile)93 f.Close()94 b.ResetTimer()95 for i := 0; i < b.N; i++ {96 os.Lstat(textFile)97 }98 b.StopTimer()99 RemoveOrDie(textFile)100}101func BenchmarkFileLseek(b *testing.B) {102 offset := int64(5)103 whence := 0 // offset relative to file origin104 f := CreateOrDie(textFile)105 f.Write([]byte("Hello World\n"))106 b.ResetTimer()107 for i := 0; i < b.N; i++ {108 f.Seek(offset, whence)109 }110 b.StopTimer()111 f.Close()112 RemoveOrDie(textFile)113}114func BenchmarkFilePread64(b *testing.B) {115 offset := int64(0)116 f := CreateOrDie(textFile)117 f.Write([]byte("Hello World\n"))118 f.Close()119 f = OpenOrDie(textFile)120 buf := make([]byte, 20)121 b.ResetTimer()122 for i := 0; i < b.N; i++ {123 f.ReadAt(buf, offset)124 }125 b.StopTimer()126 RemoveOrDie(textFile)127}128func BenchmarkFilePwrite64(b *testing.B) {129 offset := int64(5)130 f := CreateOrDie(textFile)131 b.ResetTimer()132 for i := 0; i < b.N; i++ {133 f.WriteAt([]byte("hello_world\n"), offset)134 }135 b.StopTimer()136 f.Close()137 RemoveOrDie(textFile)138}139func BenchmarkGetpagesize(b *testing.B) {140 for i := 0; i < b.N; i++ {141 os.Getpagesize()142 }143}144func BenchmarkExecutable(b *testing.B) {145 for i := 0; i < b.N; i++ {146 os.Executable()147 }148}149func BenchmarkGetpid(b *testing.B) {150 for i := 0; i < b.N; i++ {151 os.Getpid()152 }153}154func BenchmarkGetppid(b *testing.B) {155 for i := 0; i < b.N; i++ {156 os.Getppid()157 }158}159func BenchmarkGetwd(b *testing.B) {160 for i := 0; i < b.N; i++ {161 os.Getwd()162 }163}164func BenchmarkReaddir(b *testing.B) {165 MkdirOrDie("temp")166 CreateOrDie("temp/hello_world.txt")167 f := OpenOrDie("temp")168 b.ResetTimer()169 for i := 0; i < b.N; i++ {170 b.StartTimer()171 f.Readdir(0) // read all files in directory172 b.StopTimer()173 f.Seek(0, 0) // seek to beginning of directory174 }175 RemoveOrDie("temp")176}177func BenchmarkReaddirnames(b *testing.B) {178 MkdirOrDie("temp")179 CreateOrDie("temp/hello_world.txt")180 f := OpenOrDie("temp")181 b.ResetTimer()182 for i := 0; i < b.N; i++ {183 b.StartTimer()184 f.Readdirnames(0) // read all files in directory185 b.StopTimer()186 f.Seek(0, 0) // seek to beginning of directory187 }188 RemoveOrDie("temp")189}190func BenchmarkWait4(b *testing.B) {191 // Parameters to start a new process that sleeps forever,192 // so we can reliably kill it.193 argc := "/bin/sleep"194 argv := []string{"infinity"}195 attr := &os.ProcAttr{196 Dir: "",197 Files: nil,198 Env: nil,199 Sys: nil,200 }201 b.ResetTimer()202 b.StopTimer()203 for i := 0; i < b.N; i++ {204 proc, err := os.StartProcess(argc, argv, attr)205 if err != nil {206 log.Fatal(err)207 }208 err = proc.Kill()209 if err != nil {210 log.Fatal(err)211 }212 b.StartTimer()213 proc.Wait()214 b.StopTimer()215 }216}217func BenchmarkKill(b *testing.B) {218 // Parameters to start a new process that sleeps forever,219 // so we can reliably kill it.220 argc := "/bin/sleep"221 argv := []string{"infinity"}222 attr := &os.ProcAttr{223 Dir: "",224 Files: nil,225 Env: nil,226 Sys: nil,227 }228 b.ResetTimer()229 b.StopTimer()230 for i := 0; i < b.N; i++ {231 proc, err := os.StartProcess(argc, argv, attr)232 if err != nil {233 log.Fatal(err)234 }235 b.StartTimer()236 proc.Kill()237 b.StopTimer()238 // Wait for process to terminate so we don't use up239 // the allotted number of user processes.240 proc.Wait()241 }242}243func BenchmarkGetuid(b *testing.B) {244 for i := 0; i < b.N; i++ {245 os.Getuid()246 }247}248func BenchmarkGeteuid(b *testing.B) {249 for i := 0; i < b.N; i++ {250 os.Geteuid()251 }252}253func BenchmarkGetgid(b *testing.B) {254 for i := 0; i < b.N; i++ {255 os.Getgid()256 }257}258func BenchmarkGetegid(b *testing.B) {259 for i := 0; i < b.N; i++ {260 os.Getegid()261 }262}263func BenchmarkGetgroups(b *testing.B) {264 for i := 0; i < b.N; i++ {265 os.Getgroups()266 }267}268func BenchmarkRename(b *testing.B) {269 f := CreateOrDie(textFile)270 f.Close()271 b.ResetTimer()272 for i := 0; i < b.N; i++ {273 os.Rename(textFile, textFile)274 }275 b.StopTimer()276 RemoveOrDie(textFile)277}278func BenchmarkTruncate(b *testing.B) {279 f := CreateOrDie(textFile)280 f.Close()281 b.ResetTimer()282 for i := 0; i < b.N; i++ {283 // Truncate file to some arbitrary size.284 os.Truncate(textFile, 12)285 }286 b.StopTimer()287 RemoveOrDie(textFile)288}289func BenchmarkLink(b *testing.B) {290 f := CreateOrDie(textFile)291 f.Close()292 b.ResetTimer()293 for i := 0; i < b.N; i++ {294 b.StartTimer()295 os.Link(textFile, "new.txt")296 b.StopTimer()297 RemoveOrDie("new.txt")298 }299 RemoveOrDie(textFile)300}301func BenchmarkSymlink(b *testing.B) {302 f := CreateOrDie(textFile)303 f.Close()304 b.ResetTimer()305 for i := 0; i < b.N; i++ {306 b.StartTimer()307 os.Symlink(textFile, "new.txt")308 b.StopTimer()309 RemoveOrDie("new.txt")310 }311 RemoveOrDie(textFile)312}313func BenchmarkPipe2(b *testing.B) {314 for i := 0; i < b.N; i++ {315 b.StartTimer()316 r, w, err := os.Pipe()317 b.StopTimer()318 if err != nil {319 log.Fatal(err)320 }321 r.Close()322 w.Close()323 }324}325func BenchmarkMkdir(b *testing.B) {326 for i := 0; i < b.N; i++ {327 b.StartTimer()328 os.Mkdir("temp", os.ModePerm)329 b.StopTimer()330 RemoveOrDie("temp")331 }332}333func BenchmarkChdir(b *testing.B) {334 for i := 0; i < b.N; i++ {335 os.Chdir(".")336 }337}338func BenchmarkUnsetenv(b *testing.B) {339 b.StopTimer()340 for i := 0; i < b.N; i++ {341 err := os.Setenv("new_key", "new_value")342 if err != nil {343 log.Fatal(err)344 }345 b.StartTimer()346 os.Unsetenv("new_key")347 b.StopTimer()348 }349}350func BenchmarkGetenv(b *testing.B) {351 err := os.Setenv("new_key", "new_value")352 if err != nil {353 log.Fatal(err)354 }355 b.ResetTimer()356 for i := 0; i < b.N; i++ {357 os.Getenv("new_key")358 }359 b.StopTimer()360 err = os.Unsetenv("new_key")361 if err != nil {362 log.Fatal(err)363 }364}365func BenchmarkSetenv(b *testing.B) {366 for i := 0; i < b.N; i++ {367 b.StartTimer()368 os.Setenv("new_key", "new_value")369 b.StopTimer()370 err := os.Unsetenv("new_key")371 if err != nil {372 log.Fatal(err)373 }374 }375}376func BenchmarkClearenv(b *testing.B) {377 for i := 0; i < b.N; i++ {378 os.Clearenv()379 }380}381func BenchmarkEnviron(b *testing.B) {382 for i := 0; i < b.N; i++ {383 os.Environ()384 }385}386func BenchmarkTimenow(b *testing.B) {387 for i := 0; i < b.N; i++ {388 time.Now()389 }390}391func BenchmarkReadlink(b *testing.B) {392 f := CreateOrDie(textFile)393 f.Close()394 err := os.Symlink(textFile, "link.txt")395 if err != nil {396 log.Fatal(err)397 }398 b.ResetTimer()399 for i := 0; i < b.N; i++ {400 os.Readlink("link.txt")401 }402 b.StopTimer()403 RemoveOrDie("link.txt")404 RemoveOrDie(textFile)405}406func BenchmarkChmod(b *testing.B) {407 f := CreateOrDie(textFile)408 f.Close()409 b.ResetTimer()410 for i := 0; i < b.N; i++ {411 os.Chmod(textFile, os.ModePerm)412 }413 b.StopTimer()414 RemoveOrDie(textFile)415}416func BenchmarkFchmod(b *testing.B) {417 f := CreateOrDie(textFile)418 b.ResetTimer()419 for i := 0; i < b.N; i++ {420 f.Chmod(os.ModePerm)421 }422 b.StopTimer()423 f.Close()424 RemoveOrDie(textFile)425}426func BenchmarkChown(b *testing.B) {427 uid := os.Getuid()428 gid := os.Getgid()429 f := CreateOrDie(textFile)430 f.Close()431 b.ResetTimer()432 for i := 0; i < b.N; i++ {433 os.Chown(textFile, uid, gid)434 }435 b.StopTimer()436 RemoveOrDie(textFile)437}438func BenchmarkLchown(b *testing.B) {439 uid := os.Getuid()440 gid := os.Getgid()441 f := CreateOrDie(textFile)442 f.Close()443 b.ResetTimer()444 for i := 0; i < b.N; i++ {445 os.Lchown(textFile, uid, gid)446 }447 b.StopTimer()448 RemoveOrDie(textFile)449}450func BenchmarkFchown(b *testing.B) {451 uid := os.Getuid()452 gid := os.Getgid()453 f := CreateOrDie(textFile)454 b.ResetTimer()455 for i := 0; i < b.N; i++ {456 f.Chown(uid, gid)457 }458 b.StopTimer()459 f.Close()460 RemoveOrDie(textFile)461}462func BenchmarkFtruncate(b *testing.B) {463 f := CreateOrDie(textFile)464 b.ResetTimer()465 for i := 0; i < b.N; i++ {466 f.Truncate(10)467 }468 b.StopTimer()469 f.Close()470 RemoveOrDie(textFile)471}472func BenchmarkFsync(b *testing.B) {473 f := CreateOrDie(textFile)474 b.ResetTimer()475 for i := 0; i < b.N; i++ {476 f.Sync()477 }478 b.StopTimer()479 f.Close()480 RemoveOrDie(textFile)481}482func BenchmarkUtimes(b *testing.B) {483 now := time.Now()484 f := CreateOrDie(textFile)485 f.Close()486 b.ResetTimer()487 for i := 0; i < b.N; i++ {488 os.Chtimes(textFile, now, now)489 }490 b.StopTimer()491 RemoveOrDie(textFile)492}493func BenchmarkFchdir(b *testing.B) {494 f := CreateOrDie(textFile)495 b.ResetTimer()496 for i := 0; i < b.N; i++ {497 f.Chdir()498 }499 b.StopTimer()500 RemoveOrDie(textFile)501}502func BenchmarkSetDeadline(b *testing.B) {503 now := time.Now()504 r, w, err := os.Pipe()505 if err != nil {506 log.Fatal(err)507 }508 b.ResetTimer()509 for i := 0; i < b.N; i++ {510 r.SetDeadline(now)511 }512 b.StopTimer()513 r.Close()514 w.Close()515}516func BenchmarkSetReadDeadline(b *testing.B) {517 now := time.Now()518 r, w, err := os.Pipe()519 if err != nil {520 log.Fatal(err)521 }522 b.ResetTimer()523 for i := 0; i < b.N; i++ {524 r.SetReadDeadline(now)525 }526 b.StopTimer()527 r.Close()528 w.Close()529}530func BenchmarkSetWriteDeadline(b *testing.B) {531 now := time.Now()532 r, w, err := os.Pipe()533 if err != nil {534 log.Fatal(err)535 }536 b.ResetTimer()537 for i := 0; i < b.N; i++ {538 w.SetWriteDeadline(now)539 }540 b.StopTimer()541 r.Close()542 w.Close()543}544func BenchmarkNetRead(b *testing.B) {545 serverConn, clientConn := GenerateTCPConnPair()546 buf := make([]byte, 20)547 b.ResetTimer()548 b.StopTimer()549 for i := 0; i < b.N; i++ {550 serverConn.Write([]byte("hello world\n"))551 b.StartTimer()552 clientConn.Read(buf)553 b.StopTimer()554 }555 serverConn.Close()556 clientConn.Close()557}558func BenchmarkNetWrite(b *testing.B) {559 serverConn, clientConn := GenerateTCPConnPair()560 buf := make([]byte, 20)561 b.ResetTimer()562 for i := 0; i < b.N; i++ {563 b.StartTimer()564 clientConn.Write([]byte("hello world\n"))565 b.StopTimer()566 serverConn.Read(buf)567 }568 serverConn.Close()569 clientConn.Close()570}571func BenchmarkNetClose(b *testing.B) {572 b.StopTimer()573 for i := 0; i < b.N; i++ {574 serverConn, clientConn := GenerateTCPConnPair()575 b.StartTimer()576 clientConn.Close()577 b.StopTimer()578 serverConn.Close()579 }580}581func BenchmarkNetSetDeadline(b *testing.B) {582 t := time.Now()583 serverConn, clientConn := GenerateTCPConnPair()584 b.ResetTimer()585 for i := 0; i < b.N; i++ {586 clientConn.SetDeadline(t)587 }588 b.StopTimer()589 serverConn.Close()590 clientConn.Close()591}592func BenchmarkNetSetReadDeadline(b *testing.B) {593 t := time.Now()594 serverConn, clientConn := GenerateTCPConnPair()595 b.ResetTimer()596 for i := 0; i < b.N; i++ {597 clientConn.SetReadDeadline(t)598 }599 b.StopTimer()600 serverConn.Close()601 clientConn.Close()602}603func BenchmarkNetSetWriteDeadline(b *testing.B) {604 t := time.Now()605 serverConn, clientConn := GenerateTCPConnPair()606 b.ResetTimer()607 for i := 0; i < b.N; i++ {608 clientConn.SetWriteDeadline(t)609 }610 b.StopTimer()611 serverConn.Close()612 clientConn.Close()613}614func BenchmarkNetSetReadBuffer(b *testing.B) {615 serverConn, clientConn := GenerateTCPConnPair()616 b.ResetTimer()617 for i := 0; i < b.N; i++ {618 clientConn.SetReadBuffer(100)619 }620 b.StopTimer()621 serverConn.Close()622 clientConn.Close()623}624func BenchmarkNetSetWriteBuffer(b *testing.B) {625 serverConn, clientConn := GenerateTCPConnPair()626 b.ResetTimer()627 for i := 0; i < b.N; i++ {628 clientConn.SetWriteBuffer(100)629 }630 b.StopTimer()631 serverConn.Close()632 clientConn.Close()633}634func BenchmarkSocket(b *testing.B) {635 addr, err := net.ResolveTCPAddr("tcp", ipport)636 if err != nil {637 log.Fatal(err)638 }639 listener, err := net.ListenTCP("tcp", addr)640 if err != nil {641 log.Fatal(err)642 }643 b.ResetTimer()644 for i := 0; i < b.N; i++ {645 b.StartTimer()646 clientConn, err := net.DialTCP("tcp", nil, addr)647 b.StopTimer()648 if err != nil {649 log.Fatal(err)650 }651 serverConn, err := listener.AcceptTCP()652 if err != nil {653 log.Fatal(err)654 }655 serverConn.Close()656 clientConn.Close()657 }658 listener.Close()659}660func BenchmarkListenTCP(b *testing.B) {661 addr, err := net.ResolveTCPAddr("tcp", ipport)662 if err != nil {663 log.Fatal(err)664 }665 b.ResetTimer()666 for i := 0; i < b.N; i++ {667 b.StartTimer()668 listener, err := net.ListenTCP("tcp", addr)669 b.StopTimer()670 if err != nil {671 log.Fatal(err)672 }673 listener.Close()674 }675}676// Helpers to reduce boilerplate code677func MkdirOrDie(name string) {678 err := os.MkdirAll(name, os.ModePerm)679 if err != nil {680 log.Fatal(err)681 }682}683func CreateOrDie(name string) *os.File {684 f, err := os.Create(name)685 if err != nil {686 log.Fatal(err)687 }688 return f689}690func OpenOrDie(name string) *os.File {691 f, err := os.Open(name)692 if err != nil {693 log.Fatal(err)694 }695 return f696}697func RemoveOrDie(name string) {698 err := os.RemoveAll(name)699 if err != nil {700 log.Fatal(err)701 }702}703func GenerateTCPConnPair() (*net.TCPConn, *net.TCPConn) {704 addr, err := net.ResolveTCPAddr("tcp", ipport)705 if err != nil {706 log.Fatal(err)707 }708 listener, err := net.ListenTCP("tcp", addr)709 if err != nil {710 log.Fatal(err)711 }712 defer listener.Close()713 clientConn, err := net.DialTCP("tcp", nil, addr)714 if err != nil {715 log.Fatal(err)716 }717 serverConn, err := listener.AcceptTCP()718 if err != nil {719 log.Fatal(err)720 }721 return serverConn, clientConn722}...

Full Screen

Full Screen

bench_test.go

Source:bench_test.go Github

copy

Full Screen

1// Copyright 2012-2015 Apcera Inc. All rights reserved.2package test3import (4 "bufio"5 "fmt"6 "math/rand"7 "net"8 "testing"9 "time"10 "github.com/nats-io/gnatsd/server"11)12const PERF_PORT = 842213// For Go routine based server.14func runBenchServer() *server.Server {15 opts := DefaultTestOptions16 opts.Port = PERF_PORT17 return RunServer(&opts)18}19const defaultRecBufSize = 3276820const defaultSendBufSize = 3276821func flushConnection(b *testing.B, c net.Conn) {22 buf := make([]byte, 32)23 c.Write([]byte("PING\r\n"))24 c.SetReadDeadline(time.Now().Add(1 * time.Second))25 n, err := c.Read(buf)26 c.SetReadDeadline(time.Time{})27 if err != nil {28 b.Fatalf("Failed read: %v\n", err)29 }30 if n != 6 && buf[0] != 'P' && buf[1] != 'O' {31 b.Fatalf("Failed read of PONG: %s\n", buf)32 }33}34func benchPub(b *testing.B, subject, payload string) {35 b.StopTimer()36 s := runBenchServer()37 c := createClientConn(b, "localhost", PERF_PORT)38 doDefaultConnect(b, c)39 bw := bufio.NewWriterSize(c, defaultSendBufSize)40 sendOp := []byte(fmt.Sprintf("PUB %s %d\r\n%s\r\n", subject, len(payload), payload))41 b.SetBytes(int64(len(sendOp)))42 b.StartTimer()43 for i := 0; i < b.N; i++ {44 bw.Write(sendOp)45 }46 bw.Flush()47 flushConnection(b, c)48 b.StopTimer()49 c.Close()50 s.Shutdown()51}52var ch = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$#%^&*()")53func sizedBytes(sz int) []byte {54 b := make([]byte, sz)55 for i := range b {56 b[i] = ch[rand.Intn(len(ch))]57 }58 return b59}60func sizedString(sz int) string {61 return string(sizedBytes(sz))62}63// Publish subject for pub benchmarks.64var psub = "a"65func Benchmark_____Pub0b_Payload(b *testing.B) {66 benchPub(b, psub, "")67}68func Benchmark_____Pub8b_Payload(b *testing.B) {69 b.StopTimer()70 s := sizedString(8)71 benchPub(b, psub, s)72}73func Benchmark____Pub32b_Payload(b *testing.B) {74 b.StopTimer()75 s := sizedString(32)76 benchPub(b, psub, s)77}78func Benchmark___Pub128B_Payload(b *testing.B) {79 b.StopTimer()80 s := sizedString(128)81 benchPub(b, psub, s)82}83func Benchmark___Pub256B_Payload(b *testing.B) {84 b.StopTimer()85 s := sizedString(256)86 benchPub(b, psub, s)87}88func Benchmark_____Pub1K_Payload(b *testing.B) {89 b.StopTimer()90 s := sizedString(1024)91 benchPub(b, psub, s)92}93func Benchmark_____Pub4K_Payload(b *testing.B) {94 b.StopTimer()95 s := sizedString(4 * 1024)96 benchPub(b, psub, s)97}98func Benchmark_____Pub8K_Payload(b *testing.B) {99 b.StopTimer()100 s := sizedString(8 * 1024)101 benchPub(b, psub, s)102}103func drainConnection(b *testing.B, c net.Conn, ch chan bool, expected int) {104 buf := make([]byte, defaultRecBufSize)105 bytes := 0106 for {107 c.SetReadDeadline(time.Now().Add(5 * time.Second))108 n, err := c.Read(buf)109 if err != nil {110 b.Errorf("Error on read: %v\n", err)111 break112 }113 bytes += n114 if bytes >= expected {115 break116 }117 }118 if bytes != expected {119 b.Errorf("Did not receive all bytes: %d vs %d\n", bytes, expected)120 }121 ch <- true122}123// Benchmark the authorization code path.124func Benchmark_AuthPub0b_Payload(b *testing.B) {125 b.StopTimer()126 srv, opts := RunServerWithConfig("./configs/authorization.conf")127 defer srv.Shutdown()128 c := createClientConn(b, opts.Host, opts.Port)129 defer c.Close()130 expectAuthRequired(b, c)131 cs := fmt.Sprintf("CONNECT {\"verbose\":false,\"user\":\"%s\",\"pass\":\"%s\"}\r\n", "bench", DefaultPass)132 sendProto(b, c, cs)133 bw := bufio.NewWriterSize(c, defaultSendBufSize)134 sendOp := []byte("PUB a 0\r\n\r\n")135 b.SetBytes(int64(len(sendOp)))136 b.StartTimer()137 for i := 0; i < b.N; i++ {138 bw.Write(sendOp)139 }140 bw.Flush()141 flushConnection(b, c)142 b.StopTimer()143}144func Benchmark____________PubSub(b *testing.B) {145 b.StopTimer()146 s := runBenchServer()147 c := createClientConn(b, "localhost", PERF_PORT)148 doDefaultConnect(b, c)149 sendProto(b, c, "SUB foo 1\r\n")150 bw := bufio.NewWriterSize(c, defaultSendBufSize)151 sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n"))152 ch := make(chan bool)153 expected := len("MSG foo 1 2\r\nok\r\n") * b.N154 go drainConnection(b, c, ch, expected)155 b.StartTimer()156 for i := 0; i < b.N; i++ {157 _, err := bw.Write(sendOp)158 if err != nil {159 b.Errorf("Received error on PUB write: %v\n", err)160 }161 }162 err := bw.Flush()163 if err != nil {164 b.Errorf("Received error on FLUSH write: %v\n", err)165 }166 // Wait for connection to be drained167 <-ch168 b.StopTimer()169 c.Close()170 s.Shutdown()171}172func Benchmark____PubSubTwoConns(b *testing.B) {173 b.StopTimer()174 s := runBenchServer()175 c := createClientConn(b, "localhost", PERF_PORT)176 doDefaultConnect(b, c)177 bw := bufio.NewWriterSize(c, defaultSendBufSize)178 c2 := createClientConn(b, "localhost", PERF_PORT)179 doDefaultConnect(b, c2)180 sendProto(b, c2, "SUB foo 1\r\n")181 flushConnection(b, c2)182 sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n"))183 ch := make(chan bool)184 expected := len("MSG foo 1 2\r\nok\r\n") * b.N185 go drainConnection(b, c2, ch, expected)186 b.StartTimer()187 for i := 0; i < b.N; i++ {188 bw.Write(sendOp)189 }190 err := bw.Flush()191 if err != nil {192 b.Errorf("Received error on FLUSH write: %v\n", err)193 }194 // Wait for connection to be drained195 <-ch196 b.StopTimer()197 c.Close()198 c2.Close()199 s.Shutdown()200}201func Benchmark____PubTwoQueueSub(b *testing.B) {202 b.StopTimer()203 s := runBenchServer()204 c := createClientConn(b, "localhost", PERF_PORT)205 doDefaultConnect(b, c)206 sendProto(b, c, "SUB foo group1 1\r\n")207 sendProto(b, c, "SUB foo group1 2\r\n")208 bw := bufio.NewWriterSize(c, defaultSendBufSize)209 sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n"))210 ch := make(chan bool)211 expected := len("MSG foo 1 2\r\nok\r\n") * b.N212 go drainConnection(b, c, ch, expected)213 b.StartTimer()214 for i := 0; i < b.N; i++ {215 _, err := bw.Write(sendOp)216 if err != nil {217 b.Fatalf("Received error on PUB write: %v\n", err)218 }219 }220 err := bw.Flush()221 if err != nil {222 b.Fatalf("Received error on FLUSH write: %v\n", err)223 }224 // Wait for connection to be drained225 <-ch226 b.StopTimer()227 c.Close()228 s.Shutdown()229}230func Benchmark___PubFourQueueSub(b *testing.B) {231 b.StopTimer()232 s := runBenchServer()233 c := createClientConn(b, "localhost", PERF_PORT)234 doDefaultConnect(b, c)235 sendProto(b, c, "SUB foo group1 1\r\n")236 sendProto(b, c, "SUB foo group1 2\r\n")237 sendProto(b, c, "SUB foo group1 3\r\n")238 sendProto(b, c, "SUB foo group1 4\r\n")239 bw := bufio.NewWriterSize(c, defaultSendBufSize)240 sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n"))241 ch := make(chan bool)242 expected := len("MSG foo 1 2\r\nok\r\n") * b.N243 go drainConnection(b, c, ch, expected)244 b.StartTimer()245 for i := 0; i < b.N; i++ {246 _, err := bw.Write(sendOp)247 if err != nil {248 b.Fatalf("Received error on PUB write: %v\n", err)249 }250 }251 err := bw.Flush()252 if err != nil {253 b.Fatalf("Received error on FLUSH write: %v\n", err)254 }255 // Wait for connection to be drained256 <-ch257 b.StopTimer()258 c.Close()259 s.Shutdown()260}261func Benchmark__PubEightQueueSub(b *testing.B) {262 b.StopTimer()263 s := runBenchServer()264 c := createClientConn(b, "localhost", PERF_PORT)265 doDefaultConnect(b, c)266 sendProto(b, c, "SUB foo group1 1\r\n")267 sendProto(b, c, "SUB foo group1 2\r\n")268 sendProto(b, c, "SUB foo group1 3\r\n")269 sendProto(b, c, "SUB foo group1 4\r\n")270 sendProto(b, c, "SUB foo group1 5\r\n")271 sendProto(b, c, "SUB foo group1 6\r\n")272 sendProto(b, c, "SUB foo group1 7\r\n")273 sendProto(b, c, "SUB foo group1 8\r\n")274 bw := bufio.NewWriterSize(c, defaultSendBufSize)275 sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n"))276 ch := make(chan bool)277 expected := len("MSG foo 1 2\r\nok\r\n") * b.N278 go drainConnection(b, c, ch, expected)279 b.StartTimer()280 for i := 0; i < b.N; i++ {281 _, err := bw.Write(sendOp)282 if err != nil {283 b.Fatalf("Received error on PUB write: %v\n", err)284 }285 }286 err := bw.Flush()287 if err != nil {288 b.Fatalf("Received error on FLUSH write: %v\n", err)289 }290 // Wait for connection to be drained291 <-ch292 b.StopTimer()293 c.Close()294 s.Shutdown()295}296func routePubSub(b *testing.B, size int) {297 b.StopTimer()298 s1, o1 := RunServerWithConfig("./configs/srv_a.conf")299 defer s1.Shutdown()300 s2, o2 := RunServerWithConfig("./configs/srv_b.conf")301 defer s2.Shutdown()302 sub := createClientConn(b, o1.Host, o1.Port)303 doDefaultConnect(b, sub)304 sendProto(b, sub, "SUB foo 1\r\n")305 flushConnection(b, sub)306 payload := sizedString(size)307 pub := createClientConn(b, o2.Host, o2.Port)308 doDefaultConnect(b, pub)309 bw := bufio.NewWriterSize(pub, defaultSendBufSize)310 ch := make(chan bool)311 sendOp := []byte(fmt.Sprintf("PUB foo %d\r\n%s\r\n", len(payload), payload))312 expected := len(fmt.Sprintf("MSG foo 1 %d\r\n%s\r\n", len(payload), payload)) * b.N313 go drainConnection(b, sub, ch, expected)314 b.StartTimer()315 for i := 0; i < b.N; i++ {316 _, err := bw.Write(sendOp)317 if err != nil {318 b.Fatalf("Received error on PUB write: %v\n", err)319 }320 }321 err := bw.Flush()322 if err != nil {323 b.Errorf("Received error on FLUSH write: %v\n", err)324 }325 // Wait for connection to be drained326 <-ch327 b.StopTimer()328 pub.Close()329 sub.Close()330}331func Benchmark___RoutedPubSub_0b(b *testing.B) {332 routePubSub(b, 2)333}334func Benchmark___RoutedPubSub_1K(b *testing.B) {335 routePubSub(b, 1024)336}337func Benchmark_RoutedPubSub_100K(b *testing.B) {338 routePubSub(b, 100*1024)339}...

Full Screen

Full Screen

stopTimer

Using AI Code Generation

copy

Full Screen

1conn.stopTimer()2conn.stopTimer()3conn.stopTimer()4conn.stopTimer()5conn.stopTimer()6conn.stopTimer()7conn.stopTimer()8conn.stopTimer()9conn.stopTimer()10conn.stopTimer()11conn.stopTimer()12conn.stopTimer()13conn.stopTimer()14conn.stopTimer()15conn.stopTimer()16conn.stopTimer()17conn.stopTimer()18conn.stopTimer()19conn.stopTimer()20conn.stopTimer()21conn.stopTimer()22conn.stopTimer()23conn.stopTimer()

Full Screen

Full Screen

stopTimer

Using AI Code Generation

copy

Full Screen

1func main() {2 conn := new(Conn)3 conn.stopTimer()4}5import (6type Conn struct {7}8func (c *Conn) stopTimer() {9 fmt.Println("Stop timer")10}11func main() {12 conn := new(Conn)13 conn.stopTimer()14}15import (16type Conn struct {17}18func (c *Conn) stopTimer() {19 fmt.Println("Stop timer")20}21func main() {22 conn := new(Conn)23 conn.stopTimer()24}25import (26type Conn struct {27}28func (c *Conn) stopTimer() {29 fmt.Println("Stop timer")30}31func main() {32 conn := new(Conn)33 conn.stopTimer()34}35import (36type Conn struct {37}38func (c *Conn) stopTimer() {39 fmt.Println("Stop timer")40}41func main() {42 conn := new(Conn)43 conn.stopTimer()44}45import (46type Conn struct {47}48func (c *Conn) stopTimer() {49 fmt.Println("Stop timer")50}51func main() {52 conn := new(Conn)53 conn.stopTimer()54}55import (56type Conn struct {57}58func (c *Conn) stopTimer() {59 fmt.Println("Stop timer")60}61func main() {62 conn := new(Conn)63 conn.stopTimer()64}65import (66type Conn struct {67}68func (c *Conn) stopTimer() {69 fmt.Println("Stop timer")70}71func main() {72 conn := new(Conn)73 conn.stopTimer()74}75import (76type Conn struct {

Full Screen

Full Screen

stopTimer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 timer1 := time.NewTimer(2 * time.Second)4 fmt.Println("Timer 1 expired")5 timer2 := time.NewTimer(2 * time.Second)6 stop2 := timer2.Stop()7 if stop2 {8 fmt.Println("Timer 2 stopped")9 }10}11How to use the time.AfterFunc() function in Golang?12How to use the time.After() function in Golang?13How to use the time.Tick() function in Golang?14How to use the time.Sleep() function in Golang?15How to use the time.Ticker() function in Golang?16How to use the time.Timer() function in Golang?17How to use the time.Sleeper() function in Golang?18How to use the time.Ticker() function in Golang?19How to use the time.Sleep() function in Golang?20How to use the time.Tick() function in Golang?21How to use the time.After() function in Golang?22How to use the time.AfterFunc() function in Golang?23How to use the time.Sleeper() function in Golang?24How to use the time.Ticker() function in Golang?25How to use the time.Timer() function in Golang?26How to use the time.Sleep() function in Golang?27How to use the time.Tick() function in Golang?28How to use the time.After() function in Golang?29How to use the time.AfterFunc() function in Golang?30How to use the time.Sleeper() function in Golang?31How to use the time.Ticker() function in Golang?32How to use the time.Timer() function in Golang?33How to use the time.Sleep() function in Golang?34How to use the time.Tick() function in Golang?35How to use the time.After() function in Golang?36How to use the time.AfterFunc() function in Golang?37How to use the time.Sleeper() function in Golang?38How to use the time.Ticker() function in Golang?39How to use the time.Timer() function in Golang?

Full Screen

Full Screen

stopTimer

Using AI Code Generation

copy

Full Screen

1func main() {2 conn := new(Connection)3 conn.startTimer()4 time.Sleep(5 * time.Second)5 conn.stopTimer()6}

Full Screen

Full Screen

stopTimer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 c := conn{make(chan bool), time.Now()}5 go c.stopTimer()6 time.Sleep(5 * time.Second)7 fmt.Println("Exiting main")8}9type conn struct {10}11func (c *conn) stopTimer() {12 fmt.Println("In stopTimer")13 fmt.Println("Exiting stopTimer")14}15import (16func main() {17 fmt.Println("Hello, playground")18 c := make(chan bool)19 go stopTimer(c)20 time.Sleep(5 * time.Second)21 fmt.Println("Exiting main")22}23func stopTimer(done chan bool) {24 fmt.Println("In stopTimer")25 fmt.Println("Exiting stopTimer

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful