Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go/atreugo] Add Cached Queries test #5826

Merged
merged 6 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Cached Queries tests
  • Loading branch information
Sergio Andres Virviescas Santana committed Jul 2, 2020
commit 22c8b5a63cf21177976544581ce9925e2d4051e2
3 changes: 2 additions & 1 deletion frameworks/Go/atreugo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ This is the go portion of a [benchmarking test suite](https://www.techempower.co

## Test URLs

http://localhost:8080/plaintext
http://localhost:8080/json
http://localhost:8080/db
http://localhost:8080/queries?queries=[1-500]
http://localhost:8080/cache?queries=[1-500]
http://localhost:8080/fortune
http://localhost:8080/update?queries=[1-500]
http://localhost:8080/plaintext
2 changes: 2 additions & 0 deletions frameworks/Go/atreugo/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries?queries=",
"cached_query_url": "/cache?queries=",
"fortune_url": "/fortune",
"update_url": "/update?queries=",
"plaintext_url": "/plaintext",
Expand All @@ -29,6 +30,7 @@
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries?queries=",
"cached_query_url": "/cache?queries=",
"fortune_url": "/fortune",
"update_url": "/update?queries=",
"plaintext_url": "/plaintext",
Expand Down
4 changes: 4 additions & 0 deletions frameworks/Go/atreugo/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func main() {
}
defer views.CloseDB()

// init and populate worlds cache
views.PopulateWorldsCache()

// init atreugo server
server := atreugo.New(atreugo.Config{
Addr: bindHost,
Expand All @@ -55,6 +58,7 @@ func main() {
server.GET("/update", views.Update)
server.GET("/queries", views.Queries)
server.GET("/fortune", views.FortuneQuick)
server.GET("/cache", views.CachedQueries)

if err := server.ListenAndServe(); err != nil {
panic(err)
Expand Down
7 changes: 4 additions & 3 deletions frameworks/Go/atreugo/src/views/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const (
dbPaswd = "benchmarkdbpass"
dbName = "hello_world"

worldSelectSQL = "SELECT id, randomNumber FROM World WHERE id = $1"
worldUpdateSQL = "UPDATE World SET randomNumber = $1 WHERE id = $2"
fortuneSelectSQL = "SELECT id, message FROM Fortune"
worldSelectSQL = "SELECT id, randomNumber FROM World WHERE id = $1"
worldSelectCacheSQL = "SELECT id, randomNumber FROM World LIMIT $1"
worldUpdateSQL = "UPDATE World SET randomNumber = $1 WHERE id = $2"
fortuneSelectSQL = "SELECT id, message FROM Fortune"
)

var db *pgxpool.Pool
Expand Down
42 changes: 42 additions & 0 deletions frameworks/Go/atreugo/src/views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,38 @@ import (
"github.com/savsgio/atreugo/v11"
)

var worldsCache *Worlds

const (
helloWorldStr = "Hello, World!"

contentTypeHTML = "text/html; charset=utf-8"
)

// PopulateWorldsCache populates the worlds cache for the cache test
func PopulateWorldsCache() {
worlds := acquireWorlds()
worlds.W = worlds.W[:maxWorlds]

rows, err := db.Query(context.Background(), worldSelectCacheSQL, maxWorlds)
if err != nil {
panic(err)
}

i := 0
for rows.Next() {
w := &worlds.W[i]

if err := rows.Scan(&w.ID, &w.RandomNumber); err != nil {
panic(err)
}

i++
}

worldsCache = worlds
}

// JSON . Test 1: JSON serialization.
func JSON(ctx *atreugo.RequestCtx) error {
message := acquireMessage()
Expand Down Expand Up @@ -57,6 +83,22 @@ func Queries(ctx *atreugo.RequestCtx) error {
return err
}

// CachedQueries . Test 4: Multiple cache queries:
func CachedQueries(ctx *atreugo.RequestCtx) error {
queries := queriesParam(ctx)
worlds := acquireWorlds()
worlds.W = worlds.W[:queries]

for i := 0; i < queries; i++ {
worlds.W[i] = worldsCache.W[randomWorldNum()-1]
}

err := ctx.JSONResponse(worlds.W)
releaseWorlds(worlds)

return err
}

// FortuneQuick . Test 4: Fortunes.
func FortuneQuick(ctx *atreugo.RequestCtx) error {
fortune := templates.AcquireFortune()
Expand Down