-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacked.nim
124 lines (95 loc) · 2.98 KB
/
packed.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import jason
import criterion
import packedjson
template toJson(x: untyped): untyped = %* x
when not defined(danger):
{.error: "benchmark with --define:danger".}
when not defined(gcArc) and not defined(gcOrc):
{.warning: "recommend --gc:arc or --gc:orc".}
type
Some = object
goats: array[4, string]
sheep: int
ducks: float
dogs: string
cats: bool
fish: seq[int32]
#llama: (int, bool, string, float)
#frogs: tuple[toads: bool, rats: string]
#geese: (int, int, int, int, int)
const
thing = Some(goats: ["black", "pigs", "pink", "horses"],
sheep: 11, ducks: 12.0,
fish: @[8'i32, 6, 7, 5, 3, 0, 9],
dogs: "woof", cats: false)
#llama: (1, true, "secret", 42.0),
#geese: (9, 0, 2, 1, 0),
#frogs: (toads: true, rats: "yep"))
var cfg = newDefaultConfig()
cfg.brief = true
cfg.budget = 1.0
echo "running benchmark..."
benchmark cfg:
proc encode_packed_integer() {.measure.} =
discard $(toJson thing.sheep)
benchmark cfg:
proc encode_jason_integer() {.measure.} =
discard thing.sheep.jason.string
benchmark cfg:
proc encode_packed_bool() {.measure.} =
discard $(toJson thing.cats)
benchmark cfg:
proc encode_jason_bool() {.measure.} =
discard thing.cats.jason.string
benchmark cfg:
proc encode_packed_number() {.measure.} =
discard $(toJson thing.ducks)
benchmark cfg:
proc encode_jason_number() {.measure.} =
discard thing.ducks.jason.string
benchmark cfg:
proc encode_packed_string() {.measure.} =
discard $(toJson thing.dogs)
benchmark cfg:
proc encode_jason_string() {.measure.} =
discard thing.dogs.jason.string
when false:
benchmark cfg:
proc encode_packed_tuple() {.measure.} =
discard $(toJson thing.geese)
benchmark cfg:
proc encode_jason_tuple() {.measure.} =
discard thing.geese.jason.string
benchmark cfg:
proc encode_packed_mixed_tuple() {.measure.} =
discard $(toJson thing.llama)
benchmark cfg:
proc encode_jason_mixed_tuple() {.measure.} =
discard thing.llama.jason.string
benchmark cfg:
proc encode_packed_named_tuple() {.measure.} =
discard $(toJson thing.frogs)
benchmark cfg:
proc encode_jason_named_tuple() {.measure.} =
discard thing.frogs.jason.string
else:
echo "\nNOTE: tuple serialization is unsupported by packedjson\n"
benchmark cfg:
proc encode_packed_array() {.measure.} =
discard $(toJson thing.goats)
benchmark cfg:
proc encode_jason_array() {.measure.} =
discard thing.goats.jason.string
echo "\nNOTE: uint serialization is unsupported by packedjson\n"
benchmark cfg:
proc encode_packed_sequence() {.measure.} =
discard $(toJson thing.fish)
benchmark cfg:
proc encode_jason_sequence() {.measure.} =
discard thing.fish.jason.string
benchmark cfg:
proc encode_packed_large_object() {.measure.} =
discard $(toJson thing)
benchmark cfg:
proc encode_jason_large_object() {.measure.} =
discard thing.jason.string