Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (45 loc) · 1.41 KB
/
Copy pathmain.go
File metadata and controls
52 lines (45 loc) · 1.41 KB
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
// Example observability shows the read-only introspection API: Stats (global
// gauges), PoolStats (per-script snapshot incl. displayVersion), and
// CompileCount (distinct key:version compiled). Use these for dashboards and
// health checks.
package main
import (
"context"
"fmt"
"log"
"sort"
luart "github.com/htcom-code/go-lua-perf"
)
// observeDemo loads two scripts, runs each once, and returns the compile count,
// the sorted pool keys, and the global Stats snapshot.
func observeDemo() (compiles int64, keys []string, stats luart.Stats, err error) {
loader := luart.NewMapLoader()
a := `function f() return 1 end`
b := `function g() return 2 end`
loader.Set("a", a, luart.HashVersion(a), "1.0.0")
loader.Set("b", b, luart.HashVersion(b), "2.0.0")
rt := luart.New(loader, luart.Config{MaxStates: 4})
defer rt.Close()
if _, err = rt.Run(context.Background(), "a", "f"); err != nil {
return
}
if _, err = rt.Run(context.Background(), "b", "g"); err != nil {
return
}
compiles = rt.CompileCount()
stats = rt.Stats()
for _, ps := range rt.PoolStats() {
keys = append(keys, ps.Key)
}
sort.Strings(keys)
return
}
func main() {
compiles, keys, stats, err := observeDemo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("compiles: %d\n", compiles) // 2 (distinct key:version)
fmt.Printf("pools: %v\n", keys) // [a b]
fmt.Printf("stats: %+v\n", stats) // {Pools:2 LiveStates:2 MaxStates:4}
}