You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

package test
import (
"bytes"
"main/controllers"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func BenchmarkImportStudentInfo(b *testing.B) {
// 设置 Gin 测试模式
gin.SetMode(gin.TestMode)
// 创建一个 Gin 引擎
r := gin.Default()
r.POST("/import", controllers.ImportStudentInfoHandler)
// 模拟文件内容
fileContent := []byte("test file content")
body, boundary := createMultipartFormfile(b, "file", "/document/test.xlsx", fileContent)
// 创建一个 HTTP 请求
req := httptest.NewRequest(http.MethodPost, "/import", body)
req.Header.Set("Content-Type", "multipart/form-data; boundary="+boundary)
// 创建一个 HTTP 响应记录器
w := httptest.NewRecorder()
// 重置计时器
b.ResetTimer()
for i := 0; i < b.N; i++ {
// 处理请求
r.ServeHTTP(w, req)
// 检查响应状态码
require.Equal(b, http.StatusOK, w.Code)
}
}
// 模拟文件上传
func createMultipartFormfile(t *testing.B, fieldName, fileName string, fileContent []byte) (*bytes.Buffer, string) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(fieldName, fileName)
require.NoError(t, err)
part.Write(fileContent)
writer.Close()
return body, writer.Boundary()
}