File: parallel.go

package info (click to toggle)
golang-github-ziutek-mymysql 1.5.4%2Bgit20170206.23.0582bcf-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 388 kB
  • sloc: makefile: 8; sh: 2
file content (96 lines) | stat: -rw-r--r-- 1,775 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This file is there temporary and it isn't any example of how to use mymysql.
package main

import (
	"io"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/ziutek/mymysql/mysql"
	_ "github.com/ziutek/mymysql/native"
)

const (
	n_sends      = 3 * 1000
	n_goroutines = 100
)

func checkErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	work_chan := make(chan bool)
	sends_chan := make(chan bool)
	results_chan := make(chan bool, n_sends)

	signal_chan := make(chan os.Signal, 1)
	signal.Notify(signal_chan, syscall.SIGINT)

	for i := 0; i < n_goroutines; i++ {
		go func() {
			conn := mysql.New(
				"tcp", "", "127.0.0.1:3306",
				"testuser", "TestPasswd9",
			)
			conn.SetTimeout(2 * time.Second)
			defer conn.Close()

			for {
				<-work_chan

				if !conn.IsConnected() {
					checkErr(conn.Reconnect())
				}

				res, err := conn.Start("show processlist")
				checkErr(err)
				row := res.MakeRow()
				for {
					err := res.ScanRow(row)
					if err == io.EOF {
						break
					}
					checkErr(err)
					// _, _ = row.ForceUint64(0), row.ForceUint(1)
				}

				// sleep_time := time.Duration(rand.Intn(10)) * time.Millisecond
				// time.Sleep(sleep_time)

				results_chan <- true
			}
		}()
	}

	go func() {
		for i := 0; i < n_sends; i++ {
			work_chan <- true
			sends_chan <- true
		}
	}()

	done_sends := 0
	ticker := time.NewTicker(1 * time.Second)

	for got_results := 0; got_results < n_sends; {
		select {
		case <-results_chan:
			got_results++
		case <-sends_chan:
			done_sends++
		case <-ticker.C:
			log.Printf("done %d sends, got %d results", done_sends, got_results)
		case <-signal_chan:
			panic("show me the goroutines")
		}
	}
	log.Printf("got all %d results", n_sends)

	// panic("show me the goroutines")
}