From ccf16d9057153cceb1d8abe1b1e939922222f533 Mon Sep 17 00:00:00 2001 From: itsumura-h <39766805+itsumura-h@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:53:37 +0900 Subject: [PATCH] update benchmark example (#217) --- documents/benchmark_result.md | 25 ++++++++++ example/benchmark.nim | 87 +++++++++++++++++++++++++---------- example/connections.nim | 10 ++-- 3 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 documents/benchmark_result.md diff --git a/documents/benchmark_result.md b/documents/benchmark_result.md new file mode 100644 index 00000000..0496243d --- /dev/null +++ b/documents/benchmark_result.md @@ -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| \ No newline at end of file diff --git a/example/benchmark.nim b/example/benchmark.nim index 6a2eb972..9ebfb8f8 100644 --- a/example/benchmark.nim +++ b/example/benchmark.nim @@ -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"), @@ -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) @@ -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 \ No newline at end of file diff --git a/example/connections.nim b/example/connections.nim index c9827f03..f13a7f34 100644 --- a/example/connections.nim +++ b/example/connections.nim @@ -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.}=