-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
65 lines (48 loc) · 1.1 KB
/
server.go
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
package main
import (
"log"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/dgraph-io/badger/v3"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/django"
)
func main() {
engine := django.New("./views", ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(logger.New())
app.Get("/", func(c *fiber.Ctx) error {
opts := badger.DefaultOptions("/tmp/badger")
opts.Logger = nil
db, err := badger.Open(opts)
if err != nil {
log.Fatal(err)
}
start := time.Now()
for n := 0; n < 10000; n++ {
setTransaction(db)
}
calculatedTime := time.Since(start).Seconds()
defer db.Close()
return c.Render("index", fiber.Map{
"time": calculatedTime,
})
})
app.Static("/", "./views")
log.Fatal(app.Listen(":3000"))
}
func setTransaction(db *badger.DB) error {
txn := db.NewTransaction(true)
defer txn.Discard()
err := txn.Set([]byte(randomdata.SillyName()), []byte(randomdata.SillyName()))
if err != nil {
log.Fatal(err)
}
if err := txn.Commit(); err != nil {
log.Fatal(err)
}
return nil
}