Skip to content

Commit 6ef71c0

Browse files
committed
go multithread
1 parent 26f768b commit 6ef71c0

2,024 files changed

Lines changed: 6902450 additions & 14609 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

frameworks/bun/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function handleRequest(req: Request): Response | Promise<Response> {
9898
}));
9999
const body = JSON.stringify({ items, count: items.length });
100100
return new Response(body, {
101-
headers: { "content-type": "application/json" },
101+
headers: { "content-type": "application/json", "content-length": String(Buffer.byteLength(body)) },
102102
});
103103
} catch (e: any) {
104104
return new Response(e.message || "db error", { status: 500 });

frameworks/go-fasthttp/Dockerfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
FROM golang:1.22-alpine AS build
1+
FROM golang:1.24-alpine AS build
22
WORKDIR /app
3-
COPY go.mod go.sum ./
4-
RUN go mod download
5-
COPY main.go .
6-
RUN CGO_ENABLED=0 go build -o server main.go
3+
COPY . .
4+
RUN CGO_ENABLED=0 go build -mod=vendor -o server main.go
75

86
FROM alpine:3.19
97
COPY --from=build /app/server /server

frameworks/go-fasthttp/go.mod

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
module httparena/go-fasthttp
22

3-
go 1.22
3+
go 1.24.0
44

5-
require github.com/valyala/fasthttp v1.58.0
5+
toolchain go1.24.13
6+
7+
require (
8+
github.com/valyala/fasthttp v1.58.0
9+
modernc.org/sqlite v1.46.1
10+
)
611

712
require (
813
github.com/andybalholm/brotli v1.1.1 // indirect
14+
github.com/dustin/go-humanize v1.0.1 // indirect
15+
github.com/google/uuid v1.6.0 // indirect
916
github.com/klauspost/compress v1.17.11 // indirect
17+
github.com/mattn/go-isatty v0.0.20 // indirect
18+
github.com/ncruces/go-strftime v1.0.0 // indirect
19+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
1020
github.com/valyala/bytebufferpool v1.0.0 // indirect
21+
github.com/valyala/tcplisten v1.0.0 // indirect
22+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
23+
golang.org/x/sys v0.37.0 // indirect
24+
modernc.org/libc v1.67.6 // indirect
25+
modernc.org/mathutil v1.7.1 // indirect
26+
modernc.org/memory v1.11.0 // indirect
1127
)

frameworks/go-fasthttp/go.sum

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
22
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
3+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
4+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
5+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
6+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
7+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
10+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
311
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
412
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
13+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
14+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
15+
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
16+
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
17+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
18+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
519
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
620
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
721
github.com/valyala/fasthttp v1.58.0 h1:GGB2dWxSbEprU9j0iMJHgdKYJVDyjrOwF9RE59PbRuE=
822
github.com/valyala/fasthttp v1.58.0/go.mod h1:SYXvHHaFp7QZHGKSHmoMipInhrI5StHrhDTYVEjK/Kw=
23+
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
24+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
925
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
1026
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
27+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
28+
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
29+
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
30+
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
31+
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
32+
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
33+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
35+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
36+
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
37+
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
38+
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
39+
modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
40+
modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc=
41+
modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM=
42+
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
43+
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
44+
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
45+
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
46+
modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE=
47+
modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
48+
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
49+
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
50+
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
51+
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
52+
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
53+
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
54+
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
55+
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
56+
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
57+
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
58+
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
59+
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
60+
modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU=
61+
modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
62+
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
63+
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
64+
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
65+
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

frameworks/go-fasthttp/main.go

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package main
22

33
import (
4+
"database/sql"
45
"encoding/json"
6+
"fmt"
7+
"hash/crc32"
8+
"log"
59
"math"
610
"os"
11+
"runtime"
712
"strconv"
13+
"sync"
814

915
"compress/flate"
1016

1117
"github.com/valyala/fasthttp"
18+
"github.com/valyala/fasthttp/reuseport"
19+
_ "modernc.org/sqlite"
1220
)
1321

1422
type Rating struct {
@@ -46,6 +54,7 @@ type ProcessResponse struct {
4654

4755
var dataset []DatasetItem
4856
var jsonLargeResponse []byte
57+
var db *sql.DB
4958

5059
func loadDataset() {
5160
path := os.Getenv("DATASET_PATH")
@@ -139,11 +148,78 @@ func processHandler(ctx *fasthttp.RequestCtx) {
139148
ctx.SetBody(body)
140149
}
141150

151+
func loadDB() {
152+
d, err := sql.Open("sqlite", "file:/data/benchmark.db?mode=ro&immutable=1")
153+
if err != nil {
154+
return
155+
}
156+
d.SetMaxOpenConns(runtime.NumCPU())
157+
db = d
158+
}
159+
160+
func uploadHandler(ctx *fasthttp.RequestCtx) {
161+
body := ctx.PostBody()
162+
checksum := crc32.ChecksumIEEE(body)
163+
ctx.Response.Header.Set("Server", "go-fasthttp")
164+
ctx.SetContentType("text/plain")
165+
ctx.SetBodyString(fmt.Sprintf("%08x", checksum))
166+
}
167+
168+
func dbHandler(ctx *fasthttp.RequestCtx) {
169+
if db == nil {
170+
ctx.SetStatusCode(500)
171+
ctx.SetBodyString("DB not available")
172+
return
173+
}
174+
minPrice := 10.0
175+
maxPrice := 50.0
176+
if v := ctx.QueryArgs().Peek("min"); len(v) > 0 {
177+
if f, err := strconv.ParseFloat(string(v), 64); err == nil {
178+
minPrice = f
179+
}
180+
}
181+
if v := ctx.QueryArgs().Peek("max"); len(v) > 0 {
182+
if f, err := strconv.ParseFloat(string(v), 64); err == nil {
183+
maxPrice = f
184+
}
185+
}
186+
rows, err := db.Query("SELECT id, name, category, price, quantity, active, tags, rating_score, rating_count FROM items WHERE price BETWEEN ? AND ? LIMIT 50", minPrice, maxPrice)
187+
if err != nil {
188+
ctx.SetStatusCode(500)
189+
ctx.SetBodyString("Query failed")
190+
return
191+
}
192+
defer rows.Close()
193+
var items []map[string]interface{}
194+
for rows.Next() {
195+
var id, quantity, active, ratingCount int
196+
var name, category, tags string
197+
var price, ratingScore float64
198+
if err := rows.Scan(&id, &name, &category, &price, &quantity, &active, &tags, &ratingScore, &ratingCount); err != nil {
199+
continue
200+
}
201+
var tagsArr []string
202+
json.Unmarshal([]byte(tags), &tagsArr)
203+
items = append(items, map[string]interface{}{
204+
"id": id, "name": name, "category": category,
205+
"price": price, "quantity": quantity, "active": active == 1,
206+
"tags": tagsArr,
207+
"rating": map[string]interface{}{"score": ratingScore, "count": ratingCount},
208+
})
209+
}
210+
resp := map[string]interface{}{"items": items, "count": len(items)}
211+
ctx.Response.Header.Set("Server", "go-fasthttp")
212+
ctx.SetContentType("application/json")
213+
body, _ := json.Marshal(resp)
214+
ctx.SetBody(body)
215+
}
216+
142217
var compressedHandler fasthttp.RequestHandler
143218

144219
func main() {
145220
loadDataset()
146221
loadDatasetLarge()
222+
loadDB()
147223

148224
compressedHandler = fasthttp.CompressHandlerLevel(func(ctx *fasthttp.RequestCtx) {
149225
ctx.Response.Header.Set("Server", "go-fasthttp")
@@ -159,12 +235,30 @@ func main() {
159235
processHandler(ctx)
160236
case "/compression":
161237
compressedHandler(ctx)
238+
case "/upload":
239+
uploadHandler(ctx)
240+
case "/db":
241+
dbHandler(ctx)
162242
default:
163243
baseline11Handler(ctx)
164244
}
165245
}
166-
server := &fasthttp.Server{
167-
Handler: handler,
246+
numCPU := runtime.NumCPU()
247+
var wg sync.WaitGroup
248+
for i := 0; i < numCPU; i++ {
249+
wg.Add(1)
250+
go func() {
251+
defer wg.Done()
252+
ln, err := reuseport.Listen("tcp4", ":8080")
253+
if err != nil {
254+
log.Fatal(err)
255+
}
256+
s := &fasthttp.Server{
257+
Handler: handler,
258+
MaxRequestBodySize: 25 * 1024 * 1024, // 25 MB
259+
}
260+
s.Serve(ln)
261+
}()
168262
}
169-
server.ListenAndServe(":8080")
263+
wg.Wait()
170264
}

frameworks/go-fasthttp/meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"noisy",
1212
"limited-conn",
1313
"json",
14-
"compression"
14+
"compression",
15+
"upload",
16+
"mixed"
1517
]
1618
}

frameworks/go-fasthttp/vendor/github.com/andybalholm/brotli/LICENSE

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/go-fasthttp/vendor/github.com/andybalholm/brotli/README.md

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)