|
@@ -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) }
|