Created
June 7, 2014 16:53
-
-
Save chatman-media/ff0b380e18da012a5dda to your computer and use it in GitHub Desktop.
test simple app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "encoding/json" | |
| "log" | |
| "math/rand" | |
| "net/http" | |
| "runtime" | |
| // "strconv" | |
| // _ "github.com/lib/pq" | |
| _ "github.com/go-sql-driver/mysql" | |
| ) | |
| type Message struct { | |
| Message string `json:"message"` | |
| } | |
| type World struct { | |
| Id uint16 `json:"id"` | |
| num uint32 `json:"num"` | |
| } | |
| type Word struct { | |
| Id uint32 `json:"id"` | |
| name string `json:"name"` | |
| } | |
| // type Fortune struct { | |
| // Id uint16 `json:"id"` | |
| // Message string `json:"message"` | |
| // } | |
| const ( | |
| // Database | |
| connectionString = "root:dramaturg@tcp(localhost:3306)/rails_mysql_test" | |
| worldSelect = "SELECT id, num FROM worlds WHERE id = ?" | |
| wordSelect = "SELECT id, name FROM words WHERE id = ?" | |
| worldUpdate = "UPDATE worlds SET num = ? WHERE id = ?" | |
| // fortuneSelect = "SELECT id, message FROM Fortune;" | |
| worldRowCount = 10000 | |
| maxConnectionCount = 256 | |
| helloWorldString = "Hello, World!" | |
| ) | |
| var ( | |
| // Database | |
| worldStatement *sql.Stmt | |
| wordStatement *sql.Stmt | |
| // fortuneStatement *sql.Stmt | |
| updateStatement *sql.Stmt | |
| helloWorldBytes = []byte(helloWorldString) | |
| ) | |
| func main() { | |
| runtime.GOMAXPROCS(runtime.NumCPU()) | |
| db, err := sql.Open("mysql", connectionString) | |
| if err != nil { | |
| log.Fatalf("Error opening database: %v", err) | |
| } | |
| db.SetMaxIdleConns(maxConnectionCount) | |
| worldStatement, err = db.Prepare(worldSelect) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| wordStatement, err = db.Prepare(wordSelect) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // fortuneStatement, err = db.Prepare(fortuneSelect) | |
| // if err != nil { | |
| // log.Fatal(err) | |
| // } | |
| updateStatement, err = db.Prepare(worldUpdate) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| http.HandleFunc("/", dbHandler) | |
| http.HandleFunc("/big", dbBigHandler) | |
| http.HandleFunc("/json", jsonHandler) | |
| http.HandleFunc("/plain", plaintextHandler) | |
| http.ListenAndServe(":9292", nil) | |
| } | |
| // Test 1: JSON serialization | |
| func jsonHandler(w http.ResponseWriter, r *http.Request) { | |
| w.Header().Set("Content-Type", "application/javascript") | |
| json.NewEncoder(w).Encode(&Message{helloWorldString}) | |
| } | |
| // Test 2: Single database query | |
| func dbHandler(w http.ResponseWriter, r *http.Request) { | |
| var worlds World | |
| err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&worlds.Id, &worlds.num) | |
| if err != nil { | |
| log.Fatalf("Error scanning world row: %s", err.Error()) | |
| } | |
| w.Header().Set("Content-Type", "application/json") | |
| json.NewEncoder(w).Encode(&worlds) | |
| } | |
| // Test 2: Single database query | |
| func dbBigHandler(w http.ResponseWriter, r *http.Request) { | |
| var words Word | |
| err := wordStatement.QueryRow(rand.Intn(1000000)+1).Scan(&words.Id, &words.name) | |
| if err != nil { | |
| log.Fatalf("Error scanning word row: %s", err.Error()) | |
| } | |
| w.Header().Set("Content-Type", "application/json") | |
| json.NewEncoder(w).Encode(&words) | |
| } | |
| // Test 6: Plaintext | |
| func plaintextHandler(w http.ResponseWriter, r *http.Request) { | |
| w.Header().Set("Content-Type", "text/plain") | |
| w.Write(helloWorldBytes) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment