Skip to content

Commit 300eedf

Browse files
committed
feat: veb_brmassa
1 parent a0ebdee commit 300eedf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1819
-0
lines changed

frameworks/V/veb_brmassa/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# V lang example web server
2+
3+
Server "competition" *Web Framework Benchmarks*: https://www.techempower.com/benchmarks
4+
5+
It designed to demonstrate how to use <a href="https://modules.vlang.io/veb.html">Veb</a>'s built-in features to create a web application. It's a slightly more complex example due to the fact i tried to reuse code for multiple scenarios.
6+
7+
> NOTE: Cache test still to be properly implemented
8+
9+
## Scenarios
10+
11+
We created some scenarios. One on each folder. All scenarios use **Veb** server, the <a href="https://vlang.io">V</a> lang built-in web framework.
12+
13+
### Veb with Postgres (veb-pg/)
14+
15+
```
16+
v run veb-pg/
17+
```
18+
19+
### Veb with Postgres and ORM (veb-pg-orm/) (TBD)
20+
21+
```
22+
v run veb-pg-orm/
23+
```
24+
25+
### Veb with MySQL/MariaDB (veb-my/) (TBD)
26+
27+
```
28+
v run veb-my/
29+
```
30+
31+
### Veb with MySQL/MariaDB and ORM (veb-my-orm/) (TBD)
32+
33+
```
34+
v run veb-my-orm/
35+
```
36+
37+
## Local Setup
38+
39+
in order to test the servers locally, you will need a database server and a database client. using container, you can use:
40+
41+
### Postgres:
42+
43+
```
44+
docker run --name benckmark-postgres \
45+
-e POSTGRES_USER=benchmarkdbuser \
46+
-e POSTGRES_PASSWORD=benchmarkdbpass \
47+
-e POSTGRES_DB=hello_world \
48+
-p 5432:5432 \
49+
-d techempower/postgres:latest
50+
```
51+
52+
### MySQL/MariaDB:
53+
54+
```
55+
docker run --name benckmark-mysql \
56+
-e MYSQL_ROOT_PASSWORD=benchmarkdbpass \
57+
-e MYSQL_USER=benchmarkdbuser \
58+
-e MYSQL_PASSWORD=benchmarkdbpass \
59+
-e MYSQL_DATABASE=hello_world \
60+
-p 3306:3306 \
61+
-d techempower/mysql:latest
62+
```
63+
64+
---
65+
66+
Created by Bruno Massa
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"framework": "veb_brmassa",
3+
"authors": ["Bruno Massa"],
4+
"github": "https://github.com/TechEmpower/gemini",
5+
"tests": [
6+
{
7+
"default": {
8+
"json_url": "/json",
9+
"plaintext_url": "/plaintext",
10+
"db_url": "/db",
11+
"query_url": "/queries?q=",
12+
"fortune_url": "/fortunes",
13+
"update_url": "/update?q=",
14+
"port": 8080,
15+
"approach": "Realistic",
16+
"classification": "Fullstack",
17+
"database": "Postgres",
18+
"framework": "veb",
19+
"language": "V",
20+
"flavor": "None",
21+
"orm": "Micro",
22+
"platform": "None",
23+
"webserver": "None",
24+
"os": "Linux",
25+
"database_os": "Linux",
26+
"display_name": "veb [pg]",
27+
"notes": "",
28+
"versus": "None"
29+
},
30+
"pg_orm": {
31+
"json_url": "/json",
32+
"plaintext_url": "/plaintext",
33+
"db_url": "/db",
34+
"query_url": "/queries?q=",
35+
"fortune_url": "/fortunes",
36+
"update_url": "/update?q=",
37+
"port": 8080,
38+
"approach": "Realistic",
39+
"classification": "Fullstack",
40+
"database": "Postgres",
41+
"framework": "veb",
42+
"language": "V",
43+
"flavor": "None",
44+
"orm": "Micro",
45+
"platform": "None",
46+
"webserver": "None",
47+
"os": "Linux",
48+
"database_os": "Linux",
49+
"display_name": "veb [pg+orm]",
50+
"notes": "",
51+
"versus": "None"
52+
},
53+
"non-optimized": {
54+
"json_url": "/json",
55+
"plaintext_url": "/plaintext",
56+
"db_url": "/db",
57+
"query_url": "/queries?q=",
58+
"fortune_url": "/fortunes",
59+
"update_url": "/update?q=",
60+
"port": 8080,
61+
"approach": "Realistic",
62+
"classification": "Fullstack",
63+
"database": "Postgres",
64+
"framework": "veb",
65+
"language": "V",
66+
"flavor": "None",
67+
"orm": "Micro",
68+
"platform": "None",
69+
"webserver": "None",
70+
"os": "Linux",
71+
"database_os": "Linux",
72+
"display_name": "veb [pg] non-optimized",
73+
"notes": "",
74+
"versus": "None"
75+
},
76+
"run-script": {
77+
"json_url": "/json",
78+
"plaintext_url": "/plaintext",
79+
"db_url": "/db",
80+
"query_url": "/queries?q=",
81+
"fortune_url": "/fortunes",
82+
"update_url": "/update?q=",
83+
"port": 8080,
84+
"approach": "Realistic",
85+
"classification": "Fullstack",
86+
"database": "Postgres",
87+
"framework": "veb",
88+
"language": "V",
89+
"flavor": "None",
90+
"orm": "Micro",
91+
"platform": "None",
92+
"webserver": "",
93+
"os": "Linux",
94+
"database_os": "Linux",
95+
"display_name": "veb [pg] run-script",
96+
"notes": "",
97+
"versus": ""
98+
},
99+
"my": {
100+
"json_url": "/json",
101+
"plaintext_url": "/plaintext",
102+
"db_url": "/db",
103+
"query_url": "/queries?q=",
104+
"fortune_url": "/fortunes",
105+
"update_url": "/update?q=",
106+
"port": 8080,
107+
"approach": "Realistic",
108+
"classification": "Fullstack",
109+
"database": "MySQL",
110+
"framework": "veb",
111+
"language": "V",
112+
"flavor": "None",
113+
"orm": "Micro",
114+
"platform": "None",
115+
"webserver": "None",
116+
"os": "Linux",
117+
"database_os": "Linux",
118+
"display_name": "veb [my]",
119+
"notes": "",
120+
"versus": "None"
121+
},
122+
"my_orm": {
123+
"json_url": "/json",
124+
"plaintext_url": "/plaintext",
125+
"db_url": "/db",
126+
"query_url": "/queries?q=",
127+
"fortune_url": "/fortunes",
128+
"update_url": "/update?q=",
129+
"port": 8080,
130+
"approach": "Realistic",
131+
"classification": "Fullstack",
132+
"database": "MySQL",
133+
"framework": "veb",
134+
"language": "V",
135+
"flavor": "None",
136+
"orm": "Micro",
137+
"platform": "None",
138+
"webserver": "",
139+
"os": "Linux",
140+
"database_os": "Linux",
141+
"display_name": "veb [my+orm]",
142+
"notes": "",
143+
"versus": ""
144+
}
145+
}
146+
]
147+
}

frameworks/V/veb_brmassa/run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
for i in $(seq 0 $(($(nproc --all)-1))); do
4+
taskset -c $i ./veb &
5+
done
6+
7+
wait
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import veb
2+
import os
3+
import db.mysql as db_module
4+
5+
const db_connector = 'MySQL'
6+
const db_connector_module = 'db.mysql'
7+
const db_connector_port = 3306
8+
const db_connector_varchar = 'TEXT' // Postgres and MySQL differ the text field (used in fortune)
9+
10+
@[heap]
11+
pub struct App {
12+
veb.Middleware[Context]
13+
pub mut:
14+
db db_module.DB
15+
}
16+
17+
@[inline]
18+
fn db_init(mut app App) {
19+
mut db_host := os.environ()['db_host']
20+
if db_host == '' {
21+
db_host = 'tfb-database'
22+
}
23+
24+
config := db_module.Config{
25+
host: db_host
26+
port: db_connector_port
27+
username: 'benchmarkdbuser'
28+
password: 'benchmarkdbpass'
29+
dbname: 'hello_world'
30+
}
31+
app.db = db_module.connect(config) or { panic(err) }
32+
// or { return ctx.server_error('Database connection failed') }
33+
}
34+
35+
// MySQL implementation return values while Postgres return options. Unify all as options
36+
fn convert_option(id int, random_number ?string) World {
37+
return World{
38+
id: id
39+
randomnumber: random_number or { '' }.int()
40+
}
41+
}
42+
43+
// MySQL implementation return values while Postgres return options. Unify all as options
44+
fn convert_fortune(id ?string, random_number ?string) Fortune {
45+
return Fortune{
46+
id: id or { '' }.int()
47+
message: random_number or { '' }
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Fortunes</title>
5+
</head>
6+
<body>
7+
<table>
8+
<tr>
9+
<th>id</th>
10+
<th>message</th>
11+
</tr>
12+
@for m in fortunes
13+
<tr>
14+
<td>@m.id</td>
15+
<td>@m.message</td>
16+
</tr>
17+
@end
18+
</table>
19+
</body>
20+
</html>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>V web server</title>
5+
<style>
6+
.list li {
7+
display: inline-block;
8+
}
9+
</style>
10+
</head>
11+
<body>
12+
<h1><i>V</i> web server</h1>
13+
<p>
14+
Welcome to <a href="https://vlang.io">V</a> lang example web server
15+
for
16+
<a href="https://www.techempower.com/benchmarks">
17+
Web Framework Benchmarks
18+
</a>
19+
</p>
20+
21+
<h2>Settings</h2>
22+
<ul>
23+
<li>v: 0.4.10</li>
24+
<li>${db_connector} (version is a user choice)</li>
25+
<li>
26+
Used V libraries and modules:
27+
<ul>
28+
<li>
29+
<a href="https://modules.vlang.io/veb.html">Veb</a>: web
30+
server and front-end
31+
</li>
32+
<li>
33+
<a
34+
href="https://modules.vlang.io/${db_connector_module}.html"
35+
>
36+
${db_connector_module}</a
37+
>
38+
: ${db_connector} connector
39+
</li>
40+
</ul>
41+
</li>
42+
</ul>
43+
44+
<h2>Local Setup</h2>
45+
<ul>
46+
<li><a href="/db_init">Database setup</a></li>
47+
</ul>
48+
49+
<h2>Benchmark Tests</h2>
50+
<ul>
51+
<li><a href="/json">JSON serialization</a></li>
52+
<li><a href="/db">Single query</a></li>
53+
<li>
54+
<a href="/queries">Multiple queries</a>
55+
<ul class="list">
56+
<li><a href="/queries?q?count=0"> 0</a></li>
57+
<li><a href="/queries?q=1"> 2</a></li>
58+
<li><a href="/queries?q=foo"> foo</a></li>
59+
<li><a href="/queries?q=501"> 501</a></li>
60+
<li><a href="/queries?q=15"> 15</a></li>
61+
<li><a href="/queries?q="> (empty)</a></li>
62+
</ul>
63+
</li>
64+
<li><a href="/fortunes">Fortunes</a></li>
65+
<li>
66+
<a href="/update">Data updates</a>
67+
<ul class="list">
68+
<li><a href="/update?q?count=0"> 0</a></li>
69+
<li><a href="/update?q=1"> 2</a></li>
70+
<li><a href="/update?q=foo"> foo</a></li>
71+
<li><a href="/update?q=501"> 501</a></li>
72+
<li><a href="/update?q=15"> 15</a></li>
73+
<li><a href="/update?q="> (empty)</a></li>
74+
</ul>
75+
</li>
76+
<li><a href="/plaintext">Plaintext</a></li>
77+
</ul>
78+
</body>
79+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import veb
2+
import time
3+
4+
pub struct Context {
5+
veb.Context
6+
}
7+
8+
// Program entry point
9+
fn main() {
10+
mut app := &App{}
11+
app.use(handler: header)
12+
13+
db_init(mut app)
14+
defer {
15+
app.db.close()
16+
}
17+
18+
veb.run[App, Context](mut app, 8080)
19+
}
20+
21+
pub fn header(mut ctx Context) bool {
22+
ctx.set_header(.date, time.now().utc_string())
23+
ctx.set_header(.server, 'veb')
24+
return true
25+
}

0 commit comments

Comments
 (0)