|
| 1 | +/* |
| 2 | + * Warp (C) 2019-2025 MinIO, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package bench |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "sync" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/minio/minio-go/v7" |
| 27 | +) |
| 28 | + |
| 29 | +// Append benchmarks upload speed via appends. |
| 30 | +type Append struct { |
| 31 | + Common |
| 32 | + prefixes map[string]struct{} |
| 33 | +} |
| 34 | + |
| 35 | +// Prepare will create an empty bucket or delete any content already there. |
| 36 | +func (u *Append) Prepare(ctx context.Context) error { |
| 37 | + return u.createEmptyBucket(ctx) |
| 38 | +} |
| 39 | + |
| 40 | +// Start will execute the main benchmark. |
| 41 | +// Operations should begin executing when the start channel is closed. |
| 42 | +func (u *Append) Start(ctx context.Context, wait chan struct{}) error { |
| 43 | + var wg sync.WaitGroup |
| 44 | + wg.Add(u.Concurrency) |
| 45 | + c := u.Collector |
| 46 | + if u.AutoTermDur > 0 { |
| 47 | + ctx = c.AutoTerm(ctx, "APPEND", u.AutoTermScale, autoTermCheck, autoTermSamples, u.AutoTermDur) |
| 48 | + } |
| 49 | + u.prefixes = make(map[string]struct{}, u.Concurrency) |
| 50 | + |
| 51 | + // Non-terminating context. |
| 52 | + nonTerm := context.Background() |
| 53 | + |
| 54 | + for i := 0; i < u.Concurrency; i++ { |
| 55 | + src := u.Source() |
| 56 | + u.prefixes[src.Prefix()] = struct{}{} |
| 57 | + go func(i int) { |
| 58 | + part := 1 |
| 59 | + tmp := src.Object() |
| 60 | + masterObj := *tmp |
| 61 | + |
| 62 | + rcv := c.Receiver() |
| 63 | + defer wg.Done() |
| 64 | + |
| 65 | + // Copy usermetadata and usertags per concurrent thread. |
| 66 | + opts := u.PutOpts |
| 67 | + opts.UserMetadata = make(map[string]string, len(u.PutOpts.UserMetadata)) |
| 68 | + opts.UserTags = make(map[string]string, len(u.PutOpts.UserTags)) |
| 69 | + // Only create 1 part on initial upload. |
| 70 | + opts.DisableMultipart = true |
| 71 | + for k, v := range u.PutOpts.UserMetadata { |
| 72 | + opts.UserMetadata[k] = v |
| 73 | + } |
| 74 | + for k, v := range u.PutOpts.UserTags { |
| 75 | + opts.UserTags[k] = v |
| 76 | + } |
| 77 | + aOpts := minio.AppendObjectOptions{ |
| 78 | + Progress: nil, |
| 79 | + ChunkSize: 0, |
| 80 | + DisableContentSha256: opts.DisableContentSha256, |
| 81 | + } |
| 82 | + |
| 83 | + done := ctx.Done() |
| 84 | + |
| 85 | + <-wait |
| 86 | + for { |
| 87 | + if part >= 10000 { |
| 88 | + tmp := src.Object() |
| 89 | + masterObj = *tmp |
| 90 | + part = 1 |
| 91 | + } |
| 92 | + |
| 93 | + select { |
| 94 | + case <-done: |
| 95 | + return |
| 96 | + default: |
| 97 | + } |
| 98 | + |
| 99 | + if u.rpsLimit(ctx) != nil { |
| 100 | + return |
| 101 | + } |
| 102 | + obj := src.Object() |
| 103 | + obj.Name = masterObj.Name |
| 104 | + obj.Prefix = masterObj.Prefix |
| 105 | + obj.ContentType = masterObj.ContentType |
| 106 | + |
| 107 | + opts.ContentType = obj.ContentType |
| 108 | + client, cldone := u.Client() |
| 109 | + op := Operation{ |
| 110 | + OpType: "APPEND", |
| 111 | + Thread: uint16(i), |
| 112 | + Size: obj.Size, |
| 113 | + ObjPerOp: 1, |
| 114 | + File: obj.Name, |
| 115 | + Endpoint: client.EndpointURL().String(), |
| 116 | + } |
| 117 | + |
| 118 | + op.Start = time.Now() |
| 119 | + var err error |
| 120 | + var res minio.UploadInfo |
| 121 | + if part == 1 { |
| 122 | + res, err = client.PutObject(nonTerm, u.Bucket, obj.Name, obj.Reader, obj.Size, opts) |
| 123 | + } else { |
| 124 | + res, err = client.AppendObject(nonTerm, u.Bucket, obj.Name, obj.Reader, obj.Size, aOpts) |
| 125 | + } |
| 126 | + op.End = time.Now() |
| 127 | + if err != nil { |
| 128 | + u.Error("upload error: ", err) |
| 129 | + op.Err = err.Error() |
| 130 | + } |
| 131 | + |
| 132 | + if res.Size != int64(part)*obj.Size && op.Err == "" { |
| 133 | + err := fmt.Sprint("part ", part, " short upload. want:", int64(part)*obj.Size, ", got:", res.Size) |
| 134 | + if op.Err == "" { |
| 135 | + op.Err = err |
| 136 | + } |
| 137 | + u.Error(err) |
| 138 | + } |
| 139 | + part++ |
| 140 | + cldone() |
| 141 | + rcv <- op |
| 142 | + } |
| 143 | + }(i) |
| 144 | + } |
| 145 | + wg.Wait() |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +// Cleanup deletes everything uploaded to the bucket. |
| 150 | +func (u *Append) Cleanup(ctx context.Context) { |
| 151 | + pf := make([]string, 0, len(u.prefixes)) |
| 152 | + for p := range u.prefixes { |
| 153 | + pf = append(pf, p) |
| 154 | + } |
| 155 | + u.deleteAllInBucket(ctx, pf...) |
| 156 | +} |
0 commit comments