github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/tools/client_test.go (about) 1 // Package tools provides common tools and utilities for all unit and integration tests 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package tools_test 6 7 import ( 8 "bufio" 9 "fmt" 10 "io" 11 "net/http" 12 "net/http/httptest" 13 "os" 14 "path" 15 "testing" 16 17 "github.com/NVIDIA/aistore/api" 18 "github.com/NVIDIA/aistore/api/apc" 19 "github.com/NVIDIA/aistore/cmn" 20 "github.com/NVIDIA/aistore/cmn/cos" 21 "github.com/NVIDIA/aistore/memsys" 22 "github.com/NVIDIA/aistore/tools" 23 "github.com/NVIDIA/aistore/tools/readers" 24 "github.com/NVIDIA/aistore/tools/trand" 25 ) 26 27 var bp api.BaseParams 28 29 func TestPutFile(t *testing.T) { 30 err := putFile(1024, cos.ChecksumXXHash) 31 if err != nil { 32 t.Fatal("Put file failed", err) 33 } 34 } 35 36 func TestPutSG(t *testing.T) { 37 size := int64(10) 38 mmsa := memsys.ByteMM() 39 sgl := mmsa.NewSGL(size) 40 defer sgl.Free() 41 42 err := putSG(sgl, size, cos.ChecksumXXHash) 43 if err != nil { 44 t.Fatal(err) 45 } 46 } 47 48 func putFile(size int64, cksumType string) error { 49 fn := "ais-client-test-" + trand.String(32) 50 dir := "/tmp" 51 r, err := readers.NewRandFile(dir, fn, size, cksumType) 52 if err != nil { 53 return err 54 } 55 putArgs := api.PutArgs{ 56 BaseParams: bp, 57 Bck: cmn.Bck{Name: "bucket", Provider: apc.AIS}, 58 ObjName: "key", 59 Cksum: r.Cksum(), 60 Reader: r, 61 } 62 _, err = api.PutObject(&putArgs) 63 os.Remove(path.Join(dir, fn)) 64 return err 65 } 66 67 func putRand(size int64, cksumType string) error { 68 r, err := readers.NewRand(size, cksumType) 69 if err != nil { 70 return err 71 } 72 putArgs := api.PutArgs{ 73 BaseParams: bp, 74 Bck: cmn.Bck{Name: "bucket", Provider: apc.AIS}, 75 ObjName: "key", 76 Cksum: r.Cksum(), 77 Reader: r, 78 } 79 _, err = api.PutObject(&putArgs) 80 return err 81 } 82 83 func putSG(sgl *memsys.SGL, size int64, cksumType string) error { 84 sgl.Reset() 85 r, err := readers.NewSG(sgl, size, cksumType) 86 if err != nil { 87 return err 88 } 89 putArgs := api.PutArgs{ 90 BaseParams: bp, 91 Bck: cmn.Bck{Name: "bucket", Provider: apc.AIS}, 92 ObjName: "key", 93 Cksum: r.Cksum(), 94 Reader: r, 95 } 96 _, err = api.PutObject(&putArgs) 97 return err 98 } 99 100 func BenchmarkPutFileWithHash1M(b *testing.B) { 101 for range b.N { 102 err := putFile(1024*1024, cos.ChecksumXXHash) 103 if err != nil { 104 b.Fatal(err) 105 } 106 } 107 } 108 109 func BenchmarkPutRandWithHash1M(b *testing.B) { 110 for range b.N { 111 err := putRand(1024*1024, cos.ChecksumXXHash) 112 if err != nil { 113 b.Fatal(err) 114 } 115 } 116 } 117 118 func BenchmarkPutSGWithHash1M(b *testing.B) { 119 mmsa := memsys.PageMM() 120 sgl := mmsa.NewSGL(cos.MiB) 121 defer sgl.Free() 122 123 for range b.N { 124 err := putSG(sgl, 1024*1024, cos.ChecksumXXHash) 125 if err != nil { 126 b.Fatal(err) 127 } 128 } 129 } 130 131 func BenchmarkPutFileNoHash1M(b *testing.B) { 132 for range b.N { 133 err := putFile(1024*1024, cos.ChecksumNone) 134 if err != nil { 135 b.Fatal(err) 136 } 137 } 138 } 139 140 func BenchmarkPutRandNoHash1M(b *testing.B) { 141 for range b.N { 142 err := putRand(1024*1024, cos.ChecksumNone) 143 if err != nil { 144 b.Fatal(err) 145 } 146 } 147 } 148 149 func BenchmarkPutSGNoHash1M(b *testing.B) { 150 mmsa := memsys.PageMM() 151 sgl := mmsa.NewSGL(cos.MiB) 152 defer sgl.Free() 153 154 for range b.N { 155 err := putSG(sgl, 1024*1024, cos.ChecksumNone) 156 if err != nil { 157 b.Fatal(err) 158 } 159 } 160 } 161 162 func BenchmarkPutFileWithHash1MParallel(b *testing.B) { 163 b.RunParallel(func(pb *testing.PB) { 164 for pb.Next() { 165 err := putFile(1024*1024, cos.ChecksumXXHash) 166 if err != nil { 167 b.Fatal(err) 168 } 169 } 170 }) 171 } 172 173 func BenchmarkPutRandWithHash1MParallel(b *testing.B) { 174 b.RunParallel(func(pb *testing.PB) { 175 for pb.Next() { 176 err := putRand(1024*1024, cos.ChecksumXXHash) 177 if err != nil { 178 b.Fatal(err) 179 } 180 } 181 }) 182 } 183 184 func BenchmarkPutSGWithHash1MParallel(b *testing.B) { 185 b.RunParallel(func(pb *testing.PB) { 186 mmsa, _ := memsys.NewMMSA("dev-tools", false) 187 sgl := mmsa.NewSGL(cos.MiB) 188 defer func() { 189 mmsa.Terminate(false) 190 }() 191 192 for pb.Next() { 193 err := putSG(sgl, 1024*1024, cos.ChecksumXXHash) 194 if err != nil { 195 b.Fatal(err) 196 } 197 } 198 }) 199 } 200 201 func TestMain(m *testing.M) { 202 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 203 defer func() { 204 io.Copy(io.Discard, bufio.NewReader(r.Body)) 205 r.Body.Close() 206 }() 207 208 errCb := func(statusCode int, f string, a ...any) { 209 w.WriteHeader(statusCode) 210 fmt.Fprintf(w, f, a...) 211 } 212 213 // Verify checksum. 214 var ( 215 cksumType = r.Header.Get(apc.HdrObjCksumType) 216 cksumValue = r.Header.Get(apc.HdrObjCksumVal) 217 ) 218 _, cksum, err := cos.CopyAndChecksum(io.Discard, r.Body, nil, cksumType) 219 if err != nil { 220 errCb(http.StatusBadRequest, "server failed to read, error %v", err) 221 return 222 } 223 if cksum != nil && cksum.Value() != cksumValue { 224 errCb(http.StatusNotAcceptable, "cksum mismatch got: %q, expected: %q", cksum.Value(), cksumValue) 225 } 226 })) 227 bp = tools.BaseAPIParams(srv.URL) 228 229 m.Run() 230 srv.Close() 231 }