djberg96 / sys-proctable

A cross-platform Ruby interface for gathering process information on your operating system
Apache License 2.0
150 stars 33 forks source link

[darwin] Add Sys::ProcTable::PROC_STRUCT_FIELD_MAP #66

Closed NickLaMuro closed 6 years ago

NickLaMuro commented 6 years ago

Pulled out from https://github.com/ManageIQ/manageiq.org/pull/640

Description

This is basically a memoization of mapping the keys for the intermediate FFI::Struct objects to the fields in the Sys::ProcTable::ProcTableStruct which avoids every pid being analyzed having to re-generate these values when .ps is called for all pids (duplicate work), and even saves a bit of work/mem when calling .ps for just one pids as well..

Benchmarks

Coming soon...

Well, this was merged already, but here are some benchmarks for funsies:

Objects allocated:

Benchmarks taken by doing the following:

$ irb
irb> require 'sys-proctable'
irb> pid = Process.pid
irb> GC.stat
#=> { :total_allocated_objects => 123, ... }
irb> Sys::ProcTable.ps(pid)
irb> GC.stat
#=> { :total_allocated_objects => 234, ... }
irb> 234 - 123

The above tests the gem when installed via gem install sys-proctable. To use the source, you would run rake install from this repo dir, and then require using require 'sys/proctable' instead.

Results:

Total Objects allocated
pre-patch 1947
post-patch 1649

IPS

Munged results from all four invocations of the bench/bench_ips_ps script:

Warming up --------------------------------------
Block form - pre patch
                         1.000  i/100ms
Non-block form - pre patch
                         1.000  i/100ms
Block form - post patch
                         1.000  i/100ms
Non-block form - post patch
                         1.000  i/100ms
Calculating -------------------------------------
Non-block form - pre patch
                          8.787  (±11.4%) i/s -     44.000  in   5.032858s
Block form - pre patch
                         10.513  (± 0.0%) i/s -     53.000  in   5.047212s
Non-block form - post patch
                         11.294  (± 0.0%) i/s -     57.000  in   5.053413s
Block form - post patch
                         12.657  (± 7.9%) i/s -     64.000  in   5.067014s

Comparison:
Block form - post patch:           12.7 i/s
Non-block form - post patch:       11.3 i/s - 1.12x  slower
Block form - pre patch:            10.5 i/s - 1.20x  slower
Non-block form - pre patch:        8.8 i/s  - 1.44x  slower
djberg96 commented 6 years ago

Not sure what the test failures are about. It's been a while, but I think it's a conflict with the new test-unit and the one in the stdlib. Interesting that it only seems to happen with 2.1 and earlier. I should probably just move everything over to rspec anyway.

Anyway, looks good.