Skip to content

Commit

Permalink
新旧方式でベンチマークを取る
Browse files Browse the repository at this point in the history
Signed-off-by: drumato <[email protected]>
  • Loading branch information
Drumato committed Apr 11, 2024
1 parent 0b34dca commit 7599e4d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func proxy(w http.ResponseWriter, r *http.Request) {

body := io.NopCloser(bytes.NewBuffer(resBytes))
defer body.Close()
buf, err = convWebp(body, []string{})
buf, err = convWebp(body)
if err == nil {
defer buf.Reset()
w.Header().Set("Content-Type", "image/webp")
Expand Down
2 changes: 1 addition & 1 deletion webp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func doWebp(req *http.Request) (*http.Response, error) {
return orgRes, nil
}

func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) {
func convWebp(src io.Reader) (*bytes.Buffer, error) {
out, err := io.ReadAll(src)
if err != nil {
return nil, err
Expand Down
72 changes: 71 additions & 1 deletion webp_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"

"github.com/disintegration/imaging"
)

func TestProxyWebP(t *testing.T) {
Expand Down Expand Up @@ -48,9 +53,74 @@ func TestConvJPG2WebP(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = convWebp(resp.Body, []string{})
_, err = convWebp(resp.Body)
if err != nil {
t.Fatal(err)
}

}

func BenchmarkConvJPG2WebP_OldCwebpMethod(b *testing.B) {
tmpF, err := os.CreateTemp("/tmp", "")
if err != nil {
b.Fatal("failed to create tmp file")
}
defer tmpF.Close()
defer os.Remove(tmpF.Name())

f, err := os.Open("./testdata/oyaki.jpg")
if err != nil {
b.Fatal("failed to open testdata")
}
defer f.Close()

// to re-use src bytes
src, err := io.ReadAll(f)
if err != nil {
b.Fatal("failed to open testdata")
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
srcBuf := bytes.NewBuffer(src)
b.StartTimer()
img, err := imaging.Decode(srcBuf, imaging.AutoOrientation(true))
if err != nil {
b.Fail()
}

if err := imaging.Encode(tmpF, img, imaging.JPEG); err != nil {
b.Fail()
}

params := []string{"-quiet", "-mt", "-jpeg_like", f.Name(), "-o", "-"}
if _, err = exec.Command("cwebp", params...).Output(); err != nil {
b.Fail()
}
}
}

func BenchmarkConvJPG2WebP_bimg(b *testing.B) {
f, err := os.Open("./testdata/oyaki.jpg")
if err != nil {
b.Fatal("failed to open testdata")
}
defer f.Close()

// to re-use src bytes
src, err := io.ReadAll(f)
if err != nil {
b.Fatal("failed to open testdata")
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
srcBuf := bytes.NewBuffer(src)
b.StartTimer()
if _, err = convWebp(srcBuf); err != nil {
b.Fail()
}
}
}

0 comments on commit 7599e4d

Please sign in to comment.