Browse Source

Merge branch 'master' of https://github.com/kardianos/FrameworkBenchmarks into kardianos-master

Patrick Falls 12 years ago
parent
commit
fd5f5f3255
2 changed files with 44 additions and 0 deletions
  1. 2 0
      go/benchmark_config
  2. 42 0
      go/src/hello/hello.go

+ 2 - 0
go/benchmark_config

@@ -4,6 +4,8 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
       "port": 8080,
       "sort": 27
     }

+ 42 - 0
go/src/hello/hello.go

@@ -5,11 +5,25 @@ import (
 	"net/http"
 	"runtime"
 	"strconv"
+
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+	"log"
+	"math/rand"
 )
 
+const DB_ROWS = 10000
+
+var db *sql.DB
+var q *sql.Stmt
+
 type MessageStruct struct {
 	Message string
 }
+type World struct {
+	Id           uint16 `json:"id"`
+	RandomNumber uint16 `json:"randomNumber"`
+}
 
 func hello(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/javascript")
@@ -18,8 +32,36 @@ func hello(w http.ResponseWriter, r *http.Request) {
 	w.Write(j)
 }
 
+func dbh(w http.ResponseWriter, r *http.Request) {
+	qnum := 1
+	qnumString := r.URL.Query().Get("queries")
+	if len(qnumString) != 0 {
+		qnum, _ = strconv.Atoi(qnumString)
+	}
+	ww := make([]World, qnum)
+	for i := 0; i < qnum; i++ {
+		q.QueryRow(rand.Intn(DB_ROWS)+1).Scan(&ww[i].Id, &ww[i].RandomNumber)
+	}
+	w.Header().Set("Content-Type", "application/javascript")
+	j, _ := json.Marshal(ww)
+	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
+	w.Write(j)
+}
+
 func main() {
+	var err error
 	runtime.GOMAXPROCS(runtime.NumCPU())
+
+	db, err = sql.Open("mysql", "benchmarkdbuser:benchmarkdbpass@/hello_world?charset=utf8")
+	if err != nil {
+		log.Fatalf("Error opening database: %s", err)
+	}
+	q, err = db.Prepare("SELECT * FROM World where id = ?;")
+	if err != nil {
+		log.Fatalf("Error preparing statement: %s", err)
+	}
+
 	http.HandleFunc("/json", hello)
+	http.HandleFunc("/db", dbh)
 	http.ListenAndServe(":8080", nil)
 }