Skip to content

Commit 827a0c9

Browse files
author
mirkobrombin
committed
chore: new -b/--benchmark flag
1 parent 5845b74 commit 827a0c9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

internal/cli/cli.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
var tuiMode bool
16+
var benchMode bool
1617

1718
// rootCmd represents the base command when called without any subcommands.
1819
var rootCmd = &cobra.Command{
@@ -37,6 +38,7 @@ func init() {
3738
rootCmd.AddCommand(pluginsCmd)
3839

3940
startCmd.Flags().BoolVarP(&tuiMode, "tui", "t", false, "Enable TUI mode")
41+
startCmd.Flags().BoolVarP(&benchMode, "bench", "b", false, "Enable benchmark mode")
4042
}
4143

4244
var generateCmd = &cobra.Command{
@@ -158,7 +160,7 @@ func start(cmd *cobra.Command, args []string) {
158160
}
159161

160162
fmt.Println("Starting servers...")
161-
server.StartServers(configs, tuiMode)
163+
server.StartServers(configs, tuiMode, benchMode)
162164

163165
// Wait indefinitely if not in TUI mode, the servers will keep running
164166
// and loggers will keep writing to both the stdout and the log files.

internal/server/middleware/middleware.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"fmt"
45
"net/http"
56
"time"
67

@@ -56,6 +57,33 @@ func TimeoutMiddleware(timeout time.Duration) MiddlewareFunc {
5657
}
5758
}
5859

60+
// BenchmarkMiddleware logs the duration of HTTP requests.
61+
func BenchmarkMiddleware() MiddlewareFunc {
62+
return func(next http.Handler) http.Handler {
63+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64+
start := time.Now()
65+
66+
next.ServeHTTP(w, r)
67+
68+
duration := time.Since(start)
69+
formattedDuration := formatDuration(duration)
70+
fmt.Printf("\033[33;40m⏲ Benchmark: %s %s completed in %s\033[0m\n", r.Method, r.URL.Path, formattedDuration)
71+
})
72+
}
73+
}
74+
75+
// formatDuration formats a time.Duration to a human-readable string.
76+
func formatDuration(d time.Duration) string {
77+
switch {
78+
case d < time.Millisecond:
79+
return fmt.Sprintf("%dµs", d.Microseconds())
80+
case d < time.Second:
81+
return fmt.Sprintf("%.3fms", float64(d.Microseconds())/1000)
82+
default:
83+
return fmt.Sprintf("%.3fs", d.Seconds())
84+
}
85+
}
86+
5987
// responseWriter wraps http.ResponseWriter to capture the status code.
6088
type responseWriter struct {
6189
http.ResponseWriter

internal/server/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
)
2323

2424
// StartServers starts the servers based on the provided configurations.
25-
func StartServers(configs []config.SiteConfig, enableTUI bool) {
25+
func StartServers(configs []config.SiteConfig, enableTUI bool, enableBench bool) {
2626
tuiMode = enableTUI
2727

2828
// FIXME: move all TUI related code out of this package, I do not feel
@@ -69,6 +69,11 @@ func StartServers(configs []config.SiteConfig, enableTUI bool) {
6969
// Initialize the global middleware manager
7070
mwManager := middleware.NewMiddlewareManager()
7171

72+
// Enable benchmark middleware if requested
73+
if enableBench {
74+
mwManager.Use(middleware.BenchmarkMiddleware())
75+
}
76+
7277
// Initialize the plugins with the global middleware manager
7378
pluginManager := plugin.GetPluginManagerInstance()
7479
if err := pluginManager.InitPlugins(mwManager); err != nil {

0 commit comments

Comments
 (0)