mirror of
https://github.com/git-pkgs/proxy.git
synced 2026-11-09 15:49:16 -05:00
Cooldown only filtered the metadata. A composer.lock names the proxy's /files URL directly, so `composer install` fetched versions still inside the cooldown window without ever seeing the filtered metadata. npm already checks the publish time on tarball downloads. - Return the version's publish time with its dist URL, following the minified format's inheritance - Answer 404 for a version in cooldown before fetching its archive - Read the download lookup's metadata through ReadMetadata, so it has the same size limit as other metadata reads
81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// benchComposerMetadata builds minified Composer v2 metadata shaped like a
|
|
// large real package: thousands of versions, where the first carries every
|
|
// field and the rest mostly change version, time, dist and source, with the
|
|
// requirements changing every so often.
|
|
func benchComposerMetadata(versions int) []byte {
|
|
var b strings.Builder
|
|
b.WriteString(`{"minified":"composer/2.0","packages":{"big/sdk":[`)
|
|
for i := range versions {
|
|
if i > 0 {
|
|
b.WriteByte(',')
|
|
}
|
|
v := fmt.Sprintf("3.%d.0", versions-i)
|
|
ref := fmt.Sprintf("%040x", i)
|
|
b.WriteByte('{')
|
|
if i == 0 {
|
|
b.WriteString(`"name":"big/sdk","description":"A big SDK for a big cloud",` +
|
|
`"keywords":["cloud","sdk","api","storage","queue"],"homepage":"https://example.com/sdk",` +
|
|
`"license":["Apache-2.0"],"authors":[{"name":"Big Cloud","homepage":"https://example.com"}],` +
|
|
`"type":"library","autoload":{"psr-4":{"Big\\Sdk\\":"src/"},"files":["src/functions.php"]},` +
|
|
`"support":{"issues":"https://example.com/sdk/issues","source":"https://example.com/sdk/tree/main"},` +
|
|
`"extra":{"branch-alias":{"dev-master":"3.0-dev"}},`)
|
|
}
|
|
if i%50 == 0 {
|
|
b.WriteString(`"require":{"php":">=8.1",`)
|
|
for d := range 12 {
|
|
if d > 0 {
|
|
b.WriteByte(',')
|
|
}
|
|
fmt.Fprintf(&b, `"vendor/dep-%d":"^%d.%d"`, d, d+1, i/50)
|
|
}
|
|
b.WriteString(`},`)
|
|
}
|
|
fmt.Fprintf(&b, `"version":%q,"version_normalized":"%s.0","time":"2024-01-01T00:00:00+00:00",`+
|
|
`"source":{"url":"https://github.com/big/sdk.git","type":"git","reference":%q},`+
|
|
`"dist":{"url":"https://api.github.com/repos/big/sdk/zipball/%s","type":"zip","shasum":"","reference":%q}}`,
|
|
v, v, ref, ref, ref)
|
|
}
|
|
b.WriteString(`]}}`)
|
|
return []byte(b.String())
|
|
}
|
|
|
|
func BenchmarkComposerRewriteMetadata(b *testing.B) {
|
|
body := benchComposerMetadata(5000)
|
|
h := &ComposerHandler{proxy: testProxy(), proxyURL: "http://proxy.example"}
|
|
b.SetBytes(int64(len(body)))
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
if _, err := h.rewriteMetadata(body); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkComposerFindDownloadURL(b *testing.B) {
|
|
body := benchComposerMetadata(5000)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write(body)
|
|
}))
|
|
defer srv.Close()
|
|
h := &ComposerHandler{proxy: testProxy(), proxyURL: "http://proxy.example"}
|
|
h.proxy.HTTPClient = srv.Client()
|
|
b.SetBytes(int64(len(body)))
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
dist, err := h.findDownloadURLFromMetadata(context.Background(), srv.URL, "big/sdk", "3.2500.0")
|
|
if err != nil || dist.url == "" {
|
|
b.Fatalf("download URL not found: %q, %v", dist.url, err)
|
|
}
|
|
}
|
|
}
|