How to use Write method of conn Package

Best Gauge code snippet using conn.Write

bench_test.go

Source:bench_test.go Github

copy

Full Screen

...244 b.Fatalf("bad value for lastLoginTime: %v", record.lastLoginTime)245 }246 }247}248const benchmarkWriteTableCreateSQL = `drop table if exists t;249create table t(250 varchar_1 varchar not null,251 varchar_2 varchar not null,252 varchar_null_1 varchar,253 date_1 date not null,254 date_null_1 date,255 int4_1 int4 not null,256 int4_2 int4 not null,257 int4_null_1 int4,258 tstz_1 timestamptz not null,259 tstz_2 timestamptz,260 bool_1 bool not null,261 bool_2 bool not null,262 bool_3 bool not null263);264`265const benchmarkWriteTableInsertSQL = `insert into t(266 varchar_1,267 varchar_2,268 varchar_null_1,269 date_1,270 date_null_1,271 int4_1,272 int4_2,273 int4_null_1,274 tstz_1,275 tstz_2,276 bool_1,277 bool_2,278 bool_3279) values (280 $1::varchar,281 $2::varchar,282 $3::varchar,283 $4::date,284 $5::date,285 $6::int4,286 $7::int4,287 $8::int4,288 $9::timestamptz,289 $10::timestamptz,290 $11::bool,291 $12::bool,292 $13::bool293)`294type benchmarkWriteTableCopyFromSrc struct {295 count int296 idx int297 row []interface{}298}299func (s *benchmarkWriteTableCopyFromSrc) Next() bool {300 s.idx++301 return s.idx < s.count302}303func (s *benchmarkWriteTableCopyFromSrc) Values() ([]interface{}, error) {304 return s.row, nil305}306func (s *benchmarkWriteTableCopyFromSrc) Err() error {307 return nil308}309func newBenchmarkWriteTableCopyFromSrc(count int) pgx.CopyFromSource {310 return &benchmarkWriteTableCopyFromSrc{311 count: count,312 row: []interface{}{313 "varchar_1",314 "varchar_2",315 pgtype.Text{},316 time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local),317 pgtype.Date{},318 1,319 2,320 pgtype.Int4{},321 time.Date(2001, 1, 1, 0, 0, 0, 0, time.Local),322 time.Date(2002, 1, 1, 0, 0, 0, 0, time.Local),323 true,324 false,325 true,326 },327 }328}329func benchmarkWriteNRowsViaInsert(b *testing.B, n int) {330 conn := mustConnect(b, *defaultConnConfig)331 defer closeConn(b, conn)332 mustExec(b, conn, benchmarkWriteTableCreateSQL)333 _, err := conn.Prepare("insert_t", benchmarkWriteTableInsertSQL)334 if err != nil {335 b.Fatal(err)336 }337 b.ResetTimer()338 for i := 0; i < b.N; i++ {339 src := newBenchmarkWriteTableCopyFromSrc(n)340 tx, err := conn.Begin()341 if err != nil {342 b.Fatal(err)343 }344 for src.Next() {345 values, _ := src.Values()346 if _, err = tx.Exec("insert_t", values...); err != nil {347 b.Fatalf("Exec unexpectedly failed with: %v", err)348 }349 }350 err = tx.Commit()351 if err != nil {352 b.Fatal(err)353 }354 }355}356// note this function is only used for benchmarks -- it doesn't escape tableName357// or columnNames358func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc pgx.CopyFromSource) (int, error) {359 maxRowsPerInsert := 65535 / len(columnNames)360 rowsThisInsert := 0361 rowCount := 0362 sqlBuf := &bytes.Buffer{}363 args := make(pgx.QueryArgs, 0)364 resetQuery := func() {365 sqlBuf.Reset()366 fmt.Fprintf(sqlBuf, "insert into %s(%s) values", tableName, strings.Join(columnNames, ", "))367 args = args[0:0]368 rowsThisInsert = 0369 }370 resetQuery()371 tx, err := conn.Begin()372 if err != nil {373 return 0, err374 }375 defer tx.Rollback()376 for rowSrc.Next() {377 if rowsThisInsert > 0 {378 sqlBuf.WriteByte(',')379 }380 sqlBuf.WriteByte('(')381 values, err := rowSrc.Values()382 if err != nil {383 return 0, err384 }385 for i, val := range values {386 if i > 0 {387 sqlBuf.WriteByte(',')388 }389 sqlBuf.WriteString(args.Append(val))390 }391 sqlBuf.WriteByte(')')392 rowsThisInsert++393 if rowsThisInsert == maxRowsPerInsert {394 _, err := tx.Exec(sqlBuf.String(), args...)395 if err != nil {396 return 0, err397 }398 rowCount += rowsThisInsert399 resetQuery()400 }401 }402 if rowsThisInsert > 0 {403 _, err := tx.Exec(sqlBuf.String(), args...)404 if err != nil {405 return 0, err406 }407 rowCount += rowsThisInsert408 }409 if err := tx.Commit(); err != nil {410 return 0, nil411 }412 return rowCount, nil413}414func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) {415 conn := mustConnect(b, *defaultConnConfig)416 defer closeConn(b, conn)417 mustExec(b, conn, benchmarkWriteTableCreateSQL)418 _, err := conn.Prepare("insert_t", benchmarkWriteTableInsertSQL)419 if err != nil {420 b.Fatal(err)421 }422 b.ResetTimer()423 for i := 0; i < b.N; i++ {424 src := newBenchmarkWriteTableCopyFromSrc(n)425 _, err := multiInsert(conn, "t",426 []string{"varchar_1",427 "varchar_2",428 "varchar_null_1",429 "date_1",430 "date_null_1",431 "int4_1",432 "int4_2",433 "int4_null_1",434 "tstz_1",435 "tstz_2",436 "bool_1",437 "bool_2",438 "bool_3"},439 src)440 if err != nil {441 b.Fatal(err)442 }443 }444}445func benchmarkWriteNRowsViaCopy(b *testing.B, n int) {446 conn := mustConnect(b, *defaultConnConfig)447 defer closeConn(b, conn)448 mustExec(b, conn, benchmarkWriteTableCreateSQL)449 b.ResetTimer()450 for i := 0; i < b.N; i++ {451 src := newBenchmarkWriteTableCopyFromSrc(n)452 _, err := conn.CopyFrom(pgx.Identifier{"t"},453 []string{"varchar_1",454 "varchar_2",455 "varchar_null_1",456 "date_1",457 "date_null_1",458 "int4_1",459 "int4_2",460 "int4_null_1",461 "tstz_1",462 "tstz_2",463 "bool_1",464 "bool_2",465 "bool_3"},466 src)467 if err != nil {468 b.Fatal(err)469 }470 }471}472func BenchmarkWrite5RowsViaInsert(b *testing.B) {473 benchmarkWriteNRowsViaInsert(b, 5)474}475func BenchmarkWrite5RowsViaMultiInsert(b *testing.B) {476 benchmarkWriteNRowsViaMultiInsert(b, 5)477}478func BenchmarkWrite5RowsViaCopy(b *testing.B) {479 benchmarkWriteNRowsViaCopy(b, 5)480}481func BenchmarkWrite10RowsViaInsert(b *testing.B) {482 benchmarkWriteNRowsViaInsert(b, 10)483}484func BenchmarkWrite10RowsViaMultiInsert(b *testing.B) {485 benchmarkWriteNRowsViaMultiInsert(b, 10)486}487func BenchmarkWrite10RowsViaCopy(b *testing.B) {488 benchmarkWriteNRowsViaCopy(b, 10)489}490func BenchmarkWrite100RowsViaInsert(b *testing.B) {491 benchmarkWriteNRowsViaInsert(b, 100)492}493func BenchmarkWrite100RowsViaMultiInsert(b *testing.B) {494 benchmarkWriteNRowsViaMultiInsert(b, 100)495}496func BenchmarkWrite100RowsViaCopy(b *testing.B) {497 benchmarkWriteNRowsViaCopy(b, 100)498}499func BenchmarkWrite1000RowsViaInsert(b *testing.B) {500 benchmarkWriteNRowsViaInsert(b, 1000)501}502func BenchmarkWrite1000RowsViaMultiInsert(b *testing.B) {503 benchmarkWriteNRowsViaMultiInsert(b, 1000)504}505func BenchmarkWrite1000RowsViaCopy(b *testing.B) {506 benchmarkWriteNRowsViaCopy(b, 1000)507}508func BenchmarkWrite10000RowsViaInsert(b *testing.B) {509 benchmarkWriteNRowsViaInsert(b, 10000)510}511func BenchmarkWrite10000RowsViaMultiInsert(b *testing.B) {512 benchmarkWriteNRowsViaMultiInsert(b, 10000)513}514func BenchmarkWrite10000RowsViaCopy(b *testing.B) {515 benchmarkWriteNRowsViaCopy(b, 10000)516}517func BenchmarkMultipleQueriesNonBatch(b *testing.B) {518 config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5}519 pool, err := pgx.NewConnPool(config)520 if err != nil {521 b.Fatalf("Unable to create connection pool: %v", err)522 }523 defer pool.Close()524 queryCount := 3525 b.ResetTimer()526 for i := 0; i < b.N; i++ {527 for j := 0; j < queryCount; j++ {528 rows, err := pool.Query("select n from generate_series(0, 5) n")529 if err != nil {...

Full Screen

Full Screen

UserConnFSM.go

Source:UserConnFSM.go Github

copy

Full Screen

...46 panic("JOINABLE GAMES MARSHAL ERROR")47 }48 message := append(sm10Joinable, joinableGames...)49 message = append(message, '\n')50 (*userConnFSM.clientConn).Write(message)51 case cmHostGame:52 userConnFSM.HostGame()53 case cmJoinGame:54 // acquire potential game55 hostID := string(message[2:])56 hostIDtoGameLock.Lock()57 defer hostIDtoGameLock.Unlock()58 game := hostIDToGame[hostID]59 // check if game has been deleted60 if game == nil {61 (*userConnFSM.clientConn).Write(smJoinFailedInactive)62 return63 }64 // check if someone else joined65 if game.clientConn != nil {66 (*userConnFSM.clientConn).Write(smJoinFailedOtherJoined)67 return68 }69 // set up game now that both players in it70 userConnFSM.game = game71 game.lock.Lock()72 game.clientConn = userConnFSM73 game.state = gsBothScanning74 startScanningMessage := append(smStartScanning, []byte(userConnFSM.id)...)75 startScanningMessage = append(startScanningMessage, '\n')76 (*userConnFSM.clientConn).Write(startScanningMessage)77 (*game.hostConn.clientConn).Write(startScanningMessage)78 userConnFSM.state = usScanning79 game.hostConn.state = usScanning80 case cmReset:81 userConnFSM.dellocateGame()82 }83}84func scanningHandler(userConnFSM *UserConnFSM, message []byte) {85 switch message[0] {86 case cmDoneScanning:87 if userConnFSM.game.state == gsOneScanned {88 userConnFSM.game.state = gsCountDown89 userConnFSM.game.clientConn.state = usCountDown90 userConnFSM.game.hostConn.state = usCountDown91 (*userConnFSM.opponent().clientConn).Write(smCountDownStart)92 (*userConnFSM.clientConn).Write(smCountDownStart)93 go userConnFSM.game.startGameLoop()94 } else {95 userConnFSM.game.state = gsOneScanned96 (*userConnFSM.opponent().clientConn).Write(smOpponentScanned)97 }98 case cmReset:99 userConnFSM.dellocateGame()100 }101}102func countdownHandler(userConnFSM *UserConnFSM, message []byte) {103 switch message[0] {104 case cmReset:105 userConnFSM.dellocateGame()106 }107}108func inGameHandler(userConnFSM *UserConnFSM, message []byte) {109 switch message[0] {110 case cmAccelerated:111 var accelerationVector Vector112 err := json.Unmarshal(message[2:], &accelerationVector)113 if err != nil {114 panic("FATAL inGameHandler ERROR")115 }116 if userConnFSM.isHost() {117 userConnFSM.game.hostBall.accAccum.adding(accelerationVector)118 } else {119 userConnFSM.game.clientBall.accAccum.adding(accelerationVector)120 }121 case cmReset:122 userConnFSM.dellocateGame()123 }124}125func postGameHandler(userConnFSM *UserConnFSM, message []byte) {126 switch message[0] {127 case cmPlayAgain:128 if userConnFSM.game.state == gsOnePlayAgain {129 userConnFSM.game.state = gsCountDown130 userConnFSM.game.clientConn.state = usCountDown131 userConnFSM.game.hostConn.state = usCountDown132 (*userConnFSM.opponent().clientConn).Write(smCountDownStart)133 (*userConnFSM.clientConn).Write(smCountDownStart)134 go userConnFSM.game.startGameLoop()135 } else {136 userConnFSM.game.state = gsOnePlayAgain137 (*userConnFSM.opponent().clientConn).Write(smOpponentPlayAgain)138 }139 case cmReset:140 userConnFSM.dellocateGame()141 }142}...

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 conn, err := net.Dial("tcp", "localhost:8080")4 if err != nil {5 fmt.Println("Error connecting to server")6 }7 for {8 reader := bufio.NewReader(os.Stdin)9 fmt.Print("Text to send: ")10 text, _ := reader.ReadString('11 fmt.Fprintf(conn, text + "12 message, _ := bufio.NewReader(conn).ReadString('13 fmt.Print("Message from server: " + message)14 }15}16import (17func main() {18 conn, err := net.Dial("tcp", "localhost:8080")19 if err != nil {20 fmt.Println("Error connecting to server")21 }22 for {23 message, _ := bufio.NewReader(conn).ReadString('24 fmt.Print("Message from server: " + message)25 reader := bufio.NewReader(os.Stdin)26 fmt.Print("Text to send: ")27 text, _ := reader.ReadString('28 fmt.Fprintf(conn, text + "29 }30}31import (32func main() {33 conn, err := net.Dial("tcp", "localhost:8080")34 if err != nil {35 fmt.Println("Error connecting to server")36 }37 for {38 message, _ := bufio.NewReader(conn).ReadString('39 fmt.Print("Message from server: " + message)40 reader := bufio.NewReader(os.Stdin)41 fmt.Print("Text to send: ")42 text, _ := reader.ReadString('43 fmt.Fprintf(conn, text + "44 }45}46import (47func main() {48 conn, err := net.Dial("tcp", "localhost:8080")49 if err != nil {

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 conn, err := net.Dial("tcp", "localhost:8080")4 if err != nil {5 fmt.Println(err)6 }7 defer conn.Close()8 fmt.Fprintln(conn, "I dialed you.")9}10import (11func main() {12 ln, err := net.Listen("tcp", ":8080")13 if err != nil {14 fmt.Println(err)15 }16 defer ln.Close()17 for {18 conn, err := ln.Accept()19 if err != nil {20 fmt.Println(err)21 }22 go handle(conn)23 }24}25func handle(conn net.Conn) {26 scanner := bufio.NewScanner(conn)27 for scanner.Scan() {28 ln := scanner.Text()29 fmt.Println(ln)30 }31 defer conn.Close()32 fmt.Println("Code got here.")33}34import (35func main() {36 ln, err := net.Listen("tcp", ":8080")37 if err != nil {38 fmt.Println(err)39 }40 defer ln.Close()41 for {42 conn, err := ln.Accept()43 if err != nil {44 fmt.Println(err)45 }46 go handle(conn)47 }48}49func handle(conn net.Conn) {50 scanner := bufio.NewScanner(conn)51 for scanner.Scan() {52 ln := scanner.Text()53 fmt.Println(ln)54 fmt.Fprintf(conn, "I heard you say: %s55 if strings.TrimSpace(ln) == "I am leaving" {56 fmt.Println("Code got here.")57 }58 }59 defer conn.Close()60 fmt.Println("Code got here.")61}

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