Skip to content

Commit 3c4d632

Browse files
c-cesarc-cesar
and
c-cesar
authored
Add veb, the new V lang framework (TechEmpower#9717)
* Add veb. V lang default framework * Update Readme, config and so * set headers with a middleware to reduce code repetition --------- Co-authored-by: c-cesar <[email protected]>
1 parent 1c894c9 commit 3c4d632

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed

frameworks/V/veb/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [veb](https://modules.vlang.io/veb.html) Benchmarking Test
2+
3+
Veb is the default V language web framework. This test uses veb and V native ORM.
4+
5+
### Test Type Implementation Source Code
6+
7+
All tests in a 95 lines [main.v] and a html [template](fortunes.html)
8+
### Database
9+
10+
PostgresQL
11+
12+
## Test URLs
13+
### JSON
14+
15+
http://localhost:8080/json
16+
17+
### PLAINTEXT
18+
19+
http://localhost:8080/plaintext
20+
21+
### DB
22+
23+
http://localhost:8080/db
24+
25+
### QUERY
26+
27+
http://localhost:8080/query?q=
28+
29+
### UPDATE
30+
31+
http://localhost:8080/update?queries=
32+
33+
### FORTUNES
34+
35+
http://localhost:8080/fortunes
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"framework": "veb",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"db_url": "/db",
9+
"query_url": "/queries?q=",
10+
"fortune_url": "/fortunes",
11+
"update_url": "/update?q=",
12+
"port": 8080,
13+
"approach": "Realistic",
14+
"classification": "Fullstack",
15+
"database": "Postgres",
16+
"framework": "veb",
17+
"language": "V",
18+
"flavor": "None",
19+
"orm": "Micro",
20+
"platform": "None",
21+
"webserver": "None",
22+
"os": "Linux",
23+
"database_os": "Linux",
24+
"display_name": "veb",
25+
"notes": "",
26+
"versus": "None"
27+
}
28+
}
29+
]
30+
}

frameworks/V/veb/config.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[framework]
2+
name = "veb"
3+
4+
[main]
5+
urls.plaintext = "/plaintext"
6+
urls.json = "/json"
7+
urls.db = "/db"
8+
urls.query = "/queries?q="
9+
urls.update = "/update?q="
10+
urls.fortune = "/fortunes"
11+
approach = "Realistic"
12+
classification = "Fullstack"
13+
database = "Postgres"
14+
database_os = "Linux"
15+
os = "Linux"
16+
orm = "Micro"
17+
platform = "None"
18+
webserver = "None"
19+
versus = "None"

frameworks/V/veb/fortunes.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><title>Fortunes</title></head>
4+
<body>
5+
<table>
6+
<tr><th>id</th><th>message</th></tr>
7+
@for m in fortunes
8+
<tr><td>@m.id</td><td>@m.message</td></tr>
9+
@end
10+
</table>
11+
</body>
12+
</html>

frameworks/V/veb/main.v

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import veb
2+
import time
3+
import rand
4+
import db.pg
5+
6+
pub fn (app &App) plaintext(mut ctx Context) veb.Result {
7+
s := 'Hello, World!'
8+
return ctx.text(s)
9+
}
10+
11+
pub fn (app &App) json(mut ctx Context) veb.Result {
12+
obj := {'message': 'Hello, World!'}
13+
return ctx.json(obj)
14+
}
15+
16+
struct World {
17+
id int @[primary; sql: serial]
18+
mut:
19+
randomnumber int
20+
}
21+
22+
pub fn (app &App) db(mut ctx Context) veb.Result {
23+
r := rand.int_in_range(1, 10000) or { return ctx.text('rand error') }
24+
mut world := sql app.db {
25+
select from World where id == r
26+
} or { return ctx.text('db error') }
27+
return ctx.json(world.first())
28+
}
29+
30+
pub fn (app &App) queries(mut ctx Context) veb.Result {
31+
mut q := ctx.query['q'].int()
32+
if q < 1 { q = 1 } else if q > 500 { q = 500 }
33+
mut world := []World{}
34+
for _ in 0..q {
35+
r := rand.int_in_range(1, 10000) or { return ctx.text('rand error') }
36+
world << sql app.db {
37+
select from World where id == r
38+
} or { return ctx.text('db error') }
39+
}
40+
return ctx.json(world)
41+
}
42+
43+
pub fn (app &App) update(mut ctx Context) veb.Result {
44+
mut q := ctx.query['q'].int()
45+
if q < 1 { q = 1 } else if q > 500 { q = 500 }
46+
mut world := []World{}
47+
for _ in 0..q {
48+
r := rand.int_in_range(1, 10000) or { return ctx.text('rand error') }
49+
world << sql app.db {
50+
select from World where id == r
51+
} or { return ctx.text('db error') }
52+
world.last().randomnumber = rand.int_in_range(1, 10000) or { return ctx.text('rand error') }
53+
sql app.db {
54+
update World set randomnumber = world.last().randomnumber where id == world.last().id
55+
} or { return ctx.text('db error') }
56+
}
57+
return ctx.json(world)
58+
}
59+
60+
struct Fortune {
61+
id int @[primary; sql: serial]
62+
message string
63+
}
64+
65+
pub fn (app &App) fortunes(mut ctx Context) veb.Result {
66+
mut fortunes := sql app.db {
67+
select from Fortune
68+
} or { return ctx.text('db error') }
69+
fortunes.insert(0, Fortune{id: 0, message: 'Additional fortune added at request time.'})
70+
fortunes.sort(a.message < b.message)
71+
ctx.content_type = 'text/html; charset=utf-8'
72+
return $veb.html()
73+
}
74+
75+
pub struct Context {
76+
veb.Context
77+
}
78+
79+
pub struct App {
80+
veb.Middleware[Context]
81+
pub mut:
82+
db pg.DB
83+
}
84+
85+
pub fn header(mut ctx Context) bool {
86+
ctx.set_header(.date, time.now().as_utc().custom_format('ddd, DD MMM YYYY HH:MM:ss') + ' GMT')
87+
ctx.set_header(.server, 'veb')
88+
return true
89+
}
90+
91+
fn main() {
92+
mut app := &App{
93+
db: pg.connect(pg.Config{
94+
host: 'tfb-database'
95+
port: 5432
96+
user: 'benchmarkdbuser'
97+
password: 'benchmarkdbpass'
98+
dbname: 'hello_world'
99+
}) !
100+
}
101+
app.use(handler: header)
102+
veb.run[App, Context](mut app, 8080)
103+
}

frameworks/V/veb/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)); do
4+
taskset -c $i ./main &
5+
done
6+
7+
wait

frameworks/V/veb/veb.dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM thevlang/vlang:debian-dev
2+
RUN apt update && apt install -y libpq-dev
3+
4+
WORKDIR /app
5+
COPY ./main.v run.sh fortunes.html ./
6+
RUN v -prod -cflags '-std=gnu11 -Wall -O3 -march=native -mtune=native -flto' main.v
7+
8+
EXPOSE 8080
9+
CMD sh run.sh

0 commit comments

Comments
 (0)