Search in sources :

Example 6 with UserAgentAnalyzer

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();
    }
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) PreDestroy(javax.annotation.PreDestroy)

Example 7 with UserAgentAnalyzer

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>";
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ArrayList(java.util.ArrayList) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)

Example 8 with UserAgentAnalyzer

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)));
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) Test(org.junit.jupiter.api.Test)

Example 9 with UserAgentAnalyzer

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);
}
Also used : TestCase(nl.basjes.parse.useragent.config.TestCase) UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) Test(org.junit.jupiter.api.Test)

Example 10 with UserAgentAnalyzer

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);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) Test(org.junit.jupiter.api.Test)

Aggregations

UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)38 Test (org.junit.jupiter.api.Test)24 UserAgent (nl.basjes.parse.useragent.UserAgent)11 ArrayList (java.util.ArrayList)4 Disabled (org.junit.jupiter.api.Disabled)4 Map (java.util.Map)3 UserAgentAnalyzerBuilder (nl.basjes.parse.useragent.UserAgentAnalyzer.UserAgentAnalyzerBuilder)3 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)3 Kryo (com.esotericsoftware.kryo.Kryo)2 ByteBufferInput (com.esotericsoftware.kryo.io.ByteBufferInput)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInput (java.io.ObjectInput)2 ObjectInputStream (java.io.ObjectInputStream)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 TestCase (nl.basjes.parse.useragent.config.TestCase)2 IngestDocument (org.elasticsearch.ingest.IngestDocument)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2