123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package main
- import (
- "database/sql"
- "fmt"
- "log"
- "math/rand"
- "sync"
- "time"
- _ "xugu_driver/xugu"
- )
- const createTableQuery = `
- CREATE TABLE IF NOT EXISTS test_table (
- id INT AUTO_INCREMENT PRIMARY KEY,
- value VARCHAR(255) NOT NULL
- );`
- const insertQuery = `INSERT INTO test_table (value) VALUES (?)`
- const selectQuery = `SELECT value FROM test_table WHERE id = ?`
- const concurrency = 10
- func init() {
- rand.Seed(time.Now().UnixNano())
- }
- func main() {
- db, err := sql.Open("xugusql", "IP=10.28.20.101;DB=SYSTEM;User=SYSDBA;PWD=SYSDBA;Port=5190;AUTO_COMMIT=on;CHAR_SET=UTF8")
- if err != nil {
- log.Fatal(err)
- }
-
- _, err = db.Exec(createTableQuery)
- if err != nil {
- log.Fatalf("Failed to create test table: %v", err)
- }
-
- var wg sync.WaitGroup
-
- var successCount, failureCount int64
- var mu sync.Mutex
-
- for i := 0; i < concurrency; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- for {
-
- if rand.Intn(2) == 0 {
-
- _, err := db.Exec(insertQuery, fmt.Sprintf("value-%d", rand.Int()))
- mu.Lock()
- if err != nil {
- failureCount++
- } else {
- successCount++
- }
- mu.Unlock()
- } else {
-
- id := rand.Intn(1000) + 1
- var value string
- err := db.QueryRow(selectQuery, id).Scan(&value)
- mu.Lock()
- if err != nil {
- failureCount++
- } else {
- successCount++
- }
- mu.Unlock()
- }
-
- mu.Lock()
- log.Printf("Success: %d, Failure: %d", successCount, failureCount)
- mu.Unlock()
-
- time.Sleep(100 * time.Millisecond)
- }
- }()
- }
-
- wg.Wait()
- defer db.Close()
- }
|