Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server pages #2

Merged
merged 22 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Update benchmark pages
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
push:
branches:
- server-pages

jobs:
run_benchmark:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
check-latest: true

# - name: Install the latest versions of each library
# run: go get -u ./... && go mod tidy

- name: Benchmark Go logging libraries
run: go test -bench=. bench/*.go > ${{ runner.temp }}/bench.txt

- name: Run Server / Get Pages
run: go run cmd/server/server.go -from=${{ runner.temp }}/bench.txt & scripts/pages

# - name: Benchmark Go logging libraries
# run: go run cmd/server/server.go -from=/tmp/bench.txt
#
# - name: Get Pages from server
# run: wget -r localhost:8080/go-slog -nH --cut-dirs=1 -P docs

- name: Commit benchmark results to repo
uses: EndBug/add-and-commit@v9
with:
message: Update benchmark results
commit: '--no-verify'
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ The benchmark data generated can be processed by two applications:
* [`server`](cmd/server/server.go)
runs a simple web server showing the same tables plus bar charts.

[Recent benchmark data](https://madkins23.github.io/go-slog/index.html).

## Gin Logging Redirect

Package `gin` contains utilities for using `slog` with `gin-gonic/gin`.
Expand Down
2 changes: 2 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ The root page shows links to various test data pages and the warnings:
Test pages show the same tables as `tabular` plus charts comparing the results:
![Test pages show the same tables as `tabular` plus charts comparing the results.](../cmd/server/images/test.png)

[Recent benchmark data](https://madkins23.github.io/go-slog/index.html).

---

[^1]: Don't use `-v` in the `go test -bench` command when using `gobenchdata`.
Expand Down
16 changes: 13 additions & 3 deletions cmd/server/embed/choices.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<script>
function testChoice(form) {
window.location.href = "/go-slog/test/" + form.tag.value + ".html";
return true;
}
function hdlrChoice(form) {
window.location.href = "/go-slog/handler/" + form.tag.value + ".html";
return true;
}
</script>
<table>
<tr>
<td align="right">Test:</td>
<td>
<form action="test" class="choice">
<select name="tag" id="tag" onchange="this.form.submit();">
<form class="choice">
<select name="tag" id="tag" onchange="testChoice(this.form);">
<option value="" selected disabled hidden>- Choose -</option>
{{ range .Data.TestTags }}
<option value="{{ . }}" {{ if eq $.Test . }}selected{{ end }}>{{ $.Data.TestName . }}</option>
Expand All @@ -16,7 +26,7 @@
<td align="right">Handler:</td>
<td>
<form action="handler" class="choice">
<select name="tag" id="tag" onchange="this.form.submit();">
<select name="tag" id="tag" onchange="hdlrChoice(this.form);">
<option value="" selected disabled hidden>- Choose -</option>
{{ range .Data.HandlerTags }}
<option value="{{ . }}" {{ if eq $.Handler . }}selected{{ end }}>{{ $.Data.HandlerName . }}</option>
Expand Down
12 changes: 6 additions & 6 deletions cmd/server/embed/handler.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<html>
<head>
<title>Benchmark Data</title>
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/go-slog/style.css">
</head>

<body>
<table class="top">
<tr>
<td>
<a href="/"><img src="home.svg" alt="Home" class="home" /></a>
<a href="/go-slog/index.html"><img src="/go-slog/home.svg" alt="Home" class="home" /></a>
<h1 class="home">Handler {{ .Data.HandlerName .Handler }}</h1>
</td>
<td align="right">
Expand Down Expand Up @@ -43,12 +43,12 @@
<td colspan=2 align="center">
<table>
<tr>
<td><img src="chart.svg?tag={{ .Handler }}&item=Nanos" alt="{{ .Data.HandlerName .Handler }} Ns/Op" class="chart" /></td>
<td><img src="chart.svg?tag={{ .Handler }}&item=MemAllocs" alt="{{ .Data.HandlerName .Handler }} Allocs/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Handler }}/Nanos.svg" alt="{{ .Data.HandlerName .Handler }} Ns/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Handler }}/MemAllocs.svg" alt="{{ .Data.HandlerName .Handler }} Allocs/Op" class="chart" /></td>
</tr>
<tr>
<td><img src="chart.svg?tag={{ .Handler }}&item=MemBytes" alt="{{ .Data.HandlerName .Handler }} Bytes/Op" class="chart" /></td>
<td><img src="chart.svg?tag={{ .Handler }}&item=GbPerSec" alt="{{ .Data.HandlerName .Handler }} GB/Sec" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Handler }}/MemBytes.svg" alt="{{ .Data.HandlerName .Handler }} Bytes/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Handler }}/GbPerSec.svg" alt="{{ .Data.HandlerName .Handler }} GB/Sec" class="chart" /></td>
</tr>
</table>
</td>
Expand Down
6 changes: 3 additions & 3 deletions cmd/server/embed/root.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<title>Benchmarks</title>
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/go-slog/style.css">
</head>

<body>
Expand All @@ -11,7 +11,7 @@
<legend>Tests</legend>
<table>
{{ range .Data.TestTags }}
<tr><td><a href="test?tag={{ . }}">{{ $.Data.TestName . }}</a></td></tr>
<tr><td><a href="/go-slog/test/{{ . }}.html">{{ $.Data.TestName . }}</a></td></tr>
{{ end }}
</table>
</fieldset>
Expand All @@ -20,7 +20,7 @@
<legend>Handlers</legend>
<table>
{{ range .Data.HandlerTags }}
<tr><td><a href="handler?tag={{ . }}">{{ $.Data.HandlerName . }}</a></td></tr>
<tr><td><a href="/go-slog/handler/{{ . }}.html">{{ $.Data.HandlerName . }}</a></td></tr>
{{ end }}
</table>
</fieldset>
Expand Down
12 changes: 6 additions & 6 deletions cmd/server/embed/test.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<html>
<head>
<title>Benchmark Data</title>
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/go-slog/style.css">
</head>

<body>
<table class="top">
<tr>
<td>
<a href="/"><img src="home.svg" alt="Home" class="home" /></a>
<a href="/go-slog/index.html"><img src="/go-slog/home.svg" alt="Home" class="home" /></a>
<h1 class="home">Test {{ .Data.TestName .Test }}</h1>
</td>
<td align="right">
Expand Down Expand Up @@ -43,12 +43,12 @@
<td colspan=2 align="center">
<table>
<tr>
<td><img src="chart.svg?tag={{ .Test }}&item=Nanos" alt="{{ .Data.TestName .Test }} Ns/Op" class="chart" /></td>
<td><img src="chart.svg?tag={{ .Test }}&item=MemAllocs" alt="{{ .Data.TestName .Test }} Allocs/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Test }}/Nanos.svg" alt="{{ .Data.TestName .Test }} Ns/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Test }}/MemAllocs.svg" alt="{{ .Data.TestName .Test }} Allocs/Op" class="chart" /></td>
</tr>
<tr>
<td><img src="chart.svg?tag={{ .Test }}&item=MemBytes" alt="{{ .Data.TestName .Test }} Bytes/Op" class="chart" /></td>
<td><img src="chart.svg?tag={{ .Test }}&item=GbPerSec" alt="{{ .Data.TestName .Test }} GB/Sec" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Test }}/MemBytes.svg" alt="{{ .Data.TestName .Test }} Bytes/Op" class="chart" /></td>
<td><img src="/go-slog/chart/{{ .Test }}/GbPerSec.svg" alt="{{ .Data.TestName .Test }} GB/Sec" class="chart" /></td>
</tr>
</table>
</td>
Expand Down
55 changes: 36 additions & 19 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"os"
"strings"
"sync"
"time"

Expand All @@ -16,6 +17,9 @@ import (
"github.com/vicanso/go-charts/v2"
"golang.org/x/text/message"

"github.com/madkins23/gin-utils/pkg/handler"
"github.com/madkins23/gin-utils/pkg/shutdown"

ginslog "github.com/madkins23/go-slog/gin"
"github.com/madkins23/go-slog/internal/bench"
"github.com/madkins23/go-slog/internal/language"
Expand All @@ -26,6 +30,8 @@ import (

type pageType string

const port = 8080

const (
pageRoot = "root"
pageTest = "bench"
Expand All @@ -43,7 +49,7 @@ var (
)

func main() {
flag.Parse() // Necessary for -json=<file> argument defined in infra package.
flag.Parse() // Necessary for -from=<file> argument defined in internal/bench package.

gin.DefaultWriter = ginslog.NewWriter(&ginslog.Options{})
gin.DefaultErrorWriter = ginslog.NewWriter(&ginslog.Options{Level: slog.LevelError})
Expand All @@ -58,23 +64,31 @@ func main() {
return
}

graceful := &shutdown.Graceful{}
graceful.Initialize()
defer graceful.Close()

router := gin.Default()
router.GET("/", pageFunction(pageRoot))
router.GET("/test", pageFunction(pageTest))
router.GET("/handler", pageFunction(pageHandler))
router.GET("/chart.svg", chartFunction)
router.GET("/home.svg", svgFunction(home))
router.GET("/style.css", textFunction(css))
rootPageFn := pageFunction(pageRoot)
router.GET("/go-slog/", rootPageFn)
router.GET("/go-slog/index.html", rootPageFn)
router.GET("/go-slog/test/:tag", pageFunction(pageTest))
router.GET("/go-slog/handler/:tag", pageFunction(pageHandler))
router.GET("/go-slog/chart/:tag/:item", chartFunction)
router.GET("/go-slog/home.svg", svgFunction(home))
router.GET("/go-slog/style.css", textFunction(css))
router.GET("/go-slog/exit", handler.Exit)

if err := router.SetTrustedProxies(nil); err != nil {
slog.Error("Don't trust proxies", "err", err)
os.Exit(1)
}

// Listen and serve on 0.0.0.0:8080 (for windows "localhost:8080"). {
slog.Info("Web Server @ http://localhost:8080")
if err := router.Run(); err != nil {
slog.Error("Error during ListenAndServe()", "err", err)
slog.Info("Web Server @ http://localhost:8080/go-slog")

if err := graceful.Serve(router, port); err != nil {
slog.Error("Running gin server", "err", err)
}
}

Expand Down Expand Up @@ -150,14 +164,14 @@ var (
)

func chartFunction(c *gin.Context) {
itemArg := c.Query("item")
itemArg := strings.TrimSuffix(c.Param("item"), ".svg")
item, err := bench.TestItemsString(itemArg)
if err != nil {
slog.Error("Bad item URL argument", "arg", itemArg, "err", err)
slog.Error("Bad URL parameter", "param", itemArg, "err", err)
// TODO: what to do here?
return
}
tag := c.Query("tag")
tag := c.Param("tag")
cacheKey := tag + ":" + item.String()
chartCacheMutex.Lock()
ch, found := chartCache[cacheKey]
Expand Down Expand Up @@ -255,12 +269,15 @@ func pageFunction(page pageType) gin.HandlerFunc {
return func(c *gin.Context) {
pageData := &pageData{Data: data, Printer: language.Printer()}
if page == pageTest || page == pageHandler {
if tag := c.Query("tag"); tag == "" {
slog.Error("No URL argument", "arg", "tag")
} else if page == pageTest {
pageData.Test = bench.TestTag(tag)
} else if page == pageHandler {
pageData.Handler = bench.HandlerTag(tag)
if tag := c.Param("tag"); tag == "" {
slog.Error("No URL parameter", "param", "tag")
} else {
tag := strings.TrimSuffix(tag, ".html")
if page == pageTest {
pageData.Test = bench.TestTag(tag)
} else if page == pageHandler {
pageData.Handler = bench.HandlerTag(tag)
}
}
}
if err := templates[page].Execute(c.Writer, pageData); err != nil {
Expand Down
38 changes: 38 additions & 0 deletions docs/chart/Attributes/GbPerSec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions docs/chart/Attributes/MemAllocs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading