MCPcopy
hub / github.com/etcd-io/bbolt / benchFunc

Function benchFunc

cmd/bbolt/command/command_bench.go:136–193  ·  view source on GitHub ↗
(cmd *cobra.Command, options *benchOptions)

Source from the content-addressed store, hash-verified

134}
135
136func benchFunc(cmd *cobra.Command, options *benchOptions) error {
137 // Remove path if "-work" is not set. Otherwise keep path.
138 if options.work {
139 fmt.Fprintf(cmd.ErrOrStderr(), "work: %s\n", options.path)
140 } else {
141 defer os.Remove(options.path)
142 }
143
144 // Create database.
145 dbOptions := *bolt.DefaultOptions
146 dbOptions.PageSize = options.pageSize
147 dbOptions.InitialMmapSize = options.initialMmapSize
148 db, err := bolt.Open(options.path, 0600, &dbOptions)
149 if err != nil {
150 return err
151 }
152 db.NoSync = options.noSync
153 defer db.Close()
154
155 r := rand.New(rand.NewSource(time.Now().UnixNano()))
156
157 var writeResults benchResults
158
159 fmt.Fprintf(cmd.ErrOrStderr(), "starting write benchmark.\n")
160 keys, err := runWrites(cmd, db, options, &writeResults, r)
161 if err != nil {
162 return fmt.Errorf("write: %v", err)
163 }
164
165 if keys != nil {
166 r.Shuffle(len(keys), func(i, j int) {
167 keys[i], keys[j] = keys[j], keys[i]
168 })
169 }
170
171 var readResults benchResults
172 fmt.Fprintf(cmd.ErrOrStderr(), "starting read benchmark.\n")
173 // Read from the database.
174 if err := runReads(cmd, db, options, &readResults, keys); err != nil {
175 return fmt.Errorf("bench: read: %s", err)
176 }
177
178 // Print results.
179 if options.goBenchOutput {
180 // below replicates the output of testing.B benchmarks, e.g. for external tooling
181 benchWriteName := "BenchmarkWrite"
182 benchReadName := "BenchmarkRead"
183 maxLen := max(len(benchReadName), len(benchWriteName))
184 printGoBenchResult(cmd.OutOrStdout(), writeResults, maxLen, benchWriteName)
185 printGoBenchResult(cmd.OutOrStdout(), readResults, maxLen, benchReadName)
186 } else {
187 fmt.Fprintf(cmd.OutOrStdout(), "# Write\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", writeResults.getCompletedOps(), writeResults.getDuration(), writeResults.opDuration(), writeResults.opsPerSecond())
188 fmt.Fprintf(cmd.OutOrStdout(), "# Read\t%v(ops)\t%v\t(%v/op)\t(%v op/sec)\n", readResults.getCompletedOps(), readResults.getDuration(), readResults.opDuration(), readResults.opsPerSecond())
189 }
190 fmt.Fprintln(cmd.OutOrStdout(), "")
191
192 return nil
193}

Callers 1

newBenchCommandFunction · 0.85

Calls 9

getCompletedOpsMethod · 0.95
getDurationMethod · 0.95
opDurationMethod · 0.95
opsPerSecondMethod · 0.95
runWritesFunction · 0.85
runReadsFunction · 0.85
printGoBenchResultFunction · 0.85
ErrorfMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected