go test -v -run=^$ -bench=Benchmark_Ctx_Body_With_Compression_Immutable -benchmem -count=4
(b *testing.B)
| 1374 | |
| 1375 | // go test -v -run=^$ -bench=Benchmark_Ctx_Body_With_Compression_Immutable -benchmem -count=4 |
| 1376 | func Benchmark_Ctx_Body_With_Compression_Immutable(b *testing.B) { |
| 1377 | encodingErr := errors.New("failed to encoding data") |
| 1378 | |
| 1379 | var ( |
| 1380 | compressGzip = func(data []byte) ([]byte, error) { |
| 1381 | var buf bytes.Buffer |
| 1382 | writer := gzip.NewWriter(&buf) |
| 1383 | if _, err := writer.Write(data); err != nil { |
| 1384 | return nil, encodingErr |
| 1385 | } |
| 1386 | if err := writer.Flush(); err != nil { |
| 1387 | return nil, encodingErr |
| 1388 | } |
| 1389 | if err := writer.Close(); err != nil { |
| 1390 | return nil, encodingErr |
| 1391 | } |
| 1392 | return buf.Bytes(), nil |
| 1393 | } |
| 1394 | compressDeflate = func(data []byte) ([]byte, error) { |
| 1395 | var buf bytes.Buffer |
| 1396 | writer := zlib.NewWriter(&buf) |
| 1397 | if _, err := writer.Write(data); err != nil { |
| 1398 | return nil, encodingErr |
| 1399 | } |
| 1400 | if err := writer.Flush(); err != nil { |
| 1401 | return nil, encodingErr |
| 1402 | } |
| 1403 | if err := writer.Close(); err != nil { |
| 1404 | return nil, encodingErr |
| 1405 | } |
| 1406 | return buf.Bytes(), nil |
| 1407 | } |
| 1408 | ) |
| 1409 | const input = "john=doe" |
| 1410 | compressionTests := []struct { |
| 1411 | compressWriter func([]byte) ([]byte, error) |
| 1412 | contentEncoding string |
| 1413 | expectedBody []byte |
| 1414 | }{ |
| 1415 | { |
| 1416 | contentEncoding: "gzip", |
| 1417 | compressWriter: compressGzip, |
| 1418 | expectedBody: []byte(input), |
| 1419 | }, |
| 1420 | { |
| 1421 | contentEncoding: "gzip,invalid", |
| 1422 | compressWriter: compressGzip, |
| 1423 | expectedBody: []byte(ErrUnsupportedMediaType.Error()), |
| 1424 | }, |
| 1425 | { |
| 1426 | contentEncoding: StrDeflate, |
| 1427 | compressWriter: compressDeflate, |
| 1428 | expectedBody: []byte(input), |
| 1429 | }, |
| 1430 | { |
| 1431 | contentEncoding: "gzip,deflate", |
| 1432 | compressWriter: func(data []byte) ([]byte, error) { |
| 1433 | var ( |