Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h committed Dec 21, 2023
1 parent 7acd6eb commit dd91dca
Show file tree
Hide file tree
Showing 34 changed files with 231 additions and 586 deletions.
21 changes: 11 additions & 10 deletions examples/benchmark/FrameworkBenchmarks/asynchttpserver/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ../../../../src/basolato/core/benchmark

randomize()

proc threadProc(rdb:Rdb) {.thread.} =
proc threadProc(rdb:PostgresConnections) {.thread.} =
proc asyncProc() {.async.} =
var server = newAsyncHttpServer(true, true)
proc cb(req: Request) {.async, gcsafe.} =
Expand All @@ -25,7 +25,7 @@ proc threadProc(rdb:Rdb) {.thread.} =
await req.respond(Http200, $(%*{"message":"Hello, World!"}))
of "/db":
let i = rand(1..10000)
let response = await rdb.table("World").select("id", "randomNumber").find(i)
let response = rdb.select("id", "randomNumber").table("World").find(i).await
await req.respond(Http200, $(%*response))
of "/updates":
var countNum =
Expand All @@ -45,13 +45,14 @@ proc threadProc(rdb:Rdb) {.thread.} =
let response = newJArray()
var procs = newSeq[Future[void]](countNum)
for n in 1..countNum:
let i = rand(1..10000)
# let i = rand(1..10000)
let newRandomNumber = rand(1..10000)
procs[n-1] = (proc():Future[void]=
discard rdb.table("World").findPlain(i)
rdb.table("World").where("id", "=", i).update(%*{"randomNumber": newRandomNumber})
procs[n-1] = (
proc():Future[void] {.async.} =
discard rdb.table("World").findPlain(n)
rdb.table("World").where("id", "=", n).update(%*{"randomNumber": newRandomNumber})
)()
response.add(%*{"id":i, "randomNumber": newRandomNumber})
response.add(%*{"id":n, "randomNumber": newRandomNumber})
all(procs).await

await req.respond(Http200, $response)
Expand All @@ -75,10 +76,10 @@ proc threadProc(rdb:Rdb) {.thread.} =
proc serve() =
when compileOption("threads"):
let countThreads = countProcessors()
var arges = newSeq[Rdb](countThreads)
var arges = newSeq[PostgresConnections](countThreads)
for i in 0..arges.len-1:
arges[i] = dbOpen(PostgreSQL, "database", "user", "pass", "postgreDb", 5432, 50, 30, false, false, "")
var thr = newSeq[Thread[Rdb]](countThreads)
arges[i] = dbOpen(PostgreSQL, "database", "user", "pass", "postgreDb", 5432, 458, 30, false, false, "")
var thr = newSeq[Thread[PostgresConnections]](countThreads)
for i in 0..countThreads-1:
createThread(thr[i], threadProc, arges[i])
echo(&"Starting {countThreads} threads")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nim c -r -d:release --threads:on --gc:orc -d:ssl --panics:on --stackTrace --lineTrace --putenv:PORT=5000 main
nim c -r -d:release --threads:on -d:ssl --panics:on --stackTrace --lineTrace --putenv:PORT=5000 main
13 changes: 5 additions & 8 deletions examples/benchmark/FrameworkBenchmarks/basolato/.env
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Secret
SECRET_KEY="oaOPqiqLp7hNAN0izQl55p3SKAx9OM5Eob7DSmXDK5dRZj6QveHJPW8SRG3gbtwo7d4YeD9cbHQ64MuSK8tMLh00pNGoJw6O7nn8"
SECRET_KEY="pZWEVzA7h2FcKLgVM3ec5Eiik7eU9Ehpf0uLdYOZDgr0uZKIo5LdQE9sjIub3IDkUTrf3X2Jsh1Uw8b02GtAfWRn4C9NptfdSyoK"

# DB Connection
DB_DATABASE="database"
DB_DATABASE="database" # sqlite file path or database name
DB_USER="user"
DB_PASSWORD="pass"
DB_HOST="postgreDb"
DB_HOST="postgreDb" # host ip address
DB_PORT=5432 # postgres default...5432, mysql default...3306
; DB_MAX_CONNECTION=24 # should be smaller than (DB max connection / running num processes)
DB_MAX_CONNECTION=498 # should be smaller than (DB max connection / running num processes)
DB_TIMEOUT=20 # secounds
DB_MAX_CONNECTION=95 # should be smaller than (DB max connection / running num processes)
DB_TIMEOUT=30 # secounds

# Logging
LOG_IS_DISPLAY=false # true or false
Expand All @@ -19,9 +18,7 @@ LOG_DIR="/root/project/examples/benchmark/FrameworkBenchmarks/basolato/logs"

# Session db
# Session type, file or redis, is defined in config.nims
SESSION_DB_PATH="/root/project/examples/benchmark/FrameworkBenchmarks/basolato/session.db" # Session file path or redis host:port. ex:"127.0.0.1:6379"
SESSION_TIME=20160 # minutes of 2 weeks
ENABLE_ANONYMOUS_COOKIE=false # true or false
COOKIE_DOMAINS="" # to specify multiple domains, "sample.com, sample.org"

LOCALE=en
2 changes: 0 additions & 2 deletions examples/benchmark/FrameworkBenchmarks/basolato/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ SESSION_TIME=20160 # minutes of 2 weeks
ENABLE_ANONYMOUS_COOKIE=true # true or false
COOKIE_DOMAINS="" # to specify multiple domains, "sample.com, sample.org"

HOST="0.0.0.0"
PORT=5000
LOCALE=en
1 change: 0 additions & 1 deletion examples/benchmark/FrameworkBenchmarks/basolato/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ lcov.info
*.sqlite3
*.db
*.db.*

startServer.sh
69 changes: 61 additions & 8 deletions examples/benchmark/FrameworkBenchmarks/basolato/README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
# Congratulations!

You have successfully built a new test in the suite!

There are some remaining tasks to do before you are ready to open a pull request, however.

## Next Steps

1. Gather your source code.

You will need to ensure that your source code is beneath this directory. The most common solution is to include a `src` directory and place your source code there.

2. Edit `benchmark_config.json`

You will need alter `benchmark_config.json` to have the appropriate end-points and port specified.

3. Create `basolato.dockerfile`

This is the dockerfile that is built into a docker image and run when a benchmark test is run. Specifically, this file tells the suite how to build and start your test application.

You can create multiple implementations and they will all conform to `[name in benchmark_config.json].dockerfile`. For example, the `default` implementation in `benchmark_config.json` will be `basolato.dockerfile`, but if you wanted to make another implementation that did only the database tests for MySQL, you could make `basolato-mysql.dockerfile` and have an entry in your `benchmark_config.json` for `basolato-mysql`.

4. Test your application

$ tfb --mode verify --test basolato

This will run the suite in `verify` mode for your test. This means that no benchmarks will be captured and we will test that we can hit your implementation end-points specified by `benchmark_config.json` and that the response is correct.

Once you are able to successfully run your test through our suite in this way **and** your test passes our validation, you may move on to the next step.

5. Add your test to `.github/workflows/build.yml`

Edit `.github/workflows/build.yml` to ensure that Github Actions will automatically run our verification tests against your new test. This file is kept in alphabetical order, so find where `TESTDIR=Nim/basolato` should be inserted under `env > matrix` and put it there.

6. Fix this `README.md` and open a pull request

Starting on line 49 is your actual `README.md` that will sit with your test implementation. Update all the dummy values to their correct values so that when people visit your test in our Github repository, they will be greated with information on how your test implementation works and where to look for useful source code.

After you have the real `README.md` file in place, delete everything above line 59 and you are ready to open a pull request.

Thanks and Cheers!







# Basolato Benchmarking Test

### Test Type Implementation Source Code

* [JSON](./app/controllers/benchmark_controller.nim)
* [PLAINTEXT](./app/controllers/benchmark_controller.nim)
* [DB](./app/controllers/benchmark_controller.nim)
* [QUERY](./app/controllers/benchmark_controller.nim)
* [UPDATE](./app/controllers/benchmark_controller.nim)
* [FORTUNES](./app/controllers/benchmark_controller.nim)
* [JSON](Relative/Path/To/Your/Source/File)
* [PLAINTEXT](Relative/Path/To/Your/Source/File)
* [DB](Relative/Path/To/Your/Source/File)
* [QUERY](Relative/Path/To/Your/Source/File)
* [CACHED QUERY](Relative/Path/To/Your/Source/File)
* [UPDATE](Relative/Path/To/Your/Source/File)
* [FORTUNES](Relative/Path/To/Your/Source/File)

## Important Libraries
The tests were run with:
* [Software](https://github.com/itsumura-h/nim-basolato)
* [Example](https://github.com/itsumura-h/nim-basolato/tree/master/examples)
* [Software](https://www.example1.com/)
* [Example](http://www.example2.com/)

## Test URLs
### JSON
Expand All @@ -31,6 +80,10 @@ http://localhost:8080/db

http://localhost:8080/query?queries=

### CACHED QUERY

http://localhost:8080/cached_query?queries=

### UPDATE

http://localhost:8080/update?queries=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import std/algorithm
import std/json
import std/options
import std/random
import std/strutils
import std/sequtils
import std/httpcore
import db_connector/db_postgres
# framework
# import basolato/controller
import ../../../../../../../src/basolato/controller
import basolato/controller
# databse
import db_connector/db_postgres
import allographer/query_builder
import ../../../config/database # rdb, stdRdb, cacheDb
import ../../../config/database
# model
import ../../models/fortune
# import ../views/pages/fortune_view
# view
import ../views/pages/fortune_scf_view


const range1_10000 = 1..10000
let getFirstPrepare = stdRdb.prepare("getFirst", sql""" SELECT * FROM "World" WHERE id = $1 LIMIT 1 """, 1)
let updatePrepare = stdRdb.prepare("update", sql""" UPDATE "World" SET "randomNumber" = $1 WHERE id = $2 """, 2)


proc plaintext*(context:Context, params:Params):Future[Response] {.async.} =
let headers = newHttpHeaders()
headers.add("Content-Type", "text/plain; charset=UTF-8")
return render("Hello, World!", headers)


proc json*(context:Context, params:Params):Future[Response] {.async.} =
return render(%*{"message":"Hello, World!"})


proc db*(context:Context, params:Params):Future[Response] {.async.} =
let i = rand(1..10000)
let res = stdRdb.getRow(getFirstPrepare, i)
Expand All @@ -45,28 +46,14 @@ proc query*(context:Context, params:Params):Future[Response] {.async.} =
elif countNum > 500:
countNum = 500

# var resp:seq[Row]
# for i in 1..countNum:
# let n = rand(range1_10000)
# resp.add(stdRdb.getRow(getFirstPrepare, i))

# let response = resp.map(
# proc(x:Row):JsonNode =
# %*{"id": x[0].parseInt, "randomNumber": x[1].parseInt}
# )

var futures:seq[Future[seq[Row]]]
var resp:seq[Row]
for i in 1..countNum:
futures.add(
(
proc():Future[seq[Row]] {.async.} =
return rdb.raw(""" SELECT * FROM "World" WHERE id = ? LIMIT 1""", %*[i]).getPlain().await
)()
)
let resp = futures.all().await
let n = rand(range1_10000)
resp.add(stdRdb.getRow(getFirstPrepare, n))

let response = resp.map(
proc(x:seq[Row]):JsonNode =
%*{"id": x[0][0].`$`.parseInt, "randomNumber": x[0][1].`$`.parseInt}
proc(x:Row):JsonNode =
%*{"id": x[0].parseInt, "randomNumber": x[1].parseInt}
)

return render(%response)
Expand Down Expand Up @@ -107,42 +94,9 @@ proc update*(context:Context, params:Params):Future[Response] {.async.} =
futures[i-1] = (
proc():Future[void] =
discard stdRdb.getRow(getFirstPrepare, i)
# stdRdb.exec(updatePrepare, $number, $index)

# discard rdb.raw(""" SELECT * FROM "World" WHERE id = ? LIMIT 1""", %*[index]).getPlain()
rdb.raw(""" UPDATE "World" SET "randomNumber" = ? WHERE id = ? """, %*[number, index]).exec()

# discard rdb.table("World").findPlain(i).await
# rdb.table("World").where("id", "=", index).update(%*{"randomNumber": number}).await
)()
response[i-1] = %*{"id":i, "randomNumber": number}
all(futures).await

return render(%response)


proc cache*(context:Context, params:Params):Future[Response] {.async.} =
var countNum =
try:
params.getInt("count")
except:
1
if countNum < 1:
countNum = 1
elif countNum > 500:
countNum = 500

let response = newJArray()
for i in 1..countNum:
let id = rand(1..10000)
let number = rand(1..10000)
discard cacheDb.table("World").findPlain(id).await
cacheDb.table("World").where("id", "=", id).update(%*{"randomnumber": number}).await
response.add(%*{"id":id, "randomNumber": number})

return render(response)


proc sleep*(context:Context, params:Params):Future[Response] {.async.} =
sleepAsync(5000).await
return render("hello")
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#? stdtmpl(toString="toString") | standard
#import std/asyncdispatch
##import basolato/view
#import ../../../../../../../../src/basolato/view
#import basolato/view
#import ../../../models/fortune
#proc fortuneScfView*(rows:seq[Fortune]):Future[Component] {.async.} =
# result = Component.new()
Expand Down

This file was deleted.

Loading

0 comments on commit dd91dca

Please sign in to comment.