Skip to content

Commit

Permalink
update benchmark example (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h authored Oct 28, 2022
1 parent 49fff25 commit ccf16d9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 30 deletions.
25 changes: 25 additions & 0 deletions documents/benchmark_result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## 0.23.4
```sh
cd example
DB_POSTGRES=true nim c -r -d:release --threads:off -d:danger --mm:orc benchmark
```

query
|num|time|
|---|---|
|1|0.938770656|
|2|6.643012469|
|3|6.493994112999999|
|4|6.292020136999998|
|5|6.618309597|
|Avg|5.3972213944|

update
|num|time|
|---|---|
|1|0.3599897400000001|
|2|0.2508196720000004|
|3|0.1847369050000012|
|4|0.2267158040000012|
|5|0.2636497700000007|
|Avg|0.2571823782000007|
87 changes: 62 additions & 25 deletions example/benchmark.nim
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import std/asyncdispatch
import std/json
import std/random
import std/os
import std/strutils
import std/strformat
import std/sequtils
import std/times
import ../src/allographer/connection
import ../src/allographer/schema_builder
import ../src/allographer/query_builder
from connections import rdb


randomize()

let rdb = dbOpen(PostgreSQL, "allographer", "user", "Password!", "postgres", 5432, 95, 30, shouldDisplayLog=false)
const range1_10000 = 1..10000

proc migrate() {.async.} =
echo "=== start migration"
rdb.create(
table("World", [
Column.increments("id"),
Expand Down Expand Up @@ -49,9 +52,25 @@ proc migrate() {.async.} =
%*{"id": 12, "message": "フレームワークのベンチマーク"},
]
await rdb.table("Fortune").insert(data)
echo "=== finish migration"


proc query():Future[seq[JsonNode]] {.async.} =
const countNum = 500
var futures = newSeq[Future[seq[string]]](countNum)
for i in 1..countNum:
let n = rand(range1_10000)
futures[i-1] = rdb.table("World").findPlain(n)
let resp = all(futures).await
let response = resp.map(
proc(x:seq[string]):JsonNode =
if x.len > 0: %*{"id": x[0].parseInt, "randomnumber": x[1]}
else: newJObject()
)
return response


proc update() {.async.} =
proc update():Future[seq[JsonNode]] {.async.} =
const countNum = 500
var response = newSeq[JsonNode](countNum)
var updateFutures = newSeq[Future[void]](countNum)
Expand All @@ -66,31 +85,49 @@ proc update() {.async.} =
)()
response[i-1] = %*{"id":index, "randomNumber": number}
await all(updateFutures)


proc query() {.async.} =
const countNum = 100
var futures = newSeq[Future[seq[string]]](countNum)
for i in 1..countNum:
let n = rand(1..10000)
futures[i-1] = rdb.table("World").findPlain(n)
let resp = all(futures).await
let response = resp.map(
proc(x:seq[string]):JsonNode =
if x.len > 0: %*{"id": x[0].parseInt, "randomnumber": x[1]}
else: newJObject()
)
echo response
return response


proc main() {.async.} =
migrate().waitFor
# waitFor main()
let start = cpuTime()
for i in 1..20:
update().await
query().await
echo "===== ", i
echo cpuTime() - start
var queryResult = ""
var queryTime = 0.0
var updateResult = ""
var updateTime = 0.0

for i in 1..5:
# ===== qeury
sleep(500)
var start = cpuTime()
for _ in 1..20:
discard query().await
var time = cpuTime() - start
queryTime += time
if i > 1: queryResult.add("\c\L")
queryResult.add(&"|{i}|{time}|")

# ===== update
sleep(500)
start = cpuTime()
for _ in 1..20:
discard update().await
time = cpuTime() - start
updateTime += time
if i > 1: updateResult.add("\c\L")
updateResult.add(&"|{i}|{time}|")


echo "query"
echo "|num|time|"
echo "|---|---|"
echo queryResult
echo fmt"|Avg|{queryTime / 5}|"
echo ""
echo "update"
echo "|num|time|"
echo "|---|---|"
echo updateResult
echo fmt"|Avg|{updateTime / 5}|"


main().waitFor
10 changes: 5 additions & 5 deletions example/connections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ let
timeout = getEnv("DB_TIMEOUT").parseInt

let
# rdb* = dbopen(SQLite3, sqliteHost, maxConnections=maxConnections, shouldDisplayLog=false)
rdb* = dbopen(SQLite3, ":memory:", maxConnections=maxConnections, shouldDisplayLog=false)
# rdb* = dbopen(MySQL, database, user, password, mysqlHost, mysqlPort, maxConnections, timeout, shouldDisplayLog=true)
# rdb* = dbopen(MariaDB, database, user, password, mariadbHost, mysqlPort, maxConnections, timeout, shouldDisplayLog=true)
# rdb* = dbopen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout, shouldDisplayLog=true)
# rdb* = dbOpen(SQLite3, sqliteHost, maxConnections=maxConnections, shouldDisplayLog=false)
rdb* = dbOpen(SQLite3, ":memory:", maxConnections=maxConnections, shouldDisplayLog=false)
# rdb* = dbOpen(MySQL, database, user, password, mysqlHost, mysqlPort, maxConnections, timeout, shouldDisplayLog=true)
# rdb* = dbOpen(MariaDB, database, user, password, mariadbHost, mysqlPort, maxConnections, timeout, shouldDisplayLog=true)
# rdb* = dbOpen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout, shouldDisplayLog=true)

template asyncBlock*(body:untyped) =
(proc(){.async.}=
Expand Down

0 comments on commit ccf16d9

Please sign in to comment.