1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
package main
// stress test to validate encoding/decoding doesn't crash or have inconsistent
// results due to gc interaction with cgo code. To run:
// go run stress_test/main.go -dir=<directory with JPEG images>
import (
"bytes"
"flag"
"fmt"
"image"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"github.com/kjk/golibjpegturbo"
"github.com/kr/fs"
)
func panicIfErr(err error) {
if err != nil {
panic(err.Error())
}
}
var (
nEncoded int32
nTotalEncodedSize int64
mu sync.Mutex
)
type ImageInfo struct {
path string
data []byte
img image.Image
encodedData []byte
}
func encodeLibjpeg(img image.Image) []byte {
var buf bytes.Buffer
options := &golibjpegturbo.Options{Quality: 90}
err := golibjpegturbo.Encode(&buf, img, options)
panicIfErr(err)
return buf.Bytes()
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func userHomeDir() string {
// user.Current() returns nil if cross-compiled e.g. on mac for linux
if usr, _ := user.Current(); usr != nil {
return usr.HomeDir
}
return os.Getenv("HOME")
}
func expandTildeInPath(s string) string {
if strings.HasPrefix(s, "~") {
return userHomeDir() + s[1:]
}
return s
}
func isJpegFile(path string) bool {
ext := filepath.Ext(path)
ext = strings.ToLower(ext)
return ext == ".jpg" || ext == ".jpeg"
}
func validateImgEq(img1, img2 image.Image) {
same := reflect.DeepEqual(img1, img2)
if !same {
panic("decoded image not consistent across runs")
}
}
func decodeEncodeWorker(c chan *ImageInfo) {
for ii := range c {
d := ii.data
r := bytes.NewReader(d)
img, err := golibjpegturbo.Decode(r)
if err != nil {
// we have decoded the image during setup, so this should always succeed
panic(fmt.Sprintf("failed to decode %s with %s\n", ii.path, err))
}
validateImgEq(ii.img, img)
encoded := encodeLibjpeg(img)
mu.Lock()
if ii.encodedData == nil {
ii.encodedData = encoded
}
mu.Unlock()
if !bytes.Equal(encoded, ii.encodedData) {
panic("encoded data not consistent across runs")
}
atomic.AddInt64(&nTotalEncodedSize, int64(len(encoded)))
n := atomic.AddInt32(&nEncoded, 1)
if n%100 == 0 {
fmt.Printf("Decoded/encoded %d images\n", n)
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var flagDir string
flag.StringVar(&flagDir, "dir", "", "directory with images")
flag.Parse()
if flagDir == "" {
flag.Usage()
os.Exit(2)
}
dir := expandTildeInPath(flagDir)
if !pathExists(dir) {
fmt.Printf("dir %s doesn't exist\n", dir)
flag.Usage()
os.Exit(2)
}
walker := fs.Walk(dir)
var imagePaths []string
nMaxImages := 100
for walker.Step() {
st := walker.Stat()
if !st.Mode().IsRegular() {
continue
}
path := walker.Path()
if !isJpegFile(path) {
continue
}
imagePaths = append(imagePaths, path)
if len(imagePaths) >= nMaxImages {
break
}
}
if len(imagePaths) == 0 {
fmt.Printf("There are no jpeg images in %s\n", dir)
flag.Usage()
os.Exit(2)
}
var images []*ImageInfo
for _, path := range imagePaths {
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("ioutil.ReadFile() failed with %s\n", err)
continue
}
img, err := golibjpegturbo.DecodeData(data)
if err != nil {
fmt.Printf("Failed to decode %s with %s\n", path, err)
continue
}
ii := &ImageInfo{
path: path,
data: data,
img: img,
}
images = append(images, ii)
}
fmt.Printf("Read %d images\n", len(images))
c := make(chan *ImageInfo)
nWorkers := runtime.NumCPU() - 2 // don't fully overload the machine
if nWorkers < 1 {
nWorkers = 1
}
fmt.Printf("Staring %d workers\n", nWorkers)
for i := 0; i < nWorkers; i++ {
go decodeEncodeWorker(c)
}
fmt.Printf("To stop me, use Ctrl-C. Otherwise, I'll just keep going\n")
i := 0
for {
c <- images[i]
i++
i = i % len(images)
}
}
|