(cmd *cobra.Command, options *benchOptions)
| 134 | } |
| 135 | |
| 136 | func 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 | } |
no test coverage detected