Skip to content

Instantly share code, notes, and snippets.

@phrozen
Created April 16, 2012 01:00
Show Gist options
  • Save phrozen/2395675 to your computer and use it in GitHub Desktop.
Save phrozen/2395675 to your computer and use it in GitHub Desktop.
SQLite benchmark program in Go
package main
import (
"bitbucket.org/jpoirier/cpu"
"database/sql"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"math/rand"
"os"
"time"
)
const (
URL = "http://sqlitemark.heroku.com"
VERSION = "1.0"
SINGLE = "single.db"
MANY = "many.db"
)
type Benchmark struct {
Values int
Tables int
Generate float64
SizeSingle int
CreateOne float64
InsertOne float64
SelectOne float64
AssertOne bool
SizeMany int
CreateMany float64
InsertMany float64
SelectMany float64
AssertMany bool
Processor string
Nickname string
}
func Run(values, tables int) *Benchmark {
if values%tables != 0 {
panic("Cannot divide values into tables exactly.")
}
var start time.Time
bench := &Benchmark{Values: values, Tables: tables}
bench.Processor = cpu.ProcessorFamily
/********************************************************************/
// GENERATING RANDOM ARRAYS
fmt.Print("generate: ")
start = time.Now()
oax := make([]float32, values)
oay := make([]float32, values)
oaz := make([]float32, values)
ssx := make([]float32, values)
ssy := make([]float32, values)
ssz := make([]float32, values)
//smx := make([]float64, values)
//smy := make([]float64, values)
//smz := make([]float64, values)
for i := 0; i < values; i++ {
oax[i] = rand.Float32()
oay[i] = rand.Float32()
oaz[i] = rand.Float32()
}
bench.Generate = float64(time.Since(start).Nanoseconds() / 1000000.0)
fmt.Println(bench.Generate, "[ms]")
/********************************************************************/
// OPENING SINGLE TABLE DATABASE
fmt.Println("SINGLE: ", SINGLE)
os.Remove(SINGLE)
dbs, err := sql.Open("sqlite3", SINGLE)
if err != nil {
panic(err)
}
defer dbs.Close()
/********************************************************************/
// CREATE A SINGLE TABLE
fmt.Print(" create: ")
start = time.Now()
table := "CREATE TABLE vertex (id INTEGER PRIMARY KEY NOT NULL," +
" x REAL NOT NULL, y REAL NOT NULL, z REAL NOT NULL)"
_, err = dbs.Exec(table)
if err != nil {
panic(err)
}
bench.CreateOne = float64(time.Since(start).Nanoseconds() / 1000000.0)
fmt.Println(bench.CreateOne, "[ms]")
/********************************************************************/
// INSERT VALUES INTO TABLE
fmt.Print(" insert: ")
start = time.Now()
tx, err := dbs.Begin()
if err != nil {
panic(err)
}
stmt, err := tx.Prepare("INSERT INTO vertex VALUES(NULL, ?, ?, ?)")
if err != nil {
panic(err)
}
defer stmt.Close()
for i := 0; i < values; i++ {
_, err = stmt.Exec(oax[i], oay[i], oaz[i])
if err != nil {
panic(err)
}
}
tx.Commit()
bench.InsertOne = float64(time.Since(start).Nanoseconds() / 1000000.0)
fmt.Println(bench.InsertOne, "[ms]")
/********************************************************************/
// SELECT VALUES FROM TABLE
fmt.Print(" select: ")
start = time.Now()
rows, err := dbs.Query("SELECT * FROM vertex")
if err != nil {
panic(err)
}
defer rows.Close()
for i := 0; rows.Next(); i++ {
rows.Scan(nil, &ssx[i], &ssy[i], &ssz[i])
}
rows.Close()
bench.SelectOne = float64(time.Since(start).Nanoseconds() / 1000000.0)
fmt.Println(bench.SelectOne, "[ms]")
return bench
}
func main() {
data, err := json.Marshal(Run(1000000, 1000))
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment