(Benchmark b, long budgetNanos, int iterations)
| 248 | // (which we may exceed by a factor of two) or number of iterations, |
| 249 | // exactly one of which must be nonnegative. Reports success. |
| 250 | private static boolean run(Benchmark b, long budgetNanos, int iterations) { |
| 251 | // Exactly one of the parameters must be specified. |
| 252 | Preconditions.checkState((budgetNanos >= 0) != (iterations >= 0)); |
| 253 | |
| 254 | Mutability mu = Mutability.create("test"); |
| 255 | StarlarkThread thread = StarlarkThread.createTransient(mu, semantics); |
| 256 | |
| 257 | // Run for a fixed number of iterations? |
| 258 | if (iterations >= 0) { |
| 259 | return b.runIterations(thread, iterations); |
| 260 | } |
| 261 | |
| 262 | // Run for a fixed amount of time (default behavior). |
| 263 | iterations = 1; |
| 264 | while (b.time < budgetNanos) { |
| 265 | if (!b.runIterations(thread, iterations)) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // Keep doubling the number of iterations until we exceed the deadline. |
| 270 | // TODO(adonovan): opt: extrapolate and predict the number of iterations |
| 271 | // in the remaining time budget, being wary of extrapolation error. |
| 272 | iterations <<= 1; |
| 273 | if (iterations <= 0) { // overflow |
| 274 | System.err.printf( |
| 275 | "In %s: function is too fast; likely a loop over `range(b.n)` is missing\n", b.name); |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | // The type of the parameter to each bench(b) function. |
| 283 | // Provides n, the number of iterations. |
no test coverage detected