| 282 | // The type of the parameter to each bench(b) function. |
| 283 | // Provides n, the number of iterations. |
| 284 | @StarlarkBuiltin(name = "Benchmark") |
| 285 | private static class Benchmark implements StarlarkValue { |
| 286 | |
| 287 | private final String name; |
| 288 | private final StarlarkFunction f; |
| 289 | |
| 290 | // The cast assumes we use the "Sun" JVM, which measures per-thread allocation and CPU. |
| 291 | private final ThreadMXBean threadMX = (ThreadMXBean) ManagementFactory.getThreadMXBean(); |
| 292 | |
| 293 | // Starlark attributes |
| 294 | private int n; // requested number of iterations |
| 295 | |
| 296 | // current span (time0 != 0 => started) |
| 297 | private long cpu0; |
| 298 | private long alloc0; |
| 299 | private long time0; |
| 300 | private long steps0; |
| 301 | |
| 302 | // accumulators |
| 303 | private int count; // iterations |
| 304 | private long cpu; // CPU time (ns) in this thread |
| 305 | private long alloc; // bytes allocated by this thread |
| 306 | private long time; // wall time (ns) |
| 307 | private long steps; // Starlark computation steps |
| 308 | |
| 309 | private Benchmark(String name, StarlarkFunction f) { |
| 310 | this.name = name; |
| 311 | this.f = f; |
| 312 | } |
| 313 | |
| 314 | // Runs n iterations of this benchmark and reports success. |
| 315 | private boolean runIterations(StarlarkThread thread, int n) { |
| 316 | this.n = n; |
| 317 | try { |
| 318 | start(thread); |
| 319 | Starlark.positionalOnlyCall(thread, f, this); |
| 320 | stop(thread); |
| 321 | this.count += n; |
| 322 | |
| 323 | } catch (EvalException ex) { |
| 324 | System.err.println(ex.getMessageWithStack()); |
| 325 | return false; |
| 326 | |
| 327 | } catch ( |
| 328 | @SuppressWarnings("InterruptedExceptionSwallowed") |
| 329 | Throwable ex) { |
| 330 | // unhandled exception (incl. InterruptedException) |
| 331 | System.err.printf("In %s: %s\n", name, ex.getMessage()); |
| 332 | ex.printStackTrace(); |
| 333 | return false; |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | @StarlarkMethod(name = "n", doc = "Requested number of iterations.", structField = true) |
| 339 | public int n() { |
| 340 | return n; |
| 341 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…