github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/persistent/add-slowdown (about)

     1  #!/usr/bin/env elvish
     2  # Parse an output of "go test -bench .", annotating benchmark results for
     3  # persistent operations with the slowdown ratio compared to their native
     4  # counterparts.
     5  use re
     6  
     7  fn extract [line]{
     8      # Extract the name and ns/op of a benchmark entry.
     9      fields = [(re:split '\s+' $line)]
    10      if (not (eq $fields[-1] ns/op)) {
    11          fail 'Last column of '(repr $line)' not ns/op'
    12      }
    13      put $fields[0] $fields[-2]
    14  }
    15  
    16  native = [&]
    17  
    18  each [line]{
    19      if (re:match Native $line) {
    20          # Remember the result so that it can be used later.
    21          name data = (extract $line)
    22          native[$name] = $data
    23      } elif (re:match Persistent $line) {
    24          # Calculate slowdown and append to the end of the line.
    25          name data = (extract $line)
    26          native-name = (re:replace Persistent Native $name)
    27          if (not (has-key $native $native-name)) {
    28              fail 'Native counterpart for '$name' not found'
    29          }
    30          line = $line' '(printf '%.2f' (/ $data $native[$native-name]))'x'
    31      }
    32      echo $line
    33  }