<commit_before>package org.stagemonitor.os;

import com.codahale.metrics.MetricRegistry;
import org.hyperic.sigar.Sigar;
import org.junit.Before;
import org.junit.Test;
import org.stagemonitor.core.CorePlugin;
import org.stagemonitor.core.MeasurementSession;
import org.stagemonitor.core.configuration.Configuration;
import org.stagemonitor.core.util.GraphiteSanitizer;

import java.io.IOException;

import static com.codahale.metrics.MetricRegistry.name;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OsPluginTest {

	private final MetricRegistry metricRegistry = new MetricRegistry();
	private static Sigar sigar;

	static {
		try {
			sigar = OsPlugin.newSigar();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Before
	public void setUp() throws Exception {
		OsPlugin osPlugin = new OsPlugin(sigar);
		final Configuration configuration = mock(Configuration.class);
		when(configuration.getConfig(CorePlugin.class)).thenReturn(mock(CorePlugin.class));
		osPlugin.initializePlugin(metricRegistry, configuration);
	}

	@Test
	public void testCpuUtilisation() throws Exception {
		double cpu = getDoubleGauge("os.cpu.usage.sys") +
				getDoubleGauge("os.cpu.usage.user") +
				getDoubleGauge("os.cpu.usage.idle") +
				getDoubleGauge("os.cpu.usage.nice") +
				getDoubleGauge("os.cpu.usage.wait") +
				getDoubleGauge("os.cpu.usage.interrupt") +
				getDoubleGauge("os.cpu.usage.soft-interrupt") +
				getDoubleGauge("os.cpu.usage.stolen");

		assertEquals(1.0, cpu, 0.000001);

		assertTrue(getDoubleGauge("os.cpu.usage-percent") >= 0);
		assertTrue(getDoubleGauge("os.cpu.usage-percent") <= 1);
	}

	@Test
	public void testCpuInfo() throws Exception {
		assertTrue(getIntGauge("os.cpu.info.mhz") > 0);
		assertTrue(getIntGauge("os.cpu.info.cores") > 0);
	}

	@Test
	public void testFileSystemUsage() throws Exception {
		String baseName = name("os.fs", GraphiteSanitizer.sanitizeGraphiteMetricSegment(sigar.getFileSystemList()[0].getDevName().replace("\\", "")));
		assertEquals(getLongGauge(name(baseName, "usage.total")),
				getLongGauge(name(baseName, "usage.free")) + getLongGauge(name(baseName, "usage.used")));
	}

	@Test
	public void testFileSystemMetrics() throws Exception {
		String baseName = name("os.fs", GraphiteSanitizer.sanitizeGraphiteMetricSegment(sigar.getFileSystemList()[0].getDevName().replace("\\", "")));
		assertTrue(getDoubleGauge(name(baseName, "usage-percent")) >= 0);
		assertTrue(getDoubleGauge(name(baseName, "usage-percent")) <= 1);
		assertTrue(getLongGauge(name(baseName, "reads.bytes")) >= 0);
		assertTrue(getLongGauge(name(baseName, "writes.bytes")) >= 0);
		assertTrue(getDoubleGauge(name(baseName, "disk.queue")) >= 0);
	}

	@Test
	public void testMemoryUsage() throws Exception {
		assertEquals(getLongGauge("os.mem.usage.total"),
				getLongGauge("os.mem.usage.used") + getLongGauge("os.mem.usage.free"));

		final double usage = getDoubleGauge("os.mem.usage-percent");
		assertTrue(Double.toString(usage), usage >= 0);
		assertTrue(Double.toString(usage), usage <= 1);
	}

	@Test
	public void testSwapUsage() throws Exception {
		assertEquals(getLongGauge("os.swap.usage.total"),
				getLongGauge("os.swap.usage.used") + getLongGauge("os.swap.usage.free"));
<commit_msg>fixed file system test<commit_after>package org.stagemonitor.os;

import com.codahale.metrics.MetricRegistry;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.junit.Before;
import org.junit.Test;
import org.stagemonitor.core.CorePlugin;
import org.stagemonitor.core.MeasurementSession;
import org.stagemonitor.core.configuration.Configuration;
import org.stagemonitor.core.util.GraphiteSanitizer;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import static com.codahale.metrics.MetricRegistry.name;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OsPluginTest {

	private final MetricRegistry metricRegistry = new MetricRegistry();
	private static Sigar sigar;

	static {
		try {
			sigar = OsPlugin.newSigar();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Before
	public void setUp() throws Exception {
		OsPlugin osPlugin = new OsPlugin(sigar);
		final Configuration configuration = mock(Configuration.class);
		when(configuration.getConfig(CorePlugin.class)).thenReturn(mock(CorePlugin.class));
		osPlugin.initializePlugin(metricRegistry, configuration);
	}

	@Test
	public void testCpuUtilisation() throws Exception {
		double cpu = getDoubleGauge("os.cpu.usage.sys") +
				getDoubleGauge("os.cpu.usage.user") +
				getDoubleGauge("os.cpu.usage.idle") +
				getDoubleGauge("os.cpu.usage.nice") +
				getDoubleGauge("os.cpu.usage.wait") +
				getDoubleGauge("os.cpu.usage.interrupt") +
				getDoubleGauge("os.cpu.usage.soft-interrupt") +
				getDoubleGauge("os.cpu.usage.stolen");

		assertEquals(1.0, cpu, 0.000001);

		assertTrue(getDoubleGauge("os.cpu.usage-percent") >= 0);
		assertTrue(getDoubleGauge("os.cpu.usage-percent") <= 1);
	}

	@Test
	public void testCpuInfo() throws Exception {
		assertTrue(getIntGauge("os.cpu.info.mhz") > 0);
		assertTrue(getIntGauge("os.cpu.info.cores") > 0);
	}

	@Test
	public void testFileSystemUsage() throws Exception {
		String baseName = getFsBaseName();
		assertEquals(getLongGauge(name(baseName, "usage.total")),
				getLongGauge(name(baseName, "usage.free")) + getLongGauge(name(baseName, "usage.used")));
	}

	private String getFsBaseName() throws SigarException {
		String fsName = "";
		@SuppressWarnings("unchecked")
		final Set<Map.Entry<String, FileSystem>> entries = (Set<Map.Entry<String, FileSystem>>) sigar.getFileSystemMap().entrySet();
		for (Map.Entry<String, FileSystem> e : entries) {
			if (e.getValue().getType() == FileSystem.TYPE_LOCAL_DISK) {
				fsName = e.getKey();
			}
		}
		return name("os.fs", GraphiteSanitizer.sanitizeGraphiteMetricSegment(fsName.replace("\\", "")));
	}

	@Test
	public void testFileSystemMetrics() throws Exception {
		String baseName = getFsBaseName();
		assertTrue(getDoubleGauge(name(baseName, "usage-percent")) >= 0);
		assertTrue(getDoubleGauge(name(baseName, "usage-percent")) <= 1);
		assertTrue(getLongGauge(name(baseName, "reads.bytes")) >= 0);
		assertTrue(getLongGauge(name(baseName, "writes.bytes")) >= 0);
		assertTrue(getDoubleGauge(name(baseName, "disk.queue")) >= 0);
	}

	@Test
	public void testMemoryUsage() throws Exception {
		assertEquals(getLongGauge("os.mem.usage.total"),
				getLongGauge("os.mem.usage.used") + getLongGauge("os.mem.usage.free"));

		final double usage = getDoubleGauge("os.mem.usage-percent");
		assertTrue(Double.toString(usage), usage >= 0);
		assertTrue(Double.toString(usage), usage <= 1);
	}

	@Test
	public void testSwapUsage() throws Exception {
		assertEquals(getLongGauge("os.swap.usage.total"),
				getLongGauge("os.swap.usage.used") + getLongGauge("os.swap.usage.free"));

{'fraction_non_alphanumeric': 0.14401986480893916, 'fraction_numerical': 0.005793902607256173, 'mean_word_length': 5.602914389799635, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918891', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2370, 'n_words': 397}