use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class ParseService method preDestroy.
@PreDestroy
public void preDestroy() {
if (userAgentAnalyzer != null) {
UserAgentAnalyzer uaa = userAgentAnalyzer;
// First we disable it for all uses.
userAgentAnalyzer = null;
userAgentAnalyzerIsAvailable = false;
userAgentAnalyzerFailureMessage = "UserAgentAnalyzer has been destroyed.";
// Then we actually wipe it.
uaa.destroy();
}
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class ApiXMLOutput method createOutput.
// -------------------------------------------------
private String createOutput(String userAgentString) {
if (userAgentString == null) {
throw new MissingUserAgentException();
}
ensureStartedForApis(OutputType.XML);
if (userAgentAnalyzerIsAvailable()) {
UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
List<String> result = new ArrayList<>(2048);
for (String input : splitPerFilledLine(userAgentString)) {
UserAgent userAgent = userAgentAnalyzer.parse(input);
result.add(userAgent.toXML(userAgent.getCleanedAvailableFieldNamesSorted()));
}
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + String.join("\n", result);
}
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Yauaa></Yauaa>";
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class TestClassifier method testLimitedFields.
// Check https://github.com/nielsbasjes/yauaa/issues/236
@Test
void testLimitedFields() {
UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().withCache(10000).hideMatcherLoadStats().withField("AgentName").withField("AgentVersion").withField("LayoutEngineName").withField("LayoutEngineVersion").withField("OperatingSystemName").build();
String useragent = "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Google-Safety; +http://www.google.com/bot.html)";
assertEquals(ROBOT, getDeviceClass(uaa.parse(useragent)));
assertFalse(isHuman(uaa.parse(useragent)));
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class TestConcurrentPerformance method testCachedMultiThreadedPerformance.
@Test
void testCachedMultiThreadedPerformance() throws InterruptedException {
// NOSONAR: Do not complain about ignored performance test
UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().immediateInitialization().keepTests().build();
// This testcase does not occur in the rest of the testcases (manually manipulated version for the Chrome part).
String cachedUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.1.2.3.4.5.6 Safari/537.36";
List<TestCase> testCases = uaa.getTestCases();
// Make sure it is not in there.
assertEquals(0, testCases.stream().filter(testCase -> testCase.getUserAgent().equals(cachedUserAgent)).count());
long totalIterations = 0;
long totalNanosUsed = 0;
for (int i = 0; i < 10; i++) {
LOG.info("Iteration {} : Start", i);
FireAllTestCases fireTests = new FireAllTestCases(uaa, testCases);
List<RunCachedTestCase> cachedTestCases = Arrays.asList(new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000), new RunCachedTestCase(uaa, cachedUserAgent, 10_000_000));
// Wipe the cache for the new run.
uaa.clearCache();
// Now parse and cache the precached useragent.
uaa.parse(cachedUserAgent);
// Start both
fireTests.start();
cachedTestCases.forEach(Thread::start);
// Wait for both to finish
fireTests.join();
for (RunCachedTestCase ctc : cachedTestCases) {
ctc.join();
}
for (RunCachedTestCase cachedTestCase : cachedTestCases) {
long iterations = cachedTestCase.getIterations();
long nanosUsed = cachedTestCase.getNanosUsed();
LOG.info("Iteration {} : Took {}ns ({}ms) = {}ns each", i, nanosUsed, (nanosUsed) / 1_000_000L, nanosUsed / iterations);
}
}
LOG.info("Average : {}ns ({}ms) = {}ns each", totalNanosUsed, (totalNanosUsed) / 1_000_000L, totalNanosUsed / totalIterations);
}
use of nl.basjes.parse.useragent.UserAgentAnalyzer in project yauaa by nielsbasjes.
the class TestPerformance method checkAllPossibleFieldsFastSpeed.
@Test
void checkAllPossibleFieldsFastSpeed() {
LOG.info("Create analyzer");
long start = System.nanoTime();
UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().keepTests().delayInitialization().build();
long stop = System.nanoTime();
long constructMsecs = (stop - start) / 1000000;
LOG.info("-- Construction time: {}ms", constructMsecs);
LOG.info("List fieldnames");
start = System.nanoTime();
uaa.getAllPossibleFieldNamesSorted().forEach(LOG::info);
stop = System.nanoTime();
long listFieldNamesMsecs = (stop - start) / 1000000;
LOG.info("-- List fieldnames: {}ms", listFieldNamesMsecs);
assertTrue(listFieldNamesMsecs < 500, "Just listing the field names should only take a few ms");
LOG.info("Initializing the datastructures");
start = System.nanoTime();
uaa.initializeMatchers();
stop = System.nanoTime();
long initializeMsecs = (stop - start) / 1000000;
LOG.info("-- Initialization: {}ms", initializeMsecs);
// assertTrue("The initialization went too fast, this should take several seconds", initializeMsecs > 100);
LOG.info("Preheat");
start = System.nanoTime();
uaa.preHeat();
stop = System.nanoTime();
long preheatMsecs = (stop - start) / 1000000;
LOG.info("-- Preheat : {}ms", preheatMsecs);
}
Aggregations