Debian Codegraph @

stress_test/main.go

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