Browse Source

Merge branch 'master' of github.com:TechEmpower/FrameworkBenchmarks

Patrick Falls 12 years ago
parent
commit
c396d19f78
2 changed files with 13 additions and 18 deletions
  1. 12 17
      go/src/hello/hello.go
  2. 1 1
      ringojs-convinient/app/views.js

+ 12 - 17
go/src/hello/hello.go

@@ -3,7 +3,6 @@ package main
 import (
 	"database/sql"
 	"encoding/json"
-	_ "github.com/go-sql-driver/mysql"
 	"html/template"
 	"log"
 	"math/rand"
@@ -12,9 +11,11 @@ import (
 	"sort"
 	"strconv"
 	"sync"
+
+	_ "github.com/go-sql-driver/mysql"
 )
 
-type MessageStruct struct {
+type Message struct {
 	Message string
 }
 
@@ -75,9 +76,7 @@ func main() {
 
 func jsonHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/javascript")
-	j, _ := json.Marshal(&MessageStruct{"Hello, world"})
-	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
-	w.Write(j)
+	json.NewEncoder(w).Encode(&Message{"Hello, world"})
 }
 
 func worldHandler(w http.ResponseWriter, r *http.Request) {
@@ -87,7 +86,10 @@ func worldHandler(w http.ResponseWriter, r *http.Request) {
 	}
 	ww := make([]World, n)
 	if n == 1 {
-		worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[0].Id, &ww[0].RandomNumber)
+		err := worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[0].Id, &ww[0].RandomNumber)
+		if err != nil {
+			log.Fatalf("Error scanning world row: %v", err)
+		}
 	} else {
 		var wg sync.WaitGroup
 		wg.Add(n)
@@ -102,30 +104,23 @@ func worldHandler(w http.ResponseWriter, r *http.Request) {
 		}
 		wg.Wait()
 	}
-	j, _ := json.Marshal(ww)
 	w.Header().Set("Content-Type", "application/json")
-	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
-	w.Write(j)
+	json.NewEncoder(w).Encode(ww)
 }
 
 func fortuneHandler(w http.ResponseWriter, r *http.Request) {
-	fortunes := make([]*Fortune, 0, 16)
-
-	//Execute the query
 	rows, err := fortuneStatement.Query()
 	if err != nil {
 		log.Fatalf("Error preparing statement: %v", err)
 	}
 
-	i := 0
-	var fortune *Fortune
+	fortunes := make([]*Fortune, 0, 16)
 	for rows.Next() { //Fetch rows
-		fortune = new(Fortune)
-		if err = rows.Scan(&fortune.Id, &fortune.Message); err != nil {
+		fortune := new(Fortune)
+		if err := rows.Scan(&fortune.Id, &fortune.Message); err != nil {
 			log.Fatalf("Error scanning fortune row: %v", err)
 		}
 		fortunes = append(fortunes, fortune)
-		i++
 	}
 	fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})
 

+ 1 - 1
ringojs-convinient/app/views.js

@@ -29,7 +29,7 @@ app.get('/db/:queries?', function(request, queries) {
 });
 
 app.get('/fortune', function() {
-   var fortunes = models.Fortune.all();
+   var fortunes = models.store.query('select Fortune.* from Fortune');
    fortunes.push({
       _id: 0,
       message: 'Additional fortune added at request time.'