Browse Source

add plaintext & db query test

add plaintext & db query
astaxie 11 years ago
parent
commit
ae6c56f20e
1 changed files with 55 additions and 0 deletions
  1. 55 0
      beego/src/hello/hello.go

+ 55 - 0
beego/src/hello/hello.go

@@ -1,14 +1,38 @@
 package main
 package main
 
 
 import (
 import (
+	"log"
+	"math/rand"
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+
+	_ "github.com/go-sql-driver/mysql"
 	//"runtime"
 	//"runtime"
 )
 )
 
 
+const (
+	// Database
+	connectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world"
+	worldRowCount      = 10000
+	macIdleConnection  = 30
+	maxConnectionCount = 256
+
+	helloWorldString = "Hello, World!"
+)
+
+var (
+	helloWorldBytes = []byte(helloWorldString)
+)
+
 type MessageStruct struct {
 type MessageStruct struct {
 	Message string `json:"message"`
 	Message string `json:"message"`
 }
 }
 
 
+type World struct {
+	Id           uint16 `orm:"pk" json:"id"`
+	RandomNumber uint16 `orm:"column(randomNumber)" json:"randomNumber"`
+}
+
 type JsonController struct {
 type JsonController struct {
 	beego.Controller
 	beego.Controller
 }
 }
@@ -19,10 +43,41 @@ func (this *JsonController) Get() {
 	this.ServeJson()
 	this.ServeJson()
 }
 }
 
 
+type PlaintextController struct {
+	beego.Controller
+}
+
+func (this *PlaintextController) Get() {
+	this.Ctx.Output.Header("Content-Type", "text/plain")
+	this.Ctx.Output.Body(helloWorldBytes)
+}
+
+type DBController struct {
+	beego.Controller
+}
+
+func (this *DBController) Get() {
+	o := orm.NewOrm()
+	w := World{Id: uint16(rand.Intn(worldRowCount) + 1)}
+	err := o.Read(&w)
+	if err != nil {
+		log.Fatalf("Error read world row: %s", err.Error())
+	}
+	this.Data["json"] = &w
+	this.ServeJson()
+}
+
 func main() {
 func main() {
 	//don't need this set, beego default set it
 	//don't need this set, beego default set it
 	//runtime.GOMAXPROCS(runtime.NumCPU())
 	//runtime.GOMAXPROCS(runtime.NumCPU())
 	beego.RunMode = "prod"
 	beego.RunMode = "prod"
 	beego.Router("/json", &JsonController{})
 	beego.Router("/json", &JsonController{})
+	beego.Router("/db", &DBController{})
+	beego.Router("/plaintext", &PlaintextController{})
 	beego.Run()
 	beego.Run()
 }
 }
+
+func init() {
+	orm.RegisterModel(new(World))
+	orm.RegisterDataBase("default", "mysql", connectionString, macIdleConnection, maxConnectionCount)
+}