server.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //go:generate qtc -dir=src/templates
  2. package main
  3. import (
  4. "database/sql"
  5. "encoding/json"
  6. "flag"
  7. "log"
  8. "math/rand"
  9. "net"
  10. "os"
  11. "os/exec"
  12. "runtime"
  13. "sort"
  14. "sync"
  15. _ "github.com/go-sql-driver/mysql"
  16. "github.com/valyala/fasthttp"
  17. "github.com/valyala/fasthttp/reuseport"
  18. "templates"
  19. )
  20. type JSONResponse struct {
  21. Message string `json:"message"`
  22. }
  23. type World struct {
  24. Id uint16 `json:"id"`
  25. RandomNumber uint16 `json:"randomNumber"`
  26. }
  27. const (
  28. connectionString = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
  29. worldRowCount = 10000
  30. maxConnectionCount = 40
  31. )
  32. var (
  33. worldSelectStmt *sql.Stmt
  34. worldUpdateStmt *sql.Stmt
  35. fortuneSelectStmt *sql.Stmt
  36. db *sql.DB
  37. )
  38. var (
  39. listenAddr = flag.String("listenAddr", ":8080", "Address to listen to")
  40. prefork = flag.Bool("prefork", false, "use prefork")
  41. child = flag.Bool("child", false, "is child proc")
  42. )
  43. func main() {
  44. flag.Parse()
  45. var err error
  46. if db, err = sql.Open("mysql", connectionString); err != nil {
  47. log.Fatalf("Error opening database: %s", err)
  48. }
  49. if err = db.Ping(); err != nil {
  50. log.Fatalf("Cannot connect to db: %s", err)
  51. }
  52. dbConnCount := maxConnectionCount
  53. if *prefork {
  54. dbConnCount = (dbConnCount + runtime.NumCPU() - 1) / runtime.NumCPU()
  55. }
  56. db.SetMaxIdleConns(dbConnCount)
  57. db.SetMaxOpenConns(dbConnCount)
  58. worldSelectStmt = mustPrepare(db, "SELECT id, randomNumber FROM World WHERE id = ?")
  59. worldUpdateStmt = mustPrepare(db, "UPDATE World SET randomNumber = ? WHERE id = ?")
  60. fortuneSelectStmt = mustPrepare(db, "SELECT id, message FROM Fortune")
  61. s := &fasthttp.Server{
  62. Handler: mainHandler,
  63. Name: "go",
  64. }
  65. ln := getListener()
  66. if err = s.Serve(ln); err != nil {
  67. log.Fatalf("Error when serving incoming connections: %s", err)
  68. }
  69. }
  70. func mainHandler(ctx *fasthttp.RequestCtx) {
  71. path := ctx.Path()
  72. switch string(path) {
  73. case "/plaintext":
  74. plaintextHandler(ctx)
  75. case "/json":
  76. jsonHandler(ctx)
  77. case "/db":
  78. dbHandler(ctx)
  79. case "/queries":
  80. queriesHandler(ctx)
  81. case "/fortune":
  82. fortuneHandler(ctx)
  83. case "/update":
  84. updateHandler(ctx)
  85. default:
  86. ctx.Error("unexpected path", fasthttp.StatusBadRequest)
  87. }
  88. }
  89. // Test 1: JSON serialization
  90. func jsonHandler(ctx *fasthttp.RequestCtx) {
  91. r := jsonResponsePool.Get().(*JSONResponse)
  92. r.Message = "Hello, World!"
  93. jsonMarshal(ctx, r)
  94. jsonResponsePool.Put(r)
  95. }
  96. var jsonResponsePool = &sync.Pool{
  97. New: func() interface{} {
  98. return &JSONResponse{}
  99. },
  100. }
  101. // Test 2: Single database query
  102. func dbHandler(ctx *fasthttp.RequestCtx) {
  103. var w World
  104. fetchRandomWorld(&w)
  105. jsonMarshal(ctx, &w)
  106. }
  107. // Test 3: Multiple database queries
  108. func queriesHandler(ctx *fasthttp.RequestCtx) {
  109. n := getQueriesCount(ctx)
  110. worlds := make([]World, n)
  111. for i := 0; i < n; i++ {
  112. fetchRandomWorld(&worlds[i])
  113. }
  114. jsonMarshal(ctx, worlds)
  115. }
  116. // Test 4: Fortunes
  117. func fortuneHandler(ctx *fasthttp.RequestCtx) {
  118. rows, err := fortuneSelectStmt.Query()
  119. if err != nil {
  120. log.Fatalf("Error selecting db data: %v", err)
  121. }
  122. fortunes := make([]templates.Fortune, 0, 16)
  123. for rows.Next() {
  124. var f templates.Fortune
  125. if err := rows.Scan(&f.ID, &f.Message); err != nil {
  126. log.Fatalf("Error scanning fortune row: %s", err)
  127. }
  128. fortunes = append(fortunes, f)
  129. }
  130. rows.Close()
  131. fortunes = append(fortunes, templates.Fortune{Message: "Additional fortune added at request time."})
  132. sort.Sort(FortunesByMessage(fortunes))
  133. ctx.SetContentType("text/html; charset=utf-8")
  134. templates.WriteFortunePage(ctx, fortunes)
  135. }
  136. // Test 5: Database updates
  137. func updateHandler(ctx *fasthttp.RequestCtx) {
  138. n := getQueriesCount(ctx)
  139. worlds := make([]World, n)
  140. for i := 0; i < n; i++ {
  141. w := &worlds[i]
  142. fetchRandomWorld(w)
  143. w.RandomNumber = uint16(randomWorldNum())
  144. }
  145. // sorting is required for insert deadlock prevention.
  146. sort.Sort(WorldsByID(worlds))
  147. txn, err := db.Begin()
  148. if err != nil {
  149. log.Fatalf("Error starting transaction: %s", err)
  150. }
  151. stmt := txn.Stmt(worldUpdateStmt)
  152. for i := 0; i < n; i++ {
  153. w := &worlds[i]
  154. if _, err := stmt.Exec(w.RandomNumber, w.Id); err != nil {
  155. log.Fatalf("Error updating world row %d: %s", i, err)
  156. }
  157. }
  158. if err = txn.Commit(); err != nil {
  159. log.Fatalf("Error when commiting world rows: %s", err)
  160. }
  161. jsonMarshal(ctx, worlds)
  162. }
  163. // Test 6: Plaintext
  164. func plaintextHandler(ctx *fasthttp.RequestCtx) {
  165. ctx.SetContentType("text/plain")
  166. ctx.WriteString("Hello, World!")
  167. }
  168. func jsonMarshal(ctx *fasthttp.RequestCtx, v interface{}) {
  169. ctx.SetContentType("application/json")
  170. if err := json.NewEncoder(ctx).Encode(v); err != nil {
  171. log.Fatalf("error in json.Encoder.Encode: %s", err)
  172. }
  173. }
  174. func fetchRandomWorld(w *World) {
  175. n := randomWorldNum()
  176. if err := worldSelectStmt.QueryRow(n).Scan(&w.Id, &w.RandomNumber); err != nil {
  177. log.Fatalf("Error scanning world row: %s", err)
  178. }
  179. }
  180. func randomWorldNum() int {
  181. return rand.Intn(worldRowCount) + 1
  182. }
  183. func getQueriesCount(ctx *fasthttp.RequestCtx) int {
  184. n := ctx.QueryArgs().GetUintOrZero("queries")
  185. if n < 1 {
  186. n = 1
  187. } else if n > 500 {
  188. n = 500
  189. }
  190. return n
  191. }
  192. type FortunesByMessage []templates.Fortune
  193. func (s FortunesByMessage) Len() int { return len(s) }
  194. func (s FortunesByMessage) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  195. func (s FortunesByMessage) Less(i, j int) bool { return s[i].Message < s[j].Message }
  196. type WorldsByID []World
  197. func (w WorldsByID) Len() int { return len(w) }
  198. func (w WorldsByID) Swap(i, j int) { w[i], w[j] = w[j], w[i] }
  199. func (w WorldsByID) Less(i, j int) bool { return w[i].Id < w[j].Id }
  200. func mustPrepare(db *sql.DB, query string) *sql.Stmt {
  201. stmt, err := db.Prepare(query)
  202. if err != nil {
  203. log.Fatalf("Error when preparing statement %q: %s", query, err)
  204. }
  205. return stmt
  206. }
  207. func getListener() net.Listener {
  208. if !*prefork {
  209. runtime.GOMAXPROCS(runtime.NumCPU())
  210. ln, err := net.Listen("tcp4", *listenAddr)
  211. if err != nil {
  212. log.Fatal(err)
  213. }
  214. return ln
  215. }
  216. if !*child {
  217. children := make([]*exec.Cmd, runtime.NumCPU())
  218. for i := range children {
  219. children[i] = exec.Command(os.Args[0], "-prefork", "-child")
  220. children[i].Stdout = os.Stdout
  221. children[i].Stderr = os.Stderr
  222. if err := children[i].Start(); err != nil {
  223. log.Fatal(err)
  224. }
  225. }
  226. for _, ch := range children {
  227. if err := ch.Wait(); err != nil {
  228. log.Print(err)
  229. }
  230. }
  231. os.Exit(0)
  232. panic("unreachable")
  233. }
  234. runtime.GOMAXPROCS(1)
  235. ln, err := reuseport.Listen("tcp4", *listenAddr)
  236. if err != nil {
  237. log.Fatal(err)
  238. }
  239. return ln
  240. }