Last active
August 29, 2015 14:02
-
-
Save anton-povarov/1933714a4dcec96bc0ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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() { | |
var conn mysql.Conn = mysql.New("tcp", "", "host:port", "username", "password") | |
conn.SetTimeout(2 * time.Second) | |
defer conn.Close() | |
for { | |
<-work_chan | |
if !conn.IsConnected() { | |
conn.Reconnect() | |
} | |
res, _ := conn.Start("show processlist") | |
row := res.MakeRow() | |
for { | |
err := res.ScanRow(row) | |
if err == io.EOF { | |
break | |
} | |
// _, _ = 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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment