public static synchronized Sampler getSampler(final int sampleTimeMillis, final int dumpTimeMillis, final SamplerSupplier collector, final File dumpFolder, final String dumpFilePrefix) throws InterruptedException { if (instance == null) { try { instance = new Sampler(sampleTimeMillis, dumpTimeMillis, collector, dumpFolder.getCanonicalFile(), dumpFilePrefix); } catch (IOException ex) { throw new UncheckedIOException(ex); } instance.registerJmx(); return instance; } else { instance.dispose(); try { instance = new Sampler(sampleTimeMillis, dumpTimeMillis, collector, dumpFolder.getCanonicalFile(), dumpFilePrefix); } catch (IOException ex) { throw new UncheckedIOException(ex); } instance.registerJmx(); return instance; } }
sampler.registerJmx();
public Map<String, SampleNode> sampleTest(final Sampler sampler, final String filename) throws InterruptedException, IOException { sampler.registerJmx(); sampler.start(); MonitorTest.main(new String[]{}); final File serializedFile = File.createTempFile(filename, ".ssdump3"); Map<String, SampleNode> collected = sampler.getStackCollectionsAndReset(); Converter.saveLabeledDumps(serializedFile, collected); LOG.debug("Dumped to file {}", serializedFile); sampler.stop(); Map<String, SampleNode> loadedDumps = Converter.loadLabeledDumps(serializedFile); for (Map.Entry<String, SampleNode> entry : loadedDumps.entrySet()) { LOG.debug("Loaded {}", entry.getKey()); } return collected; } }
@SuppressFBWarnings("MDM_THREAD_YIELD") public static void main(final String[] args) throws InterruptedException, TimeoutException, SQLException, IOException { String connectionString = args[0]; String semaphoreName = args[1]; JdbcDataSource ds = new JdbcDataSource(); ds.setURL(connectionString); ds.setUser("sa"); ds.setPassword("sa"); Sampler s = new Sampler(5, 10000); s.registerJmx(); s.start(); LOG.info("started sampling"); JdbcSemaphore semaphore = new JdbcSemaphore(ds, semaphoreName, 3); for (int i = 0; i < 50; i++) { semaphore.acquire(1, 1L, TimeUnit.SECONDS); Thread.sleep((long) (Math.random() * 10) + 10); LOG.info("beat"); Thread.sleep((long) (Math.random() * 10) + 10); semaphore.release(); } semaphore.close(); File dumpToFile = s.dumpToFile(); LOG.info("stack samples dumped to {}", dumpToFile); s.stop(); System.exit(0); }
@Test public void testJmx() throws InterruptedException, IOException { Sampler sampler = new Sampler((t) -> new SimpleStackCollector(t)); sampler.registerJmx(); sampler.start(); main(new String[]{}); sampler.stop(); SampleNode original = sampler.getStackCollections().values().iterator().next(); File file = sampler.dumpToFile(); LOG.debug("Samples saved to {}", file); Assert.assertNotNull(file); Assert.assertTrue(file.exists()); SampleNode loaded = Converter.load(file); Assert.assertEquals(original, loaded); sampler.stop(); }
JdbcSemaphore semaphore = new JdbcSemaphore(ds, "test_sem2", 1, true); Sampler s = new Sampler(5, 5000); s.registerJmx(); s.start(); LOG.info("started sampling");