Skip to content

Commit

Permalink
add go benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Mar 5, 2024
1 parent 111df3a commit 1ea4d58
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
89 changes: 89 additions & 0 deletions benchmarks/go/csv-svg-path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"fmt"
"net/http"
"io"
"strings"
"strconv"
)

import _ "embed"

//go:embed static/Cargo.toml
var cargoToml string

var workerCount = 2
var workerPool chan struct{}

func main() {
// Create a buffered channel to limit the number of concurrent worker threads
workerPool = make(chan struct{}, workerCount)

// Handle requests using a single IO thread
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Acquire a worker thread from the pool
workerPool <- struct{}{}

// Handle the request in a goroutine
{
// Ensure the worker is released back to the pool when done
defer func() {
<-workerPool
}()

// Handle the actual request (in this case, just returning "Hello, World!")
handleRequest(w, r)
}
})

// Start the web server on port 8080
fmt.Println("Server listening on :8080")
http.ListenAndServe(":8080", nil)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}

pathString, err := generateSVGPath(string(body))
if err != nil {
http.Error(w, "Error processing request body", http.StatusBadRequest)
return
}

response := fmt.Sprintf(`<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<path d="%s" stroke="black" fill="transparent"/>
</svg>
`, pathString)

_, _ = w.Write([]byte(response))
}

func generateSVGPath(requestBody string) (string, error) {
lines := strings.Split(requestBody, "\n")

var pathString strings.Builder
pathString.WriteString("M 0 0 L")

for _, line := range lines {
parts := strings.SplitN(line, ", ", 2)
if len(parts) != 2 {
continue
}

x, errX := strconv.Atoi(parts[0])
y, errY := strconv.Atoi(parts[1])

if errX != nil || errY != nil {
return "", fmt.Errorf("invalid input format")
}

_, _ = fmt.Fprintf(&pathString, " %d %d", x, y)
}

return pathString.String(), nil
}
68 changes: 68 additions & 0 deletions benchmarks/go/varying-allocations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"fmt"
"net/http"
"strings"
"strconv"
)

var workerCount = 2
var workerPool chan struct{}

func main() {
// Create a buffered channel to limit the number of concurrent worker threads
workerPool = make(chan struct{}, workerCount)

// Handle requests using a single IO thread
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Acquire a worker thread from the pool
workerPool <- struct{}{}

// Handle the request in a goroutine
{
// Ensure the worker is released back to the pool when done
defer func() {
<-workerPool
}()

// Handle the actual request (in this case, just returning "Hello, World!")
handleRequest(w, r)
}
})

// Start the web server on port 8080
fmt.Println("Server listening on :8080")
http.ListenAndServe(":8080", nil)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
// Get the path from the URL
path := r.URL.Path

// Split the path using "/"
parts := strings.Split(path, "/")

// The last part of the path should be the number
if len(parts) > 0 {
lastPart := parts[len(parts)-1]

// Convert the last part to an integer
capacity, err := strconv.Atoi(lastPart)
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}

// Allocate an array of N bytes using make
byteArray := make([]byte, capacity)

for i := range byteArray {
byteArray[i] = 0xAA
}

_ = byteArray;
} else {
http.Error(w, "Invalid URL", http.StatusBadRequest)
}
}

0 comments on commit 1ea4d58

Please sign in to comment.