hello.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "os"
  7. "runtime"
  8. "sort"
  9. "strconv"
  10. "database/sql"
  11. "github.com/gin-gonic/gin"
  12. _ "github.com/go-sql-driver/mysql"
  13. )
  14. const (
  15. // Database
  16. worldSelect = "SELECT id, randomNumber FROM World WHERE id = ?"
  17. worldUpdate = "UPDATE World SET randomNumber = ? WHERE id = ?"
  18. fortuneSelect = "SELECT id, message FROM Fortune;"
  19. worldRowCount = 10000
  20. maxConnectionCount = 256
  21. )
  22. type World struct {
  23. Id uint16 `json:"id"`
  24. RandomNumber uint16 `json:"randomNumber"`
  25. }
  26. type Fortune struct {
  27. Id uint16 `json:"id"`
  28. Message string `json:"message"`
  29. }
  30. type Fortunes []*Fortune
  31. func (s Fortunes) Len() int { return len(s) }
  32. func (s Fortunes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  33. type ByMessage struct{ Fortunes }
  34. func (s ByMessage) Less(i, j int) bool { return s.Fortunes[i].Message < s.Fortunes[j].Message }
  35. var (
  36. // Database
  37. worldStatement *sql.Stmt
  38. fortuneStatement *sql.Stmt
  39. updateStatement *sql.Stmt
  40. )
  41. func parseQueries(c *gin.Context) int {
  42. n, err := strconv.Atoi(c.Request.URL.Query().Get("queries"))
  43. if err != nil {
  44. n = 1
  45. } else if n < 1 {
  46. n = 1
  47. } else if n > 500 {
  48. n = 500
  49. }
  50. return n
  51. }
  52. /// Test 1: JSON serialization
  53. func json(c *gin.Context) {
  54. c.JSON(200, gin.H{"message": "Hello, World!"})
  55. }
  56. /// Test 2: Single database query
  57. func db(c *gin.Context) {
  58. var world World
  59. err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
  60. if err != nil {
  61. c.Fail(500, err)
  62. }
  63. c.JSON(200, &world)
  64. }
  65. /// Test 3: Multiple database queries
  66. func dbs(c *gin.Context) {
  67. numQueries := parseQueries(c)
  68. worlds := make([]World, numQueries)
  69. for i := 0; i < numQueries; i++ {
  70. err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&worlds[i].Id, &worlds[i].RandomNumber)
  71. if err != nil {
  72. c.Fail(500, err)
  73. }
  74. }
  75. c.JSON(200, &worlds)
  76. }
  77. /// Test 4: Fortunes
  78. func fortunes(c *gin.Context) {
  79. rows, err := fortuneStatement.Query()
  80. if err != nil {
  81. c.Fail(500, err)
  82. }
  83. fortunes := make(Fortunes, 0, 16)
  84. for rows.Next() { //Fetch rows
  85. fortune := Fortune{}
  86. if err := rows.Scan(&fortune.Id, &fortune.Message); err != nil {
  87. c.Fail(500, err)
  88. }
  89. fortunes = append(fortunes, &fortune)
  90. }
  91. fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})
  92. sort.Sort(ByMessage{fortunes})
  93. c.HTML(200, "fortune.html", fortunes)
  94. }
  95. /// Test 5: Database updates
  96. func update(c *gin.Context) {
  97. numQueries := parseQueries(c)
  98. world := make([]World, numQueries)
  99. for i := 0; i < numQueries; i++ {
  100. if err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber); err != nil {
  101. c.Fail(500, err)
  102. }
  103. world[i].RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
  104. if _, err := updateStatement.Exec(world[i].RandomNumber, world[i].Id); err != nil {
  105. c.Fail(500, err)
  106. }
  107. }
  108. c.JSON(200, world)
  109. }
  110. /// Test 6: plaintext
  111. func plaintext(c *gin.Context) {
  112. c.String(200, "Hello, World!")
  113. }
  114. func main() {
  115. r := gin.New()
  116. r.LoadHTMLFiles("fortune.html")
  117. r.GET("/json", json)
  118. r.GET("/db", db)
  119. r.GET("/dbs", dbs)
  120. r.GET("/fortunes", fortunes)
  121. r.GET("/update", update)
  122. r.GET("/plaintext", plaintext)
  123. r.Run(":8080")
  124. }
  125. func init() {
  126. runtime.GOMAXPROCS(runtime.NumCPU())
  127. dsn := "benchmarkdbuser:benchmarkdbpass@tcp(%s:3306)/hello_world"
  128. dbhost := os.Getenv("DBHOST")
  129. db, err := sql.Open("mysql", fmt.Sprintf(dsn, dbhost))
  130. if err != nil {
  131. log.Fatalf("Error opening database: %v", err)
  132. }
  133. db.SetMaxIdleConns(maxConnectionCount)
  134. worldStatement, err = db.Prepare(worldSelect)
  135. if err != nil {
  136. log.Fatal(err)
  137. }
  138. fortuneStatement, err = db.Prepare(fortuneSelect)
  139. if err != nil {
  140. log.Fatal(err)
  141. }
  142. updateStatement, err = db.Prepare(worldUpdate)
  143. if err != nil {
  144. log.Fatal(err)
  145. }
  146. }