Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import std/algorithm
import std/json
import std/random
import std/strutils
import std/sequtils
import std/tables
# framework
import basolato/controller
Expand All @@ -17,8 +16,39 @@ import ../views/pages/fortune_scf_view


const range1_10000 = 1..10000
const worldByIdSql = """SELECT id, randomnumber FROM "World" WHERE id = ?"""
const orderedFortuneSql = """SELECT id, message FROM "Fortune" ORDER BY message ASC"""
const worldUpdateSql = """UPDATE "World" SET randomnumber = ? WHERE id = ?"""
let emptyArgs: seq[string] = @[]

let worldByIdStmt = rdb.prepare(worldByIdSql)
let orderedFortuneStmt = rdb.prepare(orderedFortuneSql)
let worldUpdateStmt = rdb.prepare(worldUpdateSql)

randomize()


proc readCount(context: Context, key: string, defaultVal: int = 1): int =
var countNum =
try:
context.params.getInt(key)
except:
defaultVal
if countNum < 1:
countNum = 1
if countNum > 500:
countNum = 500
return countNum


proc fetchWorldRow(id: int): Future[seq[string]] {.async.} =
return await worldByIdStmt.firstPlain(@[$id])


proc fetchFortuneRows(): Future[seq[seq[string]]] {.async.} =
return await orderedFortuneStmt.getPlain(emptyArgs)


proc plaintext*(context:Context):Future[Response] {.async.} =
return render("Hello, World!")

Expand All @@ -28,69 +58,68 @@ proc json*(context:Context):Future[Response] {.async.} =


proc db*(context:Context):Future[Response] {.async.} =
let i = rand(1..10000)
let res = rdb.table("World").findPlain(i).await
let i = rand(range1_10000)
let res = fetchWorldRow(i).await
return render(%*{"id": res[0].parseInt, "randomNumber": res[1].parseInt})


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

var resp:seq[JsonNode]
for i in 1..countNum:
let n = rand(range1_10000)
let res = rdb.table("World").findPlain(n).await
resp.add(%*{"id": res[0].parseInt, "randomNumber": res[1].parseInt})
var response = newSeq[JsonNode](countNum)
var futures = newSeq[Future[void]](countNum)
for i in 0..<countNum:
let id = rand(range1_10000)
futures[i] = (
proc(index: int, id: int):Future[void] {.async.} =
let row = fetchWorldRow(id).await
response[index] = %*{"id": row[0].parseInt, "randomNumber": row[1].parseInt}
)(i, id)

return render(%resp)
all(futures).await

return render(%response)


proc fortune*(context:Context):Future[Response] {.async.} =
let results = rdb.table("Fortune").orderBy("message", Asc).getPlain().await
var rows = results.map(
proc(x:seq[string]):Fortune =
return Fortune(id: x[0].parseInt, message: x[1])
let results = fetchFortuneRows().await

var rows = newSeq[Fortune](results.len + 1)
for i, row in results:
rows[i] = Fortune(id: row[0].parseInt, message: row[1])

rows[^1] = Fortune(
id: 0,
message: "Additional fortune added at request time."
)
rows.add(
Fortune(
id: 0,
message: "Additional fortune added at request time."
)

rows.sort(
proc(a, b: Fortune): int =
return cmp(a.message, b.message)
)
rows = rows.sortedByIt(it.message)

return render(fortuneScfView(rows).await)


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

var response = newSeq[JsonNode](countNum)
var futures = newSeq[Future[void]](countNum)
for i in 1..countNum:
let index = rand(range1_10000)
let number = rand(range1_10000)
response[i-1] = %*{"id": index, "randomNumber": number}
futures[i-1] = (
proc():Future[void] {.async.} =
discard rdb.table("World").findPlain(index).await
rdb.table("World").where("id", "=", index).update(%*{"randomnumber": number}).await

for i in 0..<countNum:
let id = rand(range1_10000)
let randomNumber = rand(range1_10000)

futures[i] = (proc():Future[void] {.async.} =
rdb.withConn(
proc(ctx: PostgresPreparedContext): Future[void] {.async.} =
discard await worldByIdStmt.firstPlain(ctx, @[$id])
await worldUpdateStmt.exec(ctx, @[$randomNumber, $id])
)
)()
response[i] = %*{"id": id, "randomNumber": randomNumber}

all(futures).await

return render(%response)
Expand All @@ -101,15 +130,7 @@ for i in range1_10000:
cachedQueryCache[i] = rand(range1_10000)

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

var response = newSeq[JsonNode](countNum)
for i in 1..countNum:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM ubuntu:24.04 AS build
FROM ubuntu:24.04

# prevent timezone dialogue
ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && \
apt upgrade -y
RUN apt update
RUN apt upgrade -y
RUN apt install -y \
# for build Nim
build-essential \
Expand All @@ -15,7 +15,9 @@ RUN apt install -y \
# for nim regex
libpcre3-dev \
curl \
git
git \
# for postgres
libpq-dev

ARG VERSION="2.2.8"
WORKDIR /root
Expand All @@ -34,32 +36,10 @@ WORKDIR /basolato
RUN nimble install -y -d
RUN ducere build -a --httpbeast


FROM ubuntu:24.04 AS runtime

# prevent timezone dialogue
ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && \
apt upgrade -y
RUN apt install -y \
# for build Nim
build-essential \
# for https
ca-certificates \
# for nim regex
libpcre3-dev \
# for postgres
libpq-dev

WORKDIR /basolato
COPY --from=build /basolato/main .
RUN chmod +x main
COPY --from=build /basolato/startServer.sh .
RUN chmod +x startServer.sh

ENV SECRET_KEY="secret_key"
ENV DB_URL="postgresql://benchmarkdbuser:benchmarkdbpass@tfb-database:5432/hello_world"

EXPOSE 8080

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"

# Dependencies
requires "nim >= 2.0.0"
requires "https://github.com/itsumura-h/nim-basolato#fix"
requires "https://github.com/itsumura-h/nim-basolato#main"

task test, "run testament":
echo staticExec("testament p \"./tests/test_*.nim\"")
Expand Down
Loading
Loading