-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.nim
67 lines (52 loc) · 1.62 KB
/
bench.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import ../src/validate
import std/[strutils, sequtils, times, monotimes, cmdline]
type Category = ref object
name {.valid: @[length(min = 2)].}: string
type Status = enum
onsale
sold
proc isHttpUrl(v: string): bool =
v.startswith("http")
#!fmt: off
type
Book = object
url {.validFn(fn = "isHttpUrl", msg = "url is not a http url", tags = @["show"]).}: string
category {.valid: @[nonNil()].}: Category
tags {.valid: @[length(min = 2, max = 4, tags = ["show"])].}: seq[string]
price {.valid: @[frange(min = 5, max = 50, tags = ["hide"])].}: float
case status: Status
of onsale, sold:
count {.valid: @[range(min = 100, tags = ["hide"])].}: int
#!fmt: on
proc validate(book: Book, filterTags: varargs[string]): ValidateResult {.validate: "".}
proc validateWithTagFilterExpr(
book: Book
): ValidateResult {.validate: """ it in ["default","show","hide"] """.}
let category = Category(name: "T")
let book = Book(
url: "ftp://127.0.0.1/books/979-8836539412jk",
category: category,
tags: @["nim"],
price: 52'd,
status: onsale,
count: 10,
)
let size = 1000000
let all = newSeqWith(size, book)
echo "len: ", all.len
let tagFilterMethod = paramStr(1)
echo "tag filter method: ", tagFilterMethod
let st = getmonoTime()
for s in all:
let validateResult =
case tagFilterMethod
of "filterTags":
s.validate("default", "show", "hide")
of "tagFilterExpr":
s.validateWithTagFilterExpr()
else:
ValidateResult()
if validateResult.errors.len == 0:
raise newException(ValueError, "panic")
let ed = getmonoTime()
echo "result: ", (ed - st).inMicroseconds() / size, "μs/op"