Skip to content

Commit 88cfca6

Browse files
authored
FIx benchmark tests for Basolato Nim (#8662)
* wip * wip * wip * wip * wip * fix * fix * fix * delete old project * fix * fix * fix * fix * fix * fix * fix dependency version * fix * fix * fix docs * remove unnecessary files * fix readme
1 parent baa5842 commit 88cfca6

File tree

17 files changed

+279
-243
lines changed

17 files changed

+279
-243
lines changed

frameworks/Nim/basolato/.gitignore

Lines changed: 0 additions & 14 deletions
This file was deleted.

frameworks/Nim/basolato/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
### Test Type Implementation Source Code
44

5-
* [JSON](./app/controllers/benchmark_controller.nim)
6-
* [PLAINTEXT](./app/controllers/benchmark_controller.nim)
7-
* [DB](./app/controllers/benchmark_controller.nim)
8-
* [QUERY](./app/controllers/benchmark_controller.nim)
9-
* [UPDATE](./app/controllers/benchmark_controller.nim)
10-
* [FORTUNES](./app/controllers/benchmark_controller.nim)
5+
* [JSON](./app/http/controllers/benchmark_controller.nim)
6+
* [PLAINTEXT](./app/http/controllers/benchmark_controller.nim)
7+
* [DB](./app/http/controllers/benchmark_controller.nim)
8+
* [QUERY](./app/http/controllers/benchmark_controller.nim)
9+
* [UPDATE](./app/http/controllers/benchmark_controller.nim)
10+
* [FORTUNES](./app/http/controllers/benchmark_controller.nim)
1111

1212
## Important Libraries
1313
The tests were run with:
1414
* [Software](https://github.com/itsumura-h/nim-basolato)
15-
* [Example](https://github.com/itsumura-h/nim-basolato/tree/master/examples)
1615

1716
## Test URLs
1817
### JSON
@@ -29,11 +28,11 @@ http://localhost:8080/db
2928

3029
### QUERY
3130

32-
http://localhost:8080/query?queries=
31+
http://localhost:8080/queries?queries=1
3332

3433
### UPDATE
3534

36-
http://localhost:8080/update?queries=
35+
http://localhost:8080/updates?queries=1
3736

3837
### FORTUNES
3938

frameworks/Nim/basolato/app/controllers/benchmark_controller.nim

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import std/algorithm
2+
import std/json
3+
import std/random
4+
import std/strutils
5+
import std/sequtils
6+
# framework
7+
import basolato/controller
8+
# databse
9+
import db_connector/db_postgres
10+
import allographer/query_builder
11+
import ../../../config/database
12+
# model
13+
import ../../models/fortune
14+
# view
15+
import ../views/pages/fortune_scf_view
16+
17+
18+
const range1_10000 = 1..10000
19+
let getFirstPrepare = stdRdb.prepare("getFirst", sql""" SELECT * FROM "World" WHERE id = $1 LIMIT 1 """, 1)
20+
let getFortunePrepare = stdRdb.prepare("getFortunes", sql""" SELECT * FROM "Fortune" ORDER BY message ASC """, 0)
21+
22+
23+
proc plaintext*(context:Context, params:Params):Future[Response] {.async.} =
24+
let headers = newHttpHeaders()
25+
headers.add("Content-Type", "text/plain; charset=UTF-8")
26+
return render("Hello, World!", headers)
27+
28+
29+
proc json*(context:Context, params:Params):Future[Response] {.async.} =
30+
return render(%*{"message":"Hello, World!"})
31+
32+
33+
proc db*(context:Context, params:Params):Future[Response] {.async.} =
34+
let i = rand(1..10000)
35+
let res = stdRdb.getRow(getFirstPrepare, i)
36+
return render(%*{"id": res[0].parseInt, "randomNumber": res[1].parseInt})
37+
38+
39+
proc query*(context:Context, params:Params):Future[Response] {.async.} =
40+
var countNum =
41+
try:
42+
params.getInt("queries")
43+
except:
44+
1
45+
if countNum < 1:
46+
countNum = 1
47+
elif countNum > 500:
48+
countNum = 500
49+
50+
var resp:seq[Row]
51+
for i in 1..countNum:
52+
let n = rand(range1_10000)
53+
resp.add(stdRdb.getRow(getFirstPrepare, n))
54+
55+
let response = resp.map(
56+
proc(x:Row):JsonNode =
57+
%*{"id": x[0].parseInt, "randomNumber": x[1].parseInt}
58+
)
59+
return render(%response)
60+
61+
62+
proc fortune*(context:Context, params:Params):Future[Response] {.async.} =
63+
let results = stdRdb.getAllRows(getFortunePrepare)
64+
var rows = results.map(
65+
proc(x:seq[string]):Fortune =
66+
return Fortune(id: x[0].parseInt, message: x[1])
67+
)
68+
rows.add(
69+
Fortune(
70+
id: 0,
71+
message: "Additional fortune added at request time."
72+
)
73+
)
74+
rows = rows.sortedByIt(it.message)
75+
return render(fortuneScfView(rows).await)
76+
77+
78+
proc update*(context:Context, params:Params):Future[Response] {.async.} =
79+
var countNum =
80+
try:
81+
params.getInt("queries")
82+
except:
83+
1
84+
if countNum < 1:
85+
countNum = 1
86+
elif countNum > 500:
87+
countNum = 500
88+
89+
var response = newSeq[JsonNode](countNum)
90+
var futures = newSeq[Future[void]](countNum)
91+
for i in 1..countNum:
92+
let index = rand(range1_10000)
93+
let number = rand(range1_10000)
94+
response[i-1] = %*{"id": index, "randomNumber": number}
95+
futures[i-1] = (
96+
proc():Future[void] {.async.} =
97+
discard stdRdb.getRow(getFirstPrepare, i)
98+
rdb.raw(""" UPDATE "World" SET "randomnumber" = ? WHERE id = ? """, %*[number, index]).exec()
99+
)()
100+
all(futures).await
101+
102+
return render(%response)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#? stdtmpl(toString="toString") | standard
2+
#import std/asyncdispatch
3+
#import basolato/view
4+
#import ../../../models/fortune
5+
#proc fortuneScfView*(rows:seq[Fortune]):Future[Component] {.async.} =
6+
# result = Component.new()
7+
<!DOCTYPE html>
8+
<html>
9+
10+
<head>
11+
<title>Fortunes</title>
12+
</head>
13+
14+
<body>
15+
<table>
16+
<tr>
17+
<th>id</th>
18+
<th>message</th>
19+
</tr>
20+
#for row in rows:
21+
<tr>
22+
<td>${row.id}</td>
23+
<td>${row.message}</td>
24+
</tr>
25+
#end for
26+
</table>
27+
</body>
28+
29+
</html>

frameworks/Nim/basolato/app/middlewares/framework_middleware.nim

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Fortune* = object
2+
id*:int
3+
message*:string
Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,73 @@
1-
FROM nimlang/nim:alpine
1+
FROM ubuntu:22.04 AS build
22

3-
ENV PATH $PATH:/root/.nimble/bin
3+
# prevent timezone dialogue
4+
ENV DEBIAN_FRONTEND=noninteractive
45

5-
RUN echo http://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
6-
RUN apk update && \
7-
apk upgrade --no-cache && \
8-
apk add --no-cache \
9-
openssh-client \
6+
RUN apt update && \
7+
apt upgrade -y
8+
RUN apt install -y --fix-missing \
9+
gcc \
10+
xz-utils \
1011
ca-certificates \
11-
openssl \
12-
pcre \
13-
bsd-compat-headers \
14-
lcov \
15-
sqlite mariadb-dev libpq && \
16-
rm /usr/lib/mysqld* -fr && rm /usr/bin/mysql* -fr && \
17-
update-ca-certificates
12+
curl \
13+
git
14+
15+
ARG VERSION="2.0.2"
16+
WORKDIR /root
17+
RUN curl https://nim-lang.org/choosenim/init.sh -o init.sh
18+
RUN sh init.sh -y
19+
RUN rm -f init.sh
20+
ENV PATH $PATH:/root/.nimble/bin
21+
RUN choosenim ${VERSION}
22+
23+
ENV PATH $PATH:/root/.nimble/bin
1824

1925
ADD ./ /basolato
2026
WORKDIR /basolato
2127

2228
RUN nimble install -y
23-
RUN ducere build
29+
RUN ducere build -p:8080 -o:speed
30+
31+
32+
FROM ubuntu:22.04 AS runtime
33+
34+
# prevent timezone dialogue
35+
ENV DEBIAN_FRONTEND=noninteractive
36+
37+
RUN apt update && \
38+
apt upgrade -y
39+
RUN apt install -y --fix-missing \
40+
xz-utils \
41+
ca-certificates \
42+
libpq-dev
43+
44+
WORKDIR /basolato
45+
COPY --from=build /basolato/main .
46+
RUN chmod 111 main
47+
COPY --from=build /basolato/startServer.sh .
48+
RUN chmod 111 startServer.sh
49+
50+
51+
# Secret
52+
ENV SECRET_KEY="pZWEVzA7h2FcKLgVM3ec5Eiik7eU9Ehpf0uLdYOZDgr0uZKIo5LdQE9sjIub3IDkUTrf3X2Jsh1Uw8b02GtAfWRn4C9NptfdSyoK"
53+
# DB Connection
54+
ENV DB_DATABASE="hello_world"
55+
ENV DB_USER="benchmarkdbuser"
56+
ENV DB_PASSWORD="benchmarkdbpass"
57+
ENV DB_HOST="tfb-database"
58+
ENV DB_PORT=5432
59+
ENV DB_MAX_CONNECTION=2000
60+
ENV DB_TIMEOUT=30
61+
# Logging
62+
ENV LOG_IS_DISPLAY=false
63+
ENV LOG_IS_FILE=false
64+
ENV LOG_IS_ERROR_FILE=false
65+
# Session db
66+
# Session type, file or redis, is defined in config.nims
67+
ENV SESSION_TIME=20160
68+
ENV LOCALE=en
69+
2470

2571
EXPOSE 8080
2672

27-
CMD ./main
73+
CMD ./startServer.sh
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Package
2+
version = "0.1.0"
3+
author = "Anonymous"
4+
description = "A new awesome basolato package"
5+
license = "MIT"
6+
srcDir = "."
7+
bin = @["main"]
8+
backend = "c"
9+
10+
# Dependencies
11+
requires "nim >= 2.0.0"
12+
requires "https://github.com/itsumura-h/nim-basolato == 0.15.0"
13+
requires "allographer == 0.29.1"
14+
requires "interface_implements >= 0.2.2"
15+
requires "bcrypt >= 0.2.1"
16+
requires "cligen >= 1.5.9"
17+
requires "redis >= 0.3.0"
18+
requires "sass >= 0.1.0"
19+
20+
task test, "run testament":
21+
echo staticExec("testament p \"./tests/test_*.nim\"")
22+
discard staticExec("find tests/ -type f ! -name \"*.*\" -delete 2> /dev/null")

0 commit comments

Comments
 (0)