Browse Source

go update test

Patrick Falls 12 years ago
parent
commit
f27f7fe8a1
3 changed files with 41 additions and 1 deletions
  1. 1 1
      framework_test.py
  2. 1 0
      go/benchmark_config
  3. 39 0
      go/src/hello/hello.go

+ 1 - 1
framework_test.py

@@ -155,7 +155,7 @@ class FrameworkTest:
     # Update
     # Update
     try:
     try:
       print "VERIFYING Update (" + self.update_url + "2) ..."
       print "VERIFYING Update (" + self.update_url + "2) ..."
-      url = self.benchmarker.generate_url(self.update_url, self.port)
+      url = self.benchmarker.generate_url(self.update_url + "2", self.port)
       subprocess.check_call(["curl", "-f", url])
       subprocess.check_call(["curl", "-f", url])
       print ""
       print ""
       self.update_url_passed = True
       self.update_url_passed = True

+ 1 - 0
go/benchmark_config

@@ -7,6 +7,7 @@
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/db?queries=",
       "query_url": "/db?queries=",
       "fortune_url": "/fortune",
       "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
       "port": 8080,
       "port": 8080,
       "sort": 27
       "sort": 27
     }
     }

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

@@ -31,6 +31,7 @@ type Fortune struct {
 const (
 const (
 	ConnectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8"
 	ConnectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8"
 	WorldSelect        = "SELECT id, randomNumber FROM World where id = ?"
 	WorldSelect        = "SELECT id, randomNumber FROM World where id = ?"
+	WorldUpdate				 = "UPDATE World SET randomNumber = ? where id = ?"
 	FortuneSelect      = "SELECT id, message FROM Fortune;"
 	FortuneSelect      = "SELECT id, message FROM Fortune;"
 	WorldRowCount      = 10000
 	WorldRowCount      = 10000
 	MaxConnectionCount = 100
 	MaxConnectionCount = 100
@@ -41,6 +42,7 @@ var (
 
 
 	worldStatement    *sql.Stmt
 	worldStatement    *sql.Stmt
 	fortuneStatement *sql.Stmt
 	fortuneStatement *sql.Stmt
+	updateStatement		*sql.Stmt
 )
 )
 
 
 func main() {
 func main() {
@@ -59,10 +61,15 @@ func main() {
 	if err != nil {
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 	}
+	updateStatement, err = db.Prepare(WorldUpdate)
+	if err != nil {
+		log.Fatal(err)
+	}
 
 
 	http.HandleFunc("/db", worldHandler)
 	http.HandleFunc("/db", worldHandler)
 	http.HandleFunc("/json", jsonHandler)
 	http.HandleFunc("/json", jsonHandler)
 	http.HandleFunc("/fortune", fortuneHandler)
 	http.HandleFunc("/fortune", fortuneHandler)
+	http.HandleFunc("/update", updateHandler)
 	http.ListenAndServe(":8080", nil)
 	http.ListenAndServe(":8080", nil)
 }
 }
 
 
@@ -129,6 +136,38 @@ func fortuneHandler(w http.ResponseWriter, r *http.Request) {
 	}
 	}
 }
 }
 
 
+func updateHandler(w http.ResponseWriter, r *http.Request) {
+	n := 1
+	if nStr := r.URL.Query().Get("queries"); len(nStr) != 0 {
+		n, _ = strconv.Atoi(nStr)
+	}
+	ww := make([]World, n)
+	if n == 1 {
+		worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[0].Id, &ww[0].RandomNumber)
+		ww[0].RandomNumber = rand.Intn(WorldRowCount) + 1
+		updateStatement.Exec(ww[0].RandomNumber, ww[0].Id)
+	} else {
+		var wg sync.WaitGroup
+		wg.Add(n)
+		for i := 0; i < n; i++ {
+			go func(i int) {
+				err := worldStatement.QueryRow(rand.Intn(WorldRowCount)+1).Scan(&ww[i].Id, &ww[i].RandomNumber)
+				ww[i].RandomNumber = rand.Intn(WorldRowCount) + 1
+				updateStatement.Exec(ww[i].RandomNumber, ww[i].Id)
+				if err != nil {
+					log.Fatalf("Error scanning world row: %v", err)
+				}
+				wg.Done()
+			}(i)
+		}
+		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)
+}
+
 type Fortunes []*Fortune
 type Fortunes []*Fortune
 
 
 func (s Fortunes) Len() int      { return len(s) }
 func (s Fortunes) Len() int      { return len(s) }