github.com/fretkak/mattermost-mattermost-server@v5.11.1+incompatible/app/file_bench_test.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"image"
    10  	"image/gif"
    11  	"image/jpeg"
    12  	"io"
    13  	"io/ioutil"
    14  	"math/rand"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/mattermost/mattermost-server/mlog"
    19  	"github.com/mattermost/mattermost-server/model"
    20  )
    21  
    22  var randomJPEG []byte
    23  var randomGIF []byte
    24  var zero10M = make([]byte, 10*1024*1024)
    25  var rgba *image.RGBA
    26  
    27  func prepareTestImages(tb testing.TB) {
    28  	if rgba != nil {
    29  		return
    30  	}
    31  
    32  	// Create a random image (pre-seeded for predictability)
    33  	rgba = image.NewRGBA(image.Rectangle{
    34  		image.Point{0, 0},
    35  		image.Point{2048, 2048},
    36  	})
    37  	_, err := rand.New(rand.NewSource(1)).Read(rgba.Pix)
    38  	if err != nil {
    39  		tb.Fatal(err)
    40  	}
    41  
    42  	// Encode it as JPEG and GIF
    43  	buf := &bytes.Buffer{}
    44  	err = jpeg.Encode(buf, rgba, &jpeg.Options{Quality: 50})
    45  	if err != nil {
    46  		tb.Fatal(err)
    47  	}
    48  	randomJPEG = buf.Bytes()
    49  
    50  	buf = &bytes.Buffer{}
    51  	err = gif.Encode(buf, rgba, nil)
    52  	if err != nil {
    53  		tb.Fatal(err)
    54  	}
    55  	randomGIF = buf.Bytes()
    56  }
    57  
    58  func BenchmarkUploadFile(b *testing.B) {
    59  	prepareTestImages(b)
    60  	th := Setup(b).InitBasic()
    61  	defer th.TearDown()
    62  	// disable logging in the benchmark, as best we can
    63  	th.App.Log.SetConsoleLevel(mlog.LevelError)
    64  	teamId := model.NewId()
    65  	channelId := model.NewId()
    66  	userId := model.NewId()
    67  
    68  	mb := func(i int) int {
    69  		return (i + 512*1024) / (1024 * 1024)
    70  	}
    71  
    72  	files := []struct {
    73  		title string
    74  		ext   string
    75  		data  []byte
    76  	}{
    77  		{fmt.Sprintf("random-%dMb-gif", mb(len(randomGIF))), ".gif", randomGIF},
    78  		{fmt.Sprintf("random-%dMb-jpg", mb(len(randomJPEG))), ".jpg", randomJPEG},
    79  		{fmt.Sprintf("zero-%dMb", mb(len(zero10M))), ".zero", zero10M},
    80  	}
    81  
    82  	file_benchmarks := []struct {
    83  		title string
    84  		f     func(b *testing.B, n int, data []byte, ext string)
    85  	}{
    86  		{
    87  			title: "raw-ish DoUploadFile",
    88  			f: func(b *testing.B, n int, data []byte, ext string) {
    89  				info1, err := th.App.DoUploadFile(time.Now(), teamId, channelId,
    90  					userId, fmt.Sprintf("BenchmarkDoUploadFile-%d%s", n, ext), data)
    91  				if err != nil {
    92  					b.Fatal(err)
    93  				}
    94  				<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
    95  				th.App.RemoveFile(info1.Path)
    96  
    97  			},
    98  		},
    99  		{
   100  			title: "raw UploadFileX Content-Length",
   101  			f: func(b *testing.B, n int, data []byte, ext string) {
   102  				info, aerr := th.App.UploadFileX(channelId,
   103  					fmt.Sprintf("BenchmarkUploadFileTask-%d%s", n, ext),
   104  					bytes.NewReader(data),
   105  					UploadFileSetTeamId(teamId),
   106  					UploadFileSetUserId(userId),
   107  					UploadFileSetTimestamp(time.Now()),
   108  					UploadFileSetContentLength(int64(len(data))),
   109  					UploadFileSetRaw())
   110  				if aerr != nil {
   111  					b.Fatal(aerr)
   112  				}
   113  				<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
   114  				th.App.RemoveFile(info.Path)
   115  			},
   116  		},
   117  		{
   118  			title: "raw UploadFileX chunked",
   119  			f: func(b *testing.B, n int, data []byte, ext string) {
   120  				info, aerr := th.App.UploadFileX(channelId,
   121  					fmt.Sprintf("BenchmarkUploadFileTask-%d%s", n, ext),
   122  					bytes.NewReader(data),
   123  					UploadFileSetTeamId(teamId),
   124  					UploadFileSetUserId(userId),
   125  					UploadFileSetTimestamp(time.Now()),
   126  					UploadFileSetContentLength(-1),
   127  					UploadFileSetRaw())
   128  				if aerr != nil {
   129  					b.Fatal(aerr)
   130  				}
   131  				<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
   132  				th.App.RemoveFile(info.Path)
   133  			},
   134  		},
   135  		{
   136  			title: "image UploadFiles",
   137  			f: func(b *testing.B, n int, data []byte, ext string) {
   138  				resp, err := th.App.UploadFiles(teamId, channelId, userId,
   139  					[]io.ReadCloser{ioutil.NopCloser(bytes.NewReader(data))},
   140  					[]string{fmt.Sprintf("BenchmarkDoUploadFiles-%d%s", n, ext)},
   141  					[]string{},
   142  					time.Now())
   143  				if err != nil {
   144  					b.Fatal(err)
   145  				}
   146  				<-th.App.Srv.Store.FileInfo().PermanentDelete(resp.FileInfos[0].Id)
   147  				th.App.RemoveFile(resp.FileInfos[0].Path)
   148  			},
   149  		},
   150  		{
   151  			title: "image UploadFileX Content-Length",
   152  			f: func(b *testing.B, n int, data []byte, ext string) {
   153  				info, aerr := th.App.UploadFileX(channelId,
   154  					fmt.Sprintf("BenchmarkUploadFileTask-%d%s", n, ext),
   155  					bytes.NewReader(data),
   156  					UploadFileSetTeamId(teamId),
   157  					UploadFileSetUserId(userId),
   158  					UploadFileSetTimestamp(time.Now()),
   159  					UploadFileSetContentLength(int64(len(data))))
   160  				if aerr != nil {
   161  					b.Fatal(aerr)
   162  				}
   163  				<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
   164  				th.App.RemoveFile(info.Path)
   165  			},
   166  		},
   167  		{
   168  			title: "image UploadFileX chunked",
   169  			f: func(b *testing.B, n int, data []byte, ext string) {
   170  				info, aerr := th.App.UploadFileX(channelId,
   171  					fmt.Sprintf("BenchmarkUploadFileTask-%d%s", n, ext),
   172  					bytes.NewReader(data),
   173  					UploadFileSetTeamId(teamId),
   174  					UploadFileSetUserId(userId),
   175  					UploadFileSetTimestamp(time.Now()),
   176  					UploadFileSetContentLength(int64(len(data))))
   177  				if aerr != nil {
   178  					b.Fatal(aerr)
   179  				}
   180  				<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
   181  				th.App.RemoveFile(info.Path)
   182  			},
   183  		},
   184  	}
   185  
   186  	for _, file := range files {
   187  		for _, fb := range file_benchmarks {
   188  			b.Run(file.title+"-"+fb.title, func(b *testing.B) {
   189  				for i := 0; i < b.N; i++ {
   190  					fb.f(b, i, file.data, file.ext)
   191  				}
   192  			})
   193  		}
   194  	}
   195  }