Skip to content

Commit 694513d

Browse files
committed
Add benchmarking test
1 parent 254e3fb commit 694513d

2 files changed

Lines changed: 480 additions & 168 deletions

File tree

README.md

Lines changed: 181 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Currently, it can only perform select queries.
1212

1313
## TODO:
1414

15-
- Implement Union queries
16-
- Insert Query
17-
- Validate SQL Syntax
18-
- ?
15+
- Implement Union queries
16+
- Insert Query
17+
- Validate SQL Syntax
18+
- ?
1919

2020
## Installation
2121

@@ -120,185 +120,185 @@ const (
120120

121121
In general, the structure of the JSON format used is as follows:
122122

123-
- **_table_**: Used to describe the table name, e.g. table_name (string)
123+
- **_table_**: Used to describe the table name, e.g. table_name (string)
124124

125-
- **\*selectFields\*\***:
126-
Used to select fields from a table, this property uses the **_Array_** type, you can combine **_Array of String_**, and **_Array of Json_**, the example is as follows:
125+
- **\*selectFields\*\***:
126+
Used to select fields from a table, this property uses the **_Array_** type, you can combine **_Array of String_**, and **_Array of Json_**, the example is as follows:
127127

128-
```json
129-
{
130-
"selectFields": [
131-
"table_1.a",
132-
{
133-
"field": "table_1.b",
134-
"alias": "foo_bar"
135-
},
136-
{
137-
"field": "table_2.a",
138-
"alias": "baz",
139-
"subquery": {
140-
"table": "table_4",
141-
"selectFields": ["*"],
142-
"conditions": [
128+
```json
129+
{
130+
"selectFields": [
131+
"table_1.a",
143132
{
144-
"datatype": "number",
145-
"clause": "a",
146-
"operator": "=",
147-
"value": 1
148-
}
149-
],
150-
"limit": 1
151-
}
152-
},
153-
"table_2.b",
154-
"table_3.a",
155-
"table_3.b"
156-
]
157-
}
158-
```
133+
"field": "table_1.b",
134+
"alias": "foo_bar"
135+
},
136+
{
137+
"field": "table_2.a",
138+
"alias": "baz",
139+
"subquery": {
140+
"table": "table_4",
141+
"selectFields": ["*"],
142+
"conditions": [
143+
{
144+
"datatype": "number",
145+
"clause": "a",
146+
"operator": "=",
147+
"value": 1
148+
}
149+
],
150+
"limit": 1
151+
}
152+
},
153+
"table_2.b",
154+
"table_3.a",
155+
"table_3.b"
156+
]
157+
}
158+
```
159159

160-
There you can see there is a subquery, you can use a subquery with the same format as its parent, you can also describe a field with an alias in the selection field.
160+
There you can see there is a subquery, you can use a subquery with the same format as its parent, you can also describe a field with an alias in the selection field.
161161

162-
> **_NOTE:_** If you are using a subquery, you do not need to describe the data type
162+
> **_NOTE:_** If you are using a subquery, you do not need to describe the data type
163163

164-
- **_join_**:
165-
You can use join to combine multiple tables, an example is as follows:
164+
- **_join_**:
165+
You can use join to combine multiple tables, an example is as follows:
166166

167-
```json
168-
{
169-
"join": [
170-
{
171-
"table": "table_2",
172-
"type": "join",
173-
"on": {
174-
"table_2.a": "table_1.a"
175-
}
176-
},
177-
{
178-
"table": "table_3",
179-
"type": "left",
180-
"on": {
181-
"table_3.a": "table_2.a"
182-
}
183-
}
184-
]
185-
}
186-
```
167+
```json
168+
{
169+
"join": [
170+
{
171+
"table": "table_2",
172+
"type": "join",
173+
"on": {
174+
"table_2.a": "table_1.a"
175+
}
176+
},
177+
{
178+
"table": "table_3",
179+
"type": "left",
180+
"on": {
181+
"table_3.a": "table_2.a"
182+
}
183+
}
184+
]
185+
}
186+
```
187187

188-
- **conditions**:
189-
Conditions are used for SQL Where clauses. The structure of these conditions is dynamic; you can use a function, subquery, or composite. Consider the following example:
190-
```json
191-
{
192-
"conditions": [
193-
{
194-
"datatype": "string",
195-
"clause": "table_1.a",
196-
"operator": "=",
197-
"value": "foo"
198-
},
199-
{
200-
"operand": "and",
201-
"datatype": "boolean",
202-
"clause": "table_1.b",
203-
"operator": "=",
204-
"value": true
205-
},
206-
{
207-
"operand": "and",
208-
"datatype": "function",
209-
"clause": "table_2.a",
210-
"operator": ">",
211-
"value": {
212-
"sqlFunc": {
213-
"name": "sum",
214-
"params": [100]
215-
}
216-
}
217-
},
218-
{
219-
"operand": "and",
220-
"clause": "table_2.b",
221-
"operator": "=",
222-
"value": {
223-
"subquery": {
224-
"table": "table_4",
225-
"selectFields": ["*"],
226-
"conditions": [
227-
{
228-
"datatype": "number",
229-
"clause": "a",
188+
- **conditions**:
189+
Conditions are used for SQL Where clauses. The structure of these conditions is dynamic; you can use a function, subquery, or composite. Consider the following example:
190+
```json
191+
{
192+
"conditions": [
193+
{
194+
"datatype": "string",
195+
"clause": "table_1.a",
230196
"operator": "=",
231-
"value": 1
232-
}
233-
],
234-
"limit": 1
235-
}
236-
}
237-
},
238-
{
239-
"operand": "or",
240-
"composite": [
241-
{
242-
"clause": "table_3.a",
243-
"datatype": "string",
244-
"operator": "between",
245-
"value": {
246-
"from": "2020-01-01",
247-
"to": "2023-01-01"
197+
"value": "foo"
198+
},
199+
{
200+
"operand": "and",
201+
"datatype": "boolean",
202+
"clause": "table_1.b",
203+
"operator": "=",
204+
"value": true
205+
},
206+
{
207+
"operand": "and",
208+
"datatype": "function",
209+
"clause": "table_2.a",
210+
"operator": ">",
211+
"value": {
212+
"sqlFunc": {
213+
"name": "sum",
214+
"params": [100]
215+
}
216+
}
217+
},
218+
{
219+
"operand": "and",
220+
"clause": "table_2.b",
221+
"operator": "=",
222+
"value": {
223+
"subquery": {
224+
"table": "table_4",
225+
"selectFields": ["*"],
226+
"conditions": [
227+
{
228+
"datatype": "number",
229+
"clause": "a",
230+
"operator": "=",
231+
"value": 1
232+
}
233+
],
234+
"limit": 1
235+
}
236+
}
237+
},
238+
{
239+
"operand": "or",
240+
"composite": [
241+
{
242+
"clause": "table_3.a",
243+
"datatype": "string",
244+
"operator": "between",
245+
"value": {
246+
"from": "2020-01-01",
247+
"to": "2023-01-01"
248+
}
249+
},
250+
{
251+
"operand": "and",
252+
"datatype": "string",
253+
"clause": "table_3.b",
254+
"operator": "=",
255+
"value": "2"
256+
}
257+
]
248258
}
249-
},
250-
{
251-
"operand": "and",
252-
"datatype": "string",
253-
"clause": "table_3.b",
254-
"operator": "=",
255-
"value": "2"
256-
}
257259
]
258-
}
259-
]
260-
}
261-
```
262-
- **groupBy**:
260+
}
261+
```
262+
- **groupBy**:
263263

264-
```json
265-
{
266-
"groupBy": {
267-
"fields": ["table_1.a"]
264+
```json
265+
{
266+
"groupBy": {
267+
"fields": ["table_1.a"]
268+
}
268269
}
269-
}
270-
```
270+
```
271271

272-
- **having**
273-
```json
274-
{
275-
"having": [
276-
{
277-
"clause": {
278-
"sqlFunc": {
279-
"name": "count",
280-
"isField": true,
281-
"params": ["table_2.a"]
282-
}
272+
- **having**
273+
```json
274+
{
275+
"having": [
276+
{
277+
"clause": {
278+
"sqlFunc": {
279+
"name": "count",
280+
"isField": true,
281+
"params": ["table_2.a"]
282+
}
283+
},
284+
"datatype": "number",
285+
"operator": ">",
286+
"value": 10
287+
}
288+
]
289+
}
290+
```
291+
**orderBy, limit, offset**:
292+
```json
293+
{
294+
"orderBy": {
295+
"fields": ["table_1.a", "table_2.a"],
296+
"sort": "asc"
283297
},
284-
"datatype": "number",
285-
"operator": ">",
286-
"value": 10
287-
}
288-
]
289-
}
290-
```
291-
**orderBy, limit, offset**:
292-
```json
293-
{
294-
"orderBy": {
295-
"fields": ["table_1.a", "table_2.a"],
296-
"sort": "asc"
297-
},
298-
"limit": 1,
299-
"offset": 0
300-
}
301-
```
298+
"limit": 1,
299+
"offset": 0
300+
}
301+
```
302302

303303
## Convert to Raw Query
304304

@@ -483,3 +483,16 @@ SELECT table_1.a, table_1.b AS foo_bar, (SELECT * FROM table_4 WHERE a = ? LIMIT
483483
Param:
484484
[1 foo true 100 1 2020-01-01 2023-01-01 2 10]
485485
```
486+
487+
## Benchmarking
488+
489+
In this benchmarking process, I used a 2020 MacBook Pro M1 with an 8-core processor (arm64) and 8GB of RAM. The following are the benchmark results obtained on my MacBook.
490+
491+
```go
492+
> go test -bench=. -benchmem
493+
goos: darwin
494+
goarch: arm64
495+
pkg: github.com/bonkzero404/gojson2sql
496+
BenchmarkJson2Sql_Build-8 12582 95278 ns/op 31511 B/op 535 allocs/op
497+
BenchmarkJson2Sql_Generate-8 9433 123334 ns/op 43108 B/op 634 allocs/op
498+
```

0 commit comments

Comments
 (0)