Commit f66cb485 authored by dagal's avatar dagal
Browse files

basic benchmarking

parent 91eafad9
package main
import (
"time"
"log"
)
func BenchAll() {
backends := map[string]queryBackend{"ctr": ctrDummy, "db": dbDummy}
log.Printf("Bench StopStats over %d stops", len(stops))
for name, b := range backends {
usec := benchStopStats(b)
log.Printf("StopStats %s: %d usec ( %.2f usec/query )", name, usec, float64(usec)/float64(len(stops)))
}
}
func benchStopStats(b queryBackend) int64 {
benchStart := time.Now()
var measuredTime int64
for _, s := range stops {
benchStart = time.Now()
b.GetStopStats(s,0,0)
measuredTime += time.Since(benchStart).Microseconds()
}
return measuredTime
}
\ No newline at end of file
......@@ -47,6 +47,9 @@ func main() {
router.GET("/stop_stats/:id", GetStopStats)
router.GET("/xy", GetXY)
router.POST("/xy_area", GetXYArea)
BenchAll()
err := router.Run(":8088")
LogAndQuit(err)
}
......
......@@ -33,11 +33,12 @@ func (d tdb) GetStopStats(s Stop, fromTime uint32, toTime uint32) *geojson.Featu
rows, err := db.Raw("SELECT line_id FROM line_stop WHERE stop_id = ?", s.ID).Rows()
LogAndPanic(err)
defer rows.Close()
lines := make([]string, 0)
var line_id string
var board, start, end uint32
defer rows.Close()
for rows.Next() {
rows.Scan(&line_id)
lines = append(lines, line_id)
......@@ -45,30 +46,49 @@ func (d tdb) GetStopStats(s Stop, fromTime uint32, toTime uint32) *geojson.Featu
for _, l := range lines {
// FIXME missing a join with line_stops for avg time!
var fromJourney, toJourney, lboard, lstart, lend uint32
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? and start_time >= to_timestamp(?) at time zone 'UTC'", l, fromTime).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? and start_time < to_timestamp(?) at time zone 'UTC'", l, toTime).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&toJourney)
var fromJourney, toJourney, lboard, lstart, lend uint32
if fromTime == 0 && toTime == 0 {
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? ", l).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? ", l).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&toJourney)
} else {
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? and start_time >= to_timestamp(?) at time zone 'UTC'", l, fromTime).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? and start_time < to_timestamp(?) at time zone 'UTC'", l, toTime).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&toJourney)
}
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ?", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lboard)
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = 0", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lstart)
rows, err = db.Raw("select count(1) from stage s where alight_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = (select max(num_stage) from stage where trip_id = s.trip_id)", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lend)
......
package main
import (
"github.com/jinzhu/gorm"
"github.com/gin-gonic/gin"
"log"
"database/sql"
"strconv"
"strings"
)
......@@ -52,3 +54,9 @@ func Atoi32(s string, c *gin.Context) uint32 {
LogAndPanic2(err, c)
return uint32(i)
}
func SQLPanic(db *gorm.DB, query string, values... interface{}) *sql.Rows {
rows, err := db.Raw(query, values...).Rows()
LogAndPanic(err)
return rows
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment