Skip to content

Instantly share code, notes, and snippets.

@unixdj
Created December 1, 2012 20:19
Show Gist options
  • Save unixdj/4184712 to your computer and use it in GitHub Desktop.
Save unixdj/4184712 to your computer and use it in GitHub Desktop.
Go database/sql thrasher
/*
This program writes to a database concurrently from 16 goroutines.
To see the results sorted (GNU sort can't sort by two numeric fields):
sqlite3 the.db 'select * from tbl' \
| awk -F\| '{printf "%3d %5d\n", $1, $2}' | sort | less
Here's a little script that interprets the results:
sqlite3 the.db 'select * from tbl' | awk -F\| '
BEGIN {
for (i = 0; i < 16; i++)
arr[i] = -1
}
arr[$1] != $2-1 {
print $1 ": gap", arr[$1]+1, "-", $2-1
}
{
arr[$1] = $2
}
END {
for (i = 0; i < 16; i++)
print i ": last is", arr[i]
}'
*/
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"time"
)
var db *sql.DB
func thrash(n int) {
var i int
for {
_, err := db.Exec("insert into tbl (thread, seq) values (?, ?)", n, i)
if err != nil {
log.Printf("thread %d, loop %d: %v\n", n, i, err)
}
i++
}
}
func main() {
var err error
db, err = sql.Open("sqlite3", "the.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
for _, v := range []string{
"create table if not exists tbl (thread integer, seq integer)",
"delete from tbl",
} {
if _, err = db.Exec(v); err != nil {
log.Fatalf("%v: %v\n", v, err)
}
}
for i := 0; i < 16; i++ {
go thrash(i)
}
time.Sleep(time.Minute)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment