KristofferC / TimerOutputs.jl

Formatted output of timed sections in Julia
Other
659 stars 54 forks source link

add `todict` function #139

Closed ericphanson closed 2 years ago

ericphanson commented 3 years ago

This allows serialization with JSON3:

julia> to
 ──────────────────────────────────────────────────────────────────────
                               Time                   Allocations      
                       ──────────────────────   ───────────────────────
   Tot / % measured:         222s / 0.52%           1.19GiB / 0.04%    

 Section       ncalls     time   %tot     avg     alloc   %tot      avg
 ──────────────────────────────────────────────────────────────────────
 nest 2             1    705ms  61.6%   705ms   2.09KiB  0.42%  2.09KiB
   level 2.2        1    401ms  35.1%   401ms      176B  0.03%     176B
   level 2.1        1    303ms  26.5%   303ms      176B  0.03%     176B
 nest 1             1    439ms  38.4%   439ms    498KiB  100%    498KiB
   level 2.2        1    207ms  18.1%   207ms      176B  0.03%     176B
   level 2.1        3    117ms  10.3%  39.2ms   8.73KiB  1.75%  2.91KiB
 ──────────────────────────────────────────────────────────────────────

julia> JSON3.write(to)
"{\"total_time_ns\":1144369999,\"total_allocated_bytes\":512511,\"time_ns\":0,\"n_calls\":0,\"allocated_bytes\":0,\"inner_timers\":{\"nest 1\":{\"total_time_ns\":324575583,\"total_allocated_bytes\":9120,\"time_ns\":439140583,\"n_calls\":1,\"allocated_bytes\":510367,\"inner_timers\":{\"level 2.1\":{\"total_time_ns\":0,\"total_allocated_bytes\":0,\"time_ns\":117488250,\"n_calls\":3,\"allocated_bytes\":8944,\"inner_timers\":{}},\"level 2.2\":{\"total_time_ns\":0,\"total_allocated_bytes\":0,\"time_ns\":207087333,\"n_calls\":1,\"allocated_bytes\":176,\"inner_timers\":{}}}},\"nest 2\":{\"total_time_ns\":704607667,\"total_allocated_bytes\":352,\"time_ns\":705229416,\"n_calls\":1,\"allocated_bytes\":2144,\"inner_timers\":{\"level 2.1\":{\"total_time_ns\":0,\"total_allocated_bytes\":0,\"time_ns\":303114542,\"n_calls\":1,\"allocated_bytes\":176,\"inner_timers\":{}},\"level 2.2\":{\"total_time_ns\":0,\"total_allocated_bytes\":0,\"time_ns\":401493125,\"n_calls\":1,\"allocated_bytes\":176,\"inner_timers\":{}}}}}}"

julia> JSON3.pretty(to)
{
           "total_time_ns": 1144369999,
   "total_allocated_bytes": 512511,
                 "time_ns": 0,
                 "n_calls": 0,
         "allocated_bytes": 0,
            "inner_timers": {
                               "nest 1": {
                                                    "total_time_ns": 324575583,
                                            "total_allocated_bytes": 9120,
                                                          "time_ns": 439140583,
                                                          "n_calls": 1,
                                                  "allocated_bytes": 510367,
                                                     "inner_timers": {
                                                                        "level 2.1": {
                                                                                                "total_time_ns": 0,
                                                                                        "total_allocated_bytes": 0,
                                                                                                      "time_ns": 117488250,
                                                                                                      "n_calls": 3,
                                                                                              "allocated_bytes": 8944,
                                                                                                 "inner_timers": {}
                                                                                     },
                                                                        "level 2.2": {
                                                                                                "total_time_ns": 0,
                                                                                        "total_allocated_bytes": 0,
                                                                                                      "time_ns": 207087333,
                                                                                                      "n_calls": 1,
                                                                                              "allocated_bytes": 176,
                                                                                                 "inner_timers": {}
                                                                                     }
                                                                     }
                                         },
                               "nest 2": {
                                                    "total_time_ns": 704607667,
                                            "total_allocated_bytes": 352,
                                                          "time_ns": 705229416,
                                                          "n_calls": 1,
                                                  "allocated_bytes": 2144,
                                                     "inner_timers": {
                                                                        "level 2.1": {
                                                                                                "total_time_ns": 0,
                                                                                        "total_allocated_bytes": 0,
                                                                                                      "time_ns": 303114542,
                                                                                                      "n_calls": 1,
                                                                                              "allocated_bytes": 176,
                                                                                                 "inner_timers": {}
                                                                                     },
                                                                        "level 2.2": {
                                                                                                "total_time_ns": 0,
                                                                                        "total_allocated_bytes": 0,
                                                                                                      "time_ns": 401493125,
                                                                                                      "n_calls": 1,
                                                                                              "allocated_bytes": 176,
                                                                                                 "inner_timers": {}
                                                                                     }
                                                                     }
                                         }
                            }
}

This is not enough to be able to de-serialize back into a TimerOutput object, but for my purposes (logging out timers), it's sufficient.

KristofferC commented 3 years ago

I would rather just add a todict function that just creates the dictionary. And then people can serialize it using JSON or whatever.

ericphanson commented 2 years ago

bump

ericphanson commented 2 years ago

bump

KristofferC commented 2 years ago

Thanks for the bump!

KristofferC commented 2 years ago

This will need some docs in the README so that people can use it.