t000755001750001750 015174311231 16402 5ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006ps.t100644001750001750 556615174311231 17365 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::V0; BEGIN { if ($^O eq 'MSWin32') { plan skip_all => "ps test requires a unix shell"; } my $probe = qx{echo hello 2>/dev/null}; unless (defined $probe && $probe =~ /hello/) { plan skip_all => "shell echo unavailable"; } } use Test2::Plugin::MemUsage; subtest happy_path => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::ps_command = sub { q{echo ' 1234 5678'} }; my %mem = Test2::Plugin::MemUsage::_collect_ps(); is($mem{rss}, ['1234', 'kB'], "rss"); is($mem{size}, ['5678', 'kB'], "size"); is($mem{peak}, ['NA', ''], "peak NA (ps does not surface)"); }; subtest empty_output => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::ps_command = sub { 'true' }; my @out = Test2::Plugin::MemUsage::_collect_ps(); is(\@out, [], "empty output -> empty"); }; subtest unparseable_output => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::ps_command = sub { q{echo 'definitely not numbers'} }; my @out = Test2::Plugin::MemUsage::_collect_ps(); is(\@out, [], "unparseable -> empty"); }; # Real-system compatibility: invoke the actual ps_command and verify # its output shape matches what our mocks/parser assume. Skip cleanly # when ps is missing (exit 127 / $? == -1); fail loudly when ps is # present but exits non-zero or returns output we cannot parse. subtest real_ps_matches_mock_shape => sub { my $cmd = Test2::Plugin::MemUsage::ps_command(); my $out = qx{$cmd 2>/dev/null}; my $raw = $?; skip_all "ps not executable: $!" if $raw == -1; skip_all "ps exited non-zero (raw=$raw)" if $raw != 0; skip_all "ps output not parseable: $out" unless $out =~ /^\s*\d+\s+\d+\s*$/m; is($raw >> 8, 0, "ps exits 0"); like($out, qr/^\s*\d+\s+\d+\s*$/m, "ps output matches our parser regex"); }; # Real-system integration: drive _collect_ps end to end against the # host's ps and assert the returned values are numeric, positive, # correctly unitted, and within a sane range. Same skip rules as # above; if ps is present and the data is wrong, fail. subtest real_collect_ps_meaningful => sub { my $cmd = Test2::Plugin::MemUsage::ps_command(); my $probe = qx{$cmd 2>/dev/null}; my $raw = $?; skip_all "ps not executable: $!" if $raw == -1; skip_all "ps exited non-zero (raw=$raw)" if $raw != 0; skip_all "ps output not parseable" unless $probe =~ /^\s*\d+\s+\d+\s*$/m; my %mem = Test2::Plugin::MemUsage::_collect_ps(); ok(%mem, "got mem hash"); for my $k (qw/rss size/) { my ($v, $u) = @{$mem{$k}}; like($v, qr/^\d+$/, "$k numeric"); ok($v + 0 > 0, "$k > 0"); is($u, 'kB', "$k units kB"); } is($mem{peak}, ['NA', ''], "peak NA (ps does not surface peak RSS)"); }; done_testing; Test2-Plugin-MemUsage-0.002006000755001750001750 015174311231 16216 5ustar00exodistexodist000000000000README100644001750001750 337015174311231 17162 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006NAME Test2::Plugin::MemUsage - Collect and display memory usage information. PLATFORM SUPPORT The plugin selects a memory collector based on $^O: Linux, Cygwin, GNU/kFreeBSD Reads /proc/PID/status. Reports rss, size (VmSize), and peak (VmPeak). macOS (darwin), *BSD, Solaris, AIX, HP-UX Shells out to ps -o rss=,vsz= -p $$. Reports rss and size; peak is NA unless BSD::Resource is installed (see below). MSWin32 Uses Win32::Process::Memory if installed to call GetProcessMemoryInfo. Reports rss (WorkingSetSize), peak (PeakWorkingSetSize), and size (PagefileUsage). Other / fallback If BSD::Resource is installed, getrusage is used to fill in a peak RSS value when the primary collector did not provide one (or when no native collector matched the platform at all). If no collector and no fallback applies, the plugin is a silent no-op. DESCRIPTION This plugin will collect memory usage info from /proc/PID/status and display it for you when the test is done running. SYNOPSIS use Test2::Plugin::MemUsage; This is also useful at the command line for 1-time use: $ perl -MTest2::Plugin::MemUsage path/to/test.t Output: # rss: 36708kB # size: 49836kB # peak: 49836kB SOURCE The source code repository for Test2-Plugin-MemUsage can be found at https://github.com/Test-More/Test2-Plugin-MemUsage/. MAINTAINERS Chad Granum AUTHORS Chad Granum COPYRIGHT Copyright 2019 Chad Granum . This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/ LICENSE100644001750001750 4627615174311231 17343 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006This software is copyright (c) 2026 by Chad Granum. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2026 by Chad Granum. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Moe Ghoul, President of Vice That's all there is to it! --- The Perl Artistic License 1.0 --- This software is Copyright (c) 2026 by Chad Granum. This is free software, licensed under: The Perl Artistic License 1.0 The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder as specified below. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as uunet.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) give non-standard executables non-standard names, and clearly document the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whoever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Changes100644001750001750 1346215174311231 17620 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.0020060.002006 2026-04-28 22:09:45-07:00 America/Los_Angeles - Add [PruneCruft] to dist.ini so blib/ and pm_to_blib are excluded from the released tarball (GH #1) - Drop the "< 100 GB sanity" upper bound on rss/size in t/ps.t real collect subtest; macOS reports virtual size well above 100 GB, causing spurious failures (GH #3) 0.002005 2026-04-26 01:49:37-07:00 America/Los_Angeles - Replace defined-or (//) operator with explicit defined() checks so the module loads on the declared MIN_PERL_VERSION 5.008009 - Use 'ps -o rss= -o vsz= -p $$' instead of 'ps -o rss=,vsz= -p $$' so FreeBSD's ps parses the format string correctly (it treated the comma as part of the rss= header) - Force the linux _collect_proc collector inside t/memusage.t so the mocked t/procfile drives the assertions on every host (the previous mock was ignored on *BSD/Solaris/MSWin32, which used their native collectors and produced unrelated values) - Skip the real-* subtests in t/ps.t when ps exits non-zero or returns output our parser cannot match (covers BusyBox/Alpine and FreeBSD ps differences) - Skip the real-* subtests in t/proc.t on non-Linux hosts; Solaris /proc/PID/status is a binary pstatus_t struct, not the Linux text format - Guard t/win32.t real-* subtests with defined() checks on GetProcessMemoryInfo and short-circuit after a failed lives check so a single broken Win32::Process::Memory install does not cascade into multiple failures 0.002004 2026-04-25 07:18:08-07:00 America/Los_Angeles - Fix broken grep filter in send_mem_event that referenced @_ of the outer sub instead of the grep iterator - Match \s+ instead of a literal space between value and unit in the Vm* regexes - Warn instead of die when opening or closing the procfile fails, so the exit callback does not abort the test process - Use -e instead of -f when checking for the procfile, so non-Linux procfs implementations are not rejected - Add raw numeric value and units to harness_job_fields entries under a 'data' hash so consumers can sort and aggregate without reparsing the display string - Refactor memory collection into a $^O-keyed dispatcher with a reusable _collect_proc backend (no behavior change on Linux) - Add darwin and *BSD support via 'ps -o rss=,vsz= -p $$' (peak remains NA there unless BSD::Resource is available) - Add MSWin32 support via Win32::Process::Memory (recommended, not required) and recommend it in dist.ini - Add BSD::Resource getrusage(RUSAGE_SELF) fallback that fills peak RSS when the primary collector did not provide one and acts as a last-resort collector on platforms with no native backend; recommend BSD::Resource in dist.ini. Also document the platform support matrix in POD - Add t/dispatch.t covering _collector_for_os table, _augment_peak fill / preserve / no-fallback, collect_mem with no collector and no fallback, and send_mem_event short-circuits for empty and all-NA cases - Add t/proc.t covering _collect_proc happy path, missing procfile, empty procfile, files without Vm* lines, tab-separated values, and partial Vm* presence - Add t/ps.t covering _collect_ps via stubbed ps_command (happy / empty / unparseable) plus an opportunistic real-ps subtest; skips on MSWin32 and any host without a usable shell echo - Add t/win32.t covering _collect_win32 via a fake Win32::Process::Memory injected into %INC, exercising byte-to-kB conversion, undef return, exception in the underlying call, and partial-info handling - Add t/getrusage.t covering _maxrss_kb (linux kB pass-through, darwin byte conversion, zero / empty getrusage), the collect_mem last-resort path when no native collector matched, and the augment_peak chain end to end against a fake BSD::Resource - Extend t/memusage.t to assert that calling import() a second time does not register a duplicate exit callback - Add Solaris, AIX, and HP-UX support by routing them through the existing _collect_ps backend (their POSIX ps accepts 'ps -o rss=,vsz= -p $$' and reports kilobytes); peak still benefits from the BSD::Resource fallback when installed - Add real-system subtests to t/proc.t that verify the host's /proc/PID/status matches the shape our mocks assume and that _collect_proc returns numeric, positive, sanely-ranged values; both subtests skip when /proc is unavailable - Add real-system subtests to t/ps.t that verify the host's 'ps -o rss=,vsz= -p $$' exits 0 with parseable output, and that _collect_ps returns numeric/positive/in-range values with kB units; both subtests skip when ps is missing (exit 127 / fork failure) and fail when ps is present but misbehaves - Add real-system subtests to t/win32.t that verify the real Win32::Process::Memory returns the hash shape we assume and that _collect_win32 returns meaningful values; the fake module injection is now conditional so the real module is preferred when installed, and both new subtests skip off-MSWin32 or when Win32::Process::Memory is not installed - Add real-system subtests to t/getrusage.t that verify the real BSD::Resource::getrusage returns at least 3 fields with a numeric ru_maxrss and that _maxrss_kb returns a meaningful value; the fake module injection is now conditional so the real module is preferred when installed 0.002003 2020-02-26 08:09:20-08:00 America/Los_Angeles - Avoid warnings when info is missing 0.002002 2019-08-19 10:57:16-07:00 America/Los_Angeles - Add harness_job_fields 0.002001 2019-08-16 15:28:36-07:00 America/Los_Angeles - Initial Import MANIFEST100644001750001750 45415174311231 17413 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.037 Changes LICENSE MANIFEST META.json META.yml Makefile.PL README README.md cpanfile dist.ini lib/Test2/Plugin/MemUsage.pm t/dispatch.t t/getrusage.t t/memusage.t t/proc.t t/procfile t/ps.t t/win32.t xt/author/pod-syntax.t dist.ini100644001750001750 407115174311231 17745 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006name = Test2-Plugin-MemUsage author = Chad Granum license = Perl_5 copyright_holder = Chad Granum [RewriteVersion] ; sets dist version from main module's $VERSION [License] [ManifestSkip] [Manifest] [NextRelease] [GatherDir] exclude_match = ^cover exclude_match = ^nyt exclude_match = ^test-logs exclude_filename = LICENSE exclude_filename = Makefile.PL exclude_filename = cpanfile exclude_filename = README exclude_filename = README.md exclude_filename = .yath-persist.json exclude_filename = CLAUDE.md [PruneCruft] ; drop blib/, pm_to_blib, MYMETA.*, etc. (gh #1) [PodSyntaxTests] [TestRelease] [MetaResources] bugtracker.web = http://github.com/Test-More/Test2-Plugin-MemUsage/issues repository.url = http://github.com/Test-More/Test2-Plugin-MemUsage/ repository.type = git [Prereqs] perl = 5.008009 Test2::API = 1.302165 Test2::Event::V2 = 1.302165 [Prereqs / TestRequires] Test2::V0 = 0.000124 [Prereqs / RuntimeRecommends] Win32::Process::Memory = 0.04 BSD::Resource = 1.2911 [Prereqs / DevelopRequires] Test::Spelling = 0.12 ; for xt/author/pod-spell.t [MakeMaker] [CPANFile] [MetaYAML] [MetaJSON] ; authordep Pod::Markdown [ReadmeFromPod / Markdown] filename = lib/Test2/Plugin/MemUsage.pm type = markdown readme = README.md [ReadmeFromPod / Text] filename = lib/Test2/Plugin/MemUsage.pm type = text readme = README [CopyFilesFromBuild] copy = LICENSE copy = cpanfile copy = README copy = README.md copy = Makefile.PL [Git::Check] allow_dirty = Makefile.PL allow_dirty = README allow_dirty = README.md allow_dirty = cpanfile allow_dirty = LICENSE allow_dirty = Changes [Git::Commit] allow_dirty = Makefile.PL allow_dirty = README allow_dirty = README.md allow_dirty = cpanfile allow_dirty = LICENSE allow_dirty = Changes [Git::Tag] [FakeRelease] [BumpVersionAfterRelease] [Git::Commit / Commit_Changes] munge_makefile_pl = true allow_dirty_match = ^lib allow_dirty_match = ^scripts allow_dirty = Makefile.PL allow_dirty = README allow_dirty = README.md allow_dirty = cpanfile allow_dirty = LICENSE commit_msg = Automated Version Bump proc.t100644001750001750 1046315174311231 17716 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::V0; use File::Temp qw/tempfile/; use Test2::Plugin::MemUsage; subtest happy_path => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { 't/procfile' }; my %mem = Test2::Plugin::MemUsage::_collect_proc(); is($mem{peak}, ['25176', 'kB'], "peak"); is($mem{size}, ['25176', 'kB'], "size"); is($mem{rss}, ['16604', 'kB'], "rss"); }; subtest missing_procfile => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { '/this/path/should/not/exist/please' }; my @out = Test2::Plugin::MemUsage::_collect_proc(); is(\@out, [], "missing procfile -> empty"); }; subtest empty_procfile => sub { my ($fh, $path) = tempfile(UNLINK => 1); close $fh; no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { $path }; my @out = Test2::Plugin::MemUsage::_collect_proc(); is(\@out, [], "empty file -> empty"); }; subtest no_vm_lines => sub { my ($fh, $path) = tempfile(UNLINK => 1); print $fh "Pid: 123\nName: foo\nState: R (running)\n"; close $fh; no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { $path }; my %mem = Test2::Plugin::MemUsage::_collect_proc(); is($mem{peak}, ['NA', ''], "peak NA"); is($mem{size}, ['NA', ''], "size NA"); is($mem{rss}, ['NA', ''], "rss NA"); }; subtest tab_separator => sub { my ($fh, $path) = tempfile(UNLINK => 1); print $fh "VmPeak:\t 111 kB\nVmSize:\t 222 kB\nVmRSS:\t 333 kB\n"; close $fh; no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { $path }; my %mem = Test2::Plugin::MemUsage::_collect_proc(); is($mem{peak}, ['111', 'kB'], "peak parsed with tab separator"); is($mem{size}, ['222', 'kB'], "size parsed with tab separator"); is($mem{rss}, ['333', 'kB'], "rss parsed with tab separator"); }; subtest partial_vm_lines => sub { my ($fh, $path) = tempfile(UNLINK => 1); print $fh "VmRSS: 1234 kB\n"; # only rss close $fh; no warnings 'redefine'; local *Test2::Plugin::MemUsage::proc_file = sub { $path }; my %mem = Test2::Plugin::MemUsage::_collect_proc(); is($mem{rss}, ['1234', 'kB'], "rss parsed"); is($mem{peak}, ['NA', ''], "peak NA when missing"); is($mem{size}, ['NA', ''], "size NA when missing"); }; # Real-system compatibility: verify the host's /proc/PID/status actually # matches the shape our mocks assume. Skip cleanly if there is no # procfile; fail loudly if the procfile exists but the format has # drifted away from what our regex expects. subtest real_procfile_matches_mock_shape => sub { # /proc/PID/status is Linux-format text only on Linux/Cygwin/gnukfreebsd. # On Solaris it is a binary pstatus_t struct; on *BSD /proc may not be # mounted at all. Mirror the dispatcher in _collector_for_os. skip_all "procfile parser is linux-only (\$^O = $^O)" unless $^O eq 'linux' || $^O eq 'cygwin' || $^O eq 'gnukfreebsd'; my $f = Test2::Plugin::MemUsage::proc_file(); skip_all "no procfile at $f" unless -e $f; open my $fh, '<', $f or skip_all "cannot open $f: $!"; local $/; my $stats = <$fh>; close $fh; ok(defined $stats && length $stats, "procfile is non-empty"); like($stats, qr/VmPeak:\s+\d+\s+\S+/, "VmPeak matches our regex"); like($stats, qr/VmSize:\s+\d+\s+\S+/, "VmSize matches our regex"); like($stats, qr/VmRSS:\s+\d+\s+\S+/, "VmRSS matches our regex"); }; # Real-system integration: actually run the collector against the # host kernel. Skip if /proc is absent; fail if the values come back # non-numeric, missing units, or wildly out of range. subtest real_collect_proc_meaningful => sub { skip_all "procfile parser is linux-only (\$^O = $^O)" unless $^O eq 'linux' || $^O eq 'cygwin' || $^O eq 'gnukfreebsd'; my $f = Test2::Plugin::MemUsage::proc_file(); skip_all "no procfile at $f" unless -e $f; my %mem = Test2::Plugin::MemUsage::_collect_proc(); ok(%mem, "got mem hash"); for my $k (qw/rss size peak/) { my ($v, $u) = @{$mem{$k}}; like($v, qr/^\d+$/, "$k value is numeric"); ok($v + 0 > 0, "$k value is positive"); ok($v + 0 < 100_000_000, "$k value < 100 GB sanity"); ok(length $u, "$k has a unit"); } }; done_testing; cpanfile100644001750001750 107415174311231 20005 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006# This file is generated by Dist::Zilla::Plugin::CPANFile v6.037 # Do not edit this file directly. To change prereqs, edit the `dist.ini` file. requires "Test2::API" => "1.302165"; requires "Test2::Event::V2" => "1.302165"; requires "perl" => "5.008009"; recommends "BSD::Resource" => "1.2911"; recommends "Win32::Process::Memory" => "0.04"; on 'test' => sub { requires "Test2::V0" => "0.000124"; }; on 'configure' => sub { requires "ExtUtils::MakeMaker" => "0"; }; on 'develop' => sub { requires "Test::Pod" => "1.41"; requires "Test::Spelling" => "0.12"; }; META.yml100644001750001750 157115174311231 17554 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006--- abstract: 'Collect and display memory usage information.' author: - 'Chad Granum ' build_requires: Test2::V0: '0.000124' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.037, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test2-Plugin-MemUsage recommends: BSD::Resource: '1.2911' Win32::Process::Memory: '0.04' requires: Test2::API: '1.302165' Test2::Event::V2: '1.302165' perl: '5.008009' resources: bugtracker: http://github.com/Test-More/Test2-Plugin-MemUsage/issues repository: http://github.com/Test-More/Test2-Plugin-MemUsage/ version: '0.002006' x_generated_by_perl: v5.42.2 x_serialization_backend: 'YAML::Tiny version 1.76' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' win32.t100644001750001750 1037715174311231 17721 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::V0; # Detect at compile time whether the real Win32::Process::Memory is # installed. If it is, we use it for the real-system subtests; if it # is not, we install a lightweight fake into %INC so the adapter # tests below can still exercise our byte-to-kB conversion logic # without requiring a Windows host or the real CPAN module. our $HAS_REAL_WPM; BEGIN { $HAS_REAL_WPM = eval { require Win32::Process::Memory; 1 } ? 1 : 0; $INC{'Win32/Process/Memory.pm'} ||= __FILE__; } # Default fake; only installed when the real module is absent. unless ($HAS_REAL_WPM) { no warnings 'redefine', 'once'; *Win32::Process::Memory::GetProcessMemoryInfo = sub { return { WorkingSetSize => 1048576, PeakWorkingSetSize => 2097152, PagefileUsage => 3145728, }; }; } use Test2::Plugin::MemUsage; # --- Real-system subtests ------------------------------------------------- # Compatibility: verify the real Win32::Process::Memory returns the hash # shape our mocks assume. Skip when we are not on Windows or the module # is not installed; fail when both prerequisites are met but the data # does not match expectations. subtest real_module_matches_mock_shape => sub { skip_all "Win32::Process::Memory not installed" unless $HAS_REAL_WPM; skip_all "not on MSWin32 (\$^O = $^O)" unless $^O eq 'MSWin32'; skip_all "Win32::Process::Memory loaded but GetProcessMemoryInfo not defined" unless defined &Win32::Process::Memory::GetProcessMemoryInfo; my $info; ok(lives { $info = Win32::Process::Memory::GetProcessMemoryInfo($$) }, "GetProcessMemoryInfo did not throw") or do { diag $@; return }; is(ref($info), 'HASH', "returns a hashref") or return; for my $k (qw/WorkingSetSize PeakWorkingSetSize PagefileUsage/) { ok(exists $info->{$k}, "$k key present") or next; like($info->{$k}, qr/^\d+$/, "$k value is numeric"); } }; # Integration: drive _collect_win32 end to end against the real module. subtest real_collect_win32_meaningful => sub { skip_all "Win32::Process::Memory not installed" unless $HAS_REAL_WPM; skip_all "not on MSWin32 (\$^O = $^O)" unless $^O eq 'MSWin32'; skip_all "Win32::Process::Memory loaded but GetProcessMemoryInfo not defined" unless defined &Win32::Process::Memory::GetProcessMemoryInfo; my %mem = Test2::Plugin::MemUsage::_collect_win32(); ok(%mem, "got mem hash") or return; for my $k (qw/rss peak size/) { my ($v, $u) = @{$mem{$k}}; like($v, qr/^\d+$/, "$k numeric"); ok($v + 0 > 0, "$k > 0"); ok($v + 0 < 100_000_000, "$k < 100 GB sanity"); is($u, 'kB', "$k units kB"); } }; # --- Mocked adapter subtests ---------------------------------------------- subtest happy_path => sub { no warnings 'redefine'; local *Win32::Process::Memory::GetProcessMemoryInfo = sub { return { WorkingSetSize => 1048576, PeakWorkingSetSize => 2097152, PagefileUsage => 3145728, }; }; my %mem = Test2::Plugin::MemUsage::_collect_win32(); is($mem{rss}, [1024, 'kB'], "rss converted from bytes"); is($mem{peak}, [2048, 'kB'], "peak converted from bytes"); is($mem{size}, [3072, 'kB'], "size converted from bytes"); }; subtest no_info_returned => sub { no warnings 'redefine'; local *Win32::Process::Memory::GetProcessMemoryInfo = sub { undef }; my @out = Test2::Plugin::MemUsage::_collect_win32(); is(\@out, [], "GetProcessMemoryInfo undef -> empty"); }; subtest die_in_call => sub { no warnings 'redefine'; local *Win32::Process::Memory::GetProcessMemoryInfo = sub { die "kaboom" }; my @out = Test2::Plugin::MemUsage::_collect_win32(); is(\@out, [], "exception inside call -> empty"); }; subtest partial_info => sub { no warnings 'redefine'; local *Win32::Process::Memory::GetProcessMemoryInfo = sub { return {WorkingSetSize => 4096}; # only rss }; my %mem = Test2::Plugin::MemUsage::_collect_win32(); is($mem{rss}, [4, 'kB'], "rss converted"); is($mem{peak}, ['NA', ''], "peak NA when missing"); is($mem{size}, ['NA', ''], "size NA when missing"); }; done_testing; META.json100644001750001750 305515174311231 17723 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006{ "abstract" : "Collect and display memory usage information.", "author" : [ "Chad Granum " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.037, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Test2-Plugin-MemUsage", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "develop" : { "requires" : { "Test::Pod" : "1.41", "Test::Spelling" : "0.12" } }, "runtime" : { "recommends" : { "BSD::Resource" : "1.2911", "Win32::Process::Memory" : "0.04" }, "requires" : { "Test2::API" : "1.302165", "Test2::Event::V2" : "1.302165", "perl" : "5.008009" } }, "test" : { "requires" : { "Test2::V0" : "0.000124" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "http://github.com/Test-More/Test2-Plugin-MemUsage/issues" }, "repository" : { "type" : "git", "url" : "http://github.com/Test-More/Test2-Plugin-MemUsage/" } }, "version" : "0.002006", "x_generated_by_perl" : "v5.42.2", "x_serialization_backend" : "Cpanel::JSON::XS version 4.40", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later" } README.md100644001750001750 350315174311231 17557 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006# NAME Test2::Plugin::MemUsage - Collect and display memory usage information. # PLATFORM SUPPORT The plugin selects a memory collector based on `$^O`: - Linux, Cygwin, GNU/kFreeBSD Reads `/proc/PID/status`. Reports rss, size (VmSize), and peak (VmPeak). - macOS (darwin), \*BSD, Solaris, AIX, HP-UX Shells out to `ps -o rss=,vsz= -p $$`. Reports rss and size; peak is NA unless [BSD::Resource](https://metacpan.org/pod/BSD%3A%3AResource) is installed (see below). - MSWin32 Uses [Win32::Process::Memory](https://metacpan.org/pod/Win32%3A%3AProcess%3A%3AMemory) if installed to call `GetProcessMemoryInfo`. Reports rss (WorkingSetSize), peak (PeakWorkingSetSize), and size (PagefileUsage). - Other / fallback If [BSD::Resource](https://metacpan.org/pod/BSD%3A%3AResource) is installed, `getrusage` is used to fill in a peak RSS value when the primary collector did not provide one (or when no native collector matched the platform at all). If no collector and no fallback applies, the plugin is a silent no-op. # DESCRIPTION This plugin will collect memory usage info from `/proc/PID/status` and display it for you when the test is done running. # SYNOPSIS use Test2::Plugin::MemUsage; This is also useful at the command line for 1-time use: $ perl -MTest2::Plugin::MemUsage path/to/test.t Output: # rss: 36708kB # size: 49836kB # peak: 49836kB # SOURCE The source code repository for Test2-Plugin-MemUsage can be found at `https://github.com/Test-More/Test2-Plugin-MemUsage/`. # MAINTAINERS - Chad Granum # AUTHORS - Chad Granum # COPYRIGHT Copyright 2019 Chad Granum . This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See `http://dev.perl.org/licenses/` procfile100644001750001750 23615174311231 20251 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tThis is a bunch of noise! gsgsdfgdsfgsdfgsdgdsf VmPeak: 25176 kB VmSize: 25176 kB VmRSS: 16604 kB This is a bunch of noise! gsgsdfgdsfgsdfgsdgdsf Makefile.PL100644001750001750 223415174311231 20252 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.037 use strict; use warnings; use 5.008009; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Collect and display memory usage information.", "AUTHOR" => "Chad Granum ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Test2-Plugin-MemUsage", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.008009", "NAME" => "Test2::Plugin::MemUsage", "PREREQ_PM" => { "Test2::API" => "1.302165", "Test2::Event::V2" => "1.302165" }, "TEST_REQUIRES" => { "Test2::V0" => "0.000124" }, "VERSION" => "0.002006", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Test2::API" => "1.302165", "Test2::Event::V2" => "1.302165", "Test2::V0" => "0.000124" ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); dispatch.t100644001750001750 750715174311231 20537 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::V0; use Test2::API qw/intercept context/; use Test2::Plugin::MemUsage; subtest collector_for_os => sub { my $proc = \&Test2::Plugin::MemUsage::_collect_proc; my $ps = \&Test2::Plugin::MemUsage::_collect_ps; my $win = \&Test2::Plugin::MemUsage::_collect_win32; is(Test2::Plugin::MemUsage::_collector_for_os('linux'), $proc, "linux -> proc"); is(Test2::Plugin::MemUsage::_collector_for_os('cygwin'), $proc, "cygwin -> proc"); is(Test2::Plugin::MemUsage::_collector_for_os('gnukfreebsd'), $proc, "gnukfreebsd -> proc"); is(Test2::Plugin::MemUsage::_collector_for_os('darwin'), $ps, "darwin -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('freebsd'), $ps, "freebsd -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('openbsd'), $ps, "openbsd -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('netbsd'), $ps, "netbsd -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('solaris'), $ps, "solaris -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('aix'), $ps, "aix -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('hpux'), $ps, "hpux -> ps"); is(Test2::Plugin::MemUsage::_collector_for_os('MSWin32'), $win, "MSWin32 -> win32"); is(Test2::Plugin::MemUsage::_collector_for_os('haiku'), undef, "unknown OS -> undef"); is(Test2::Plugin::MemUsage::_collector_for_os('riscos'), undef, "another unknown OS -> undef"); }; subtest augment_peak_fills_NA => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::_maxrss_kb = sub { 9999 }; my %in = ( rss => ['100', 'kB'], size => ['200', 'kB'], peak => ['NA', ''], ); my %out = Test2::Plugin::MemUsage::_augment_peak(%in); is($out{peak}, [9999, 'kB'], "peak filled from getrusage"); is($out{rss}, ['100', 'kB'], "rss preserved"); is($out{size}, ['200', 'kB'], "size preserved"); }; subtest augment_peak_keeps_existing => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::_maxrss_kb = sub { 9999 }; my %in = ( rss => ['100', 'kB'], size => ['200', 'kB'], peak => ['500', 'kB'], ); my %out = Test2::Plugin::MemUsage::_augment_peak(%in); is($out{peak}, ['500', 'kB'], "existing peak preserved when not NA"); }; subtest augment_peak_no_fallback => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::_maxrss_kb = sub { undef }; my %in = ( rss => ['100', 'kB'], size => ['200', 'kB'], peak => ['NA', ''], ); my %out = Test2::Plugin::MemUsage::_augment_peak(%in); is($out{peak}, ['NA', ''], "peak stays NA when fallback returns undef"); }; subtest collect_mem_no_collector_no_fallback => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::_collector_for_os = sub { undef }; local *Test2::Plugin::MemUsage::_maxrss_kb = sub { undef }; my @out = Test2::Plugin::MemUsage::collect_mem(); is(\@out, [], "no collector + no fallback -> empty"); }; subtest send_mem_event_skips_when_empty => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::collect_mem = sub { () }; my $events = intercept { my $ctx = context(); Test2::Plugin::MemUsage::send_mem_event($ctx); $ctx->release; }; is(scalar(@$events), 0, "no event when collect_mem returns empty"); }; subtest send_mem_event_skips_when_all_NA => sub { no warnings 'redefine'; local *Test2::Plugin::MemUsage::collect_mem = sub { (peak => ['NA', ''], size => ['NA', ''], rss => ['NA', '']) }; my $events = intercept { my $ctx = context(); Test2::Plugin::MemUsage::send_mem_event($ctx); $ctx->release; }; is(scalar(@$events), 0, "no event when all values are NA"); }; done_testing; memusage.t100644001750001750 425115174311231 20534 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::API qw/intercept context/; use Test2::Tools::Defer qw/def do_def/; use vars qw/@CALLBACKS/; BEGIN { no warnings 'redefine'; local *Test2::API::test2_add_callback_exit = sub { push @CALLBACKS => @_ }; require Test2::Plugin::MemUsage; def ok => (!scalar(@CALLBACKS), "requiring the module does not add a callback"); Test2::Plugin::MemUsage->import(); def ok => (scalar(@CALLBACKS), "importing the module does add a callback"); Test2::Plugin::MemUsage->import(); def ok => (scalar(@CALLBACKS) == 1, "second import is a no-op (callback registered once)"); } use Test2::Tools::Basic; use Test2::Tools::Compare qw/like is hash field etc/; do_def; is(Test2::Plugin::MemUsage->proc_file(), "/proc/$$/status", "Correct procfile"); my $events = intercept { sub { no warnings 'redefine'; # Force the linux procfile collector regardless of host OS so the # mocked t/procfile drives the assertions below. local *Test2::Plugin::MemUsage::_collector_for_os = sub { \&Test2::Plugin::MemUsage::_collect_proc }; local *Test2::Plugin::MemUsage::_maxrss_kb = sub { undef }; local *Test2::Plugin::MemUsage::proc_file = sub { 't/procfile' }; my $ctx = context(); $CALLBACKS[0]->($ctx); $ctx->release; }->(); }; chomp(my $summary = <[0], hash { field info => [{details => $summary, tag => 'MEMORY'}]; field about => {details => $summary, package => 'Test2::Plugin::MemUsage'}; field memory => { details => $summary, size => ['25176', 'kB'], peak => ['25176', 'kB'], rss => ['16604', 'kB'], }; field harness_job_fields => [ {name => 'mem_rss', details => '16604kB', data => {value => 16604, units => 'kB'}}, {name => 'mem_size', details => '25176kB', data => {value => 25176, units => 'kB'}}, {name => 'mem_peak', details => '25176kB', data => {value => 25176, units => 'kB'}}, ]; etc; }, "Got desired event" ); done_testing(); getrusage.t100644001750001750 740115174311231 20717 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/tuse strict; use warnings; use Test2::V0; # Detect at compile time whether the real BSD::Resource is installed. # When it is, we use it for the real-system subtests; when it is not, # we install a fake into %INC so the adapter subtests below can still # exercise our unit-conversion logic without requiring the real module. our $HAS_REAL_BR; BEGIN { $HAS_REAL_BR = eval { require BSD::Resource; 1 } ? 1 : 0; $INC{'BSD/Resource.pm'} ||= __FILE__; } unless ($HAS_REAL_BR) { no warnings 'redefine', 'once'; *BSD::Resource::RUSAGE_SELF = sub () { 0 }; *BSD::Resource::getrusage = sub { (0, 0, 8192) }; } use Test2::Plugin::MemUsage; # --- Real-system subtests ------------------------------------------------- # Compatibility: call real BSD::Resource::getrusage and verify the # field at index 2 (ru_maxrss) is numeric. Skip when the module is not # installed; fail when it is installed but its return shape has # drifted from what _maxrss_kb assumes. subtest real_getrusage_matches_mock_shape => sub { skip_all "BSD::Resource not installed" unless $HAS_REAL_BR; my @ru; ok(lives { @ru = BSD::Resource::getrusage(BSD::Resource::RUSAGE_SELF()) }, "getrusage did not throw") or diag $@; ok(scalar(@ru) >= 3, "getrusage returns at least 3 fields"); like($ru[2], qr/^-?\d+(?:\.\d+)?$/, "ru_maxrss is numeric"); ok($ru[2] >= 0, "ru_maxrss is non-negative"); }; # Integration: drive _maxrss_kb against real BSD::Resource. Skip when # the module is missing; fail when present but value is wrong. subtest real_maxrss_kb_meaningful => sub { skip_all "BSD::Resource not installed" unless $HAS_REAL_BR; my $kb = Test2::Plugin::MemUsage::_maxrss_kb(); ok(defined $kb, "got a value"); like($kb, qr/^\d+$/, "value is numeric"); ok($kb > 0, "value > 0"); ok($kb < 100_000_000, "value < 100 GB sanity"); }; # --- Mocked adapter subtests ---------------------------------------------- subtest linux_uses_kb_directly => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { (0, 0, 8192) }; local $^O = 'linux'; is(Test2::Plugin::MemUsage::_maxrss_kb(), 8192, "ru_maxrss returned as kB on Linux"); }; subtest darwin_converts_bytes => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { (0, 0, 8192) }; local $^O = 'darwin'; is(Test2::Plugin::MemUsage::_maxrss_kb(), 8, "ru_maxrss / 1024 on darwin"); }; subtest zero_maxrss_returns_undef => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { (0, 0, 0) }; is(Test2::Plugin::MemUsage::_maxrss_kb(), undef, "0 maxrss -> undef"); }; subtest empty_getrusage_returns_undef => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { () }; is(Test2::Plugin::MemUsage::_maxrss_kb(), undef, "empty list from getrusage -> undef"); }; subtest collect_mem_last_resort => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { (0, 0, 8192) }; local *Test2::Plugin::MemUsage::_collector_for_os = sub { undef }; local $^O = 'linux'; my %mem = Test2::Plugin::MemUsage::collect_mem(); is($mem{peak}, [8192, 'kB'], "fallback fills peak"); is($mem{rss}, ['NA', ''], "rss NA"); is($mem{size}, ['NA', ''], "size NA"); }; subtest augment_peak_uses_real_helper => sub { no warnings 'redefine'; local *BSD::Resource::getrusage = sub { (0, 0, 8192) }; local $^O = 'linux'; my %in = ( rss => ['100', 'kB'], size => ['200', 'kB'], peak => ['NA', ''], ); my %out = Test2::Plugin::MemUsage::_augment_peak(%in); is($out{peak}, [8192, 'kB'], "augment_peak filled peak from getrusage chain"); }; done_testing; author000755001750001750 015174311231 20074 5ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/xtpod-syntax.t100644001750001750 25115174311231 22505 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/xt/author#!perl # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); Plugin000755001750001750 015174311231 21144 5ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/lib/Test2MemUsage.pm100644001750001750 1316115174311231 23367 0ustar00exodistexodist000000000000Test2-Plugin-MemUsage-0.002006/lib/Test2/Pluginpackage Test2::Plugin::MemUsage; use strict; use warnings; our $VERSION = '0.002006'; use Test2::API qw/test2_add_callback_exit/; my $ADDED_HOOK = 0; sub import { return if $ADDED_HOOK++; test2_add_callback_exit(\&send_mem_event); } sub proc_file { "/proc/$$/status" } sub _empty_mem { (peak => ['NA', ''], size => ['NA', ''], rss => ['NA', '']) } sub _collect_proc { my $file = proc_file(); return unless -e $file; my $stats; { open(my $fh, '<', $file) or warn("Could not open file '$file' (<): $!"), return; local $/; $stats = <$fh>; close($fh) or warn "Could not close file '$file': $!"; } return unless $stats; my %mem = _empty_mem(); $mem{peak} = [$1, $2] if $stats =~ m/VmPeak:\s+(\d+)\s+(\S+)/; $mem{size} = [$1, $2] if $stats =~ m/VmSize:\s+(\d+)\s+(\S+)/; $mem{rss} = [$1, $2] if $stats =~ m/VmRSS:\s+(\d+)\s+(\S+)/; return %mem; } sub ps_command { "ps -o rss= -o vsz= -p $$" } sub _collect_ps { my $cmd = ps_command(); my $out = `$cmd 2>/dev/null`; return unless defined $out && length $out; my ($rss, $vsz) = $out =~ /^\s*(\d+)\s+(\d+)\s*$/m or return; my %mem = _empty_mem(); $mem{rss} = [$rss, 'kB']; $mem{size} = [$vsz, 'kB']; return %mem; } sub _collect_win32 { return unless eval { require Win32::Process::Memory; 1 }; my $info = eval { Win32::Process::Memory::GetProcessMemoryInfo($$) } or return; my $rss = $info->{WorkingSetSize} || 0; my $peak = $info->{PeakWorkingSetSize} || 0; my $size = $info->{PagefileUsage} || 0; my %mem = _empty_mem(); $mem{rss} = [int($rss / 1024), 'kB'] if $rss; $mem{peak} = [int($peak / 1024), 'kB'] if $peak; $mem{size} = [int($size / 1024), 'kB'] if $size; return %mem; } sub _collector_for_os { my $os = shift; $os = $^O unless defined $os; return \&_collect_proc if $os eq 'linux' || $os eq 'cygwin' || $os eq 'gnukfreebsd'; return \&_collect_ps if $os eq 'darwin' || $os =~ /bsd$/ || $os eq 'solaris' || $os eq 'aix' || $os eq 'hpux'; return \&_collect_win32 if $os eq 'MSWin32'; return undef; } sub _maxrss_kb { return unless eval { require BSD::Resource; 1 }; my @ru = BSD::Resource::getrusage(BSD::Resource::RUSAGE_SELF()) or return; my $maxrss = $ru[2]; return unless defined $maxrss && $maxrss > 0; return $^O eq 'darwin' ? int($maxrss / 1024) : $maxrss; } sub _augment_peak { my %mem = @_; return %mem if !exists $mem{peak} || $mem{peak}->[0] ne 'NA'; my $kb = _maxrss_kb(); return %mem unless defined $kb; $mem{peak} = [$kb, 'kB']; return %mem; } sub collect_mem { my $c = _collector_for_os(); my %mem = $c ? $c->() : (); unless (%mem) { my $kb = _maxrss_kb(); return () unless defined $kb; %mem = _empty_mem(); $mem{peak} = [$kb, 'kB']; return %mem; } return _augment_peak(%mem); } sub send_mem_event { my ($ctx, $real, $new) = @_; my %mem = collect_mem(); return unless %mem; return unless grep { $_->[0] ne 'NA' } values %mem; $mem{details} = "rss: $mem{rss}->[0]$mem{rss}->[1]\nsize: $mem{size}->[0]$mem{size}->[1]\npeak: $mem{peak}->[0]$mem{peak}->[1]"; $ctx->send_ev2( memory => \%mem, about => {package => __PACKAGE__, details => $mem{details}}, info => [{tag => 'MEMORY', details => $mem{details}}], harness_job_fields => [ map { my $k = $_; my ($v, $u) = @{$mem{$k}}; +{ name => "mem_$k", details => "$v$u", data => {value => ($v eq 'NA' ? undef : $v + 0), units => $u}, }; } qw/rss size peak/, ], ); } 1; __END__ =pod =encoding UTF-8 =head1 NAME Test2::Plugin::MemUsage - Collect and display memory usage information. =head1 PLATFORM SUPPORT The plugin selects a memory collector based on C<$^O>: =over 4 =item Linux, Cygwin, GNU/kFreeBSD Reads C. Reports rss, size (VmSize), and peak (VmPeak). =item macOS (darwin), *BSD, Solaris, AIX, HP-UX Shells out to C. Reports rss and size; peak is NA unless L is installed (see below). =item MSWin32 Uses L if installed to call C. Reports rss (WorkingSetSize), peak (PeakWorkingSetSize), and size (PagefileUsage). =item Other / fallback If L is installed, C is used to fill in a peak RSS value when the primary collector did not provide one (or when no native collector matched the platform at all). =back If no collector and no fallback applies, the plugin is a silent no-op. =head1 DESCRIPTION This plugin will collect memory usage info from C and display it for you when the test is done running. =head1 SYNOPSIS use Test2::Plugin::MemUsage; This is also useful at the command line for 1-time use: $ perl -MTest2::Plugin::MemUsage path/to/test.t Output: # rss: 36708kB # size: 49836kB # peak: 49836kB =head1 SOURCE The source code repository for Test2-Plugin-MemUsage can be found at F. =head1 MAINTAINERS =over 4 =item Chad Granum Eexodist@cpan.orgE =back =head1 AUTHORS =over 4 =item Chad Granum Eexodist@cpan.orgE =back =head1 COPYRIGHT Copyright 2019 Chad Granum Eexodist@cpan.orgE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See F =cut