<commit_before>#!/usr/bin/env python

import unittest
import subprocess
import time

import psutil

from test_psutil import kill, PYTHON, DEVNULL


class BSDSpecificTestCase(unittest.TestCase):

    def setUp(self):
        self.pid = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL).pid

    def tearDown(self):
        kill(self.pid)

    def test_TOTAL_PHYMEM(self):
        p = subprocess.Popen('sysctl -a | grep hw.physmem', shell=1, stdout=subprocess.PIPE)
        out = p.stdout.read()
        value = int(out.split()[1])
        self.assertEqual(value, psutil.TOTAL_PHYMEM)

    def test_process_create_time(self):
        cmdline = "ps -o lstart -p %s" %self.pid
        p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
        output = p.communicate()[0]
        start_ps = output.replace('STARTED', '').strip()
        start_psutil = psutil.Process(self.pid).create_time
        start_psutil = time.strftime("%a %b %d %H:%M:%S %Y", 
                                     time.localtime(start_psutil))
        self.assertEqual(start_ps, start_psutil)


<commit_msg>add avail_phymem() test for freeBSD<commit_after>#!/usr/bin/env python

import unittest
import subprocess
import time

import psutil

from test_psutil import kill, PYTHON, DEVNULL


def sh(cmd):
    p = subprocess.Popen(cmd, shell=1, stdout=subprocess.PIPE) 
    return p.communicate()[0].strip()


class BSDSpecificTestCase(unittest.TestCase):

    def setUp(self):
        self.pid = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL).pid

    def tearDown(self):
        kill(self.pid)

    def test_TOTAL_PHYMEM(self):
        p = subprocess.Popen('sysctl -a | grep hw.physmem', shell=1, stdout=subprocess.PIPE)
        out = p.stdout.read()
        value = int(out.split()[1])
        self.assertEqual(value, psutil.TOTAL_PHYMEM)

    def test_avail_phymem(self):
        # This test is not particularly precise and may fail if the OS is
        # consuming memory for other applications. 
        # We just want to test that the difference between psutil result
        # and sysctl's is not too high.
        _sum = sum((
            int(sh("sysctl vm.stats.vm.v_inactive_count").split()[1]),
            int(sh("sysctl vm.stats.vm.v_cache_count").split()[1]),
            int(sh("sysctl vm.stats.vm.v_free_count").split()[1])
            ))
        _pagesize = int(sh("sysctl hw.pagesize").split()[1])
        sysctl_avail_phymem = _sum * _pagesize
        psutil_avail_phymem =  psutil.avail_phymem()
        difference = psutil_avail_phymem - sysctl_avail_phymem
        # on my system the difference is of 348160 bytes;
        # let's assume it is a filaure if it's higher than 0.5 mega bytes
        if difference > 512000:
            self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
                      sysctl_avail_phymem, psutil_avail_phymem, difference))

    def test_process_create_time(self):
        cmdline = "ps -o lstart -p %s" %self.pid
        p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
        output = p.communicate()[0]
        start_ps = output.replace('STARTED', '').strip()
        start_psutil = psutil.Process(self.pid).create_time
        start_psutil = time.strftime("%a %b %d %H:%M:%S %Y", 
                                     time.localtime(start_psutil))
        self.assertEqual(start_ps, start_psutil)



{'fraction_non_alphanumeric': 0.11825726141078838, 'fraction_numerical': 0.008298755186721992, 'mean_word_length': 2.7458379578246395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1010759', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1103, 'n_words': 241}