Skip to content

Cloud vs. Open Source: Language Detection Tools Tested in Microservice Architectures

azure ai language services detection tested performance

Language Detection Context

In my previous blog post, I compared solutions for language detection, focusing on Azure AI Language versus NuGet options like NTextCat and PanLingo. While NuGet solutions are suitable for specific use cases—mainly offline scenarios—you can read the full comparison here.

However, an interesting question remains: how do these solutions perform in a microservice architecture? Azure AI Language already provides this functionality as a REST service, with limited control over its inner workings. By adding an API Management layer for authentication and logging, it seems to fit well in modern architectures. But how do open-source alternatives compare? And is it worth pursuing?

Performance Testing Setup

For this test, I made some key assumptions:

  • Batch processing: Azure AI Language detection supports batch processing, which most NuGet solutions lack out of the box. So, peak performance won’t be tested.
  • Serverless replication: To create a fair comparison, NTextCat and PanLingo were run in a serverless setup using Azure Functions. Performance may improve with a more robust infrastructure.
  • Simplified environment: No security layers, logging, Application Insights, or API Management were used.
  • Consistent location: All services were deployed in the same Azure region.

Testing Parameters:

  • Iterations: 500 runs
  • Delay: 50ms between requests
  • Parallel clients: 1

Azure AI Language stress test

REST Setup

Azure AI Language detection is inherently designed for microservices. To test, I made a simple REST request with the Ocp-Apim-Subscription-Key header for authentication to the endpoint.

Every response will have a ‘x-envoy-upstream-service-time’ header present, it’s the time in milliseconds spent by the service to process the request. This is useful when we want to determine service time compared to network latency.

Sample Request (Single Document):

{
    "kind": "LanguageDetection",
    "parameters": { "modelVersion": "latest" },
    "analysisInput": {
        "documents": [
            {
                "id": "1",
                "text": "En un pequeño pueblo rodeado de montañas..."
            }
        ]
    }
}

Bulk Detection Example:
Azure also supports bulk requests, which can enhance performance when detecting multiple languages. Note that the x-envoy-upstream-service-time header in the response will be very similar compared to a single request.

{
    "kind": "LanguageDetection",
    "parameters": { "modelVersion": "latest" },
    "analysisInput": {
        "documents": [
            { "id": "1", "text": "En un pequeño pueblo rodeado de montañas..." },
            { "id": "2", "text": "Los habitantes vivían en armonía..." }
        ]
    }
}

NTextCat stress test

NTextCat is an open-source NuGet library, encapsulated here within an Azure Function. For optimisation, the RankedLanguageIdentifier is preloaded during the function initialisation to remove runtime overhead.

C# Azure Function Code:

public class NTextCatSampleFunction
{
    private static RankedLanguageIdentifier RankedLanguageIdentifier;

    static NTextCatSampleFunction()
    {
        var location = AppDomain.CurrentDomain.GetAssemblies()
            .FirstOrDefault(a => a.GetName().Name.Contains("NTextCat"))?.Location;
        var path = Path.Combine(Path.GetDirectoryName(location), "LanguageModels", "Core14.profile.xml");
        RankedLanguageIdentifier = new RankedLanguageIdentifierFactory().Load(path);
    }

    [Function("DetectLanguageWithNTextCat")]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        var language = RankedLanguageIdentifier.Identify(requestBody).FirstOrDefault();
        response.WriteString($"Language: {language.Item1.Iso639_2T}");
        return response;
    }
}

PanLingo stress test

PanLingo is also open-source but provides multiple language detection features, acting as a wrapper for non-C# libraries. I chose the CLD3 feature, which uses neural networks—similar to Azure AI Language detection.

panlingo ai language prediction features
Panlingo ai language prediction features

C# Azure Function Code:

public static class PanlingoSampleFunction
{
    [FunctionName("DetectLanguageWithPanlingo")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        using var cld3 = new CLD3Detector(minNumBytes: 0, maxNumBytes: 5120);
        var prediction = cld3.PredictLanguage(requestBody);
        return new OkObjectResult($"Language: {prediction.Language}");
    }
}

Performance Test Results

Language Detection MethodAverage Response Time
Azure AI Language detection19ms
PanLingo language detection28ms
NTextCat language detection41ms

Note: These tests were done on my local machine. Latency would likely improve with all applications deployed in the same Azure region.

Analysis and Key Takeaways

  1. Setup Simplicity:
    • Azure AI Language API is straightforward, with minimal configuration required.
    • NuGet solutions add hosting responsibilities, requiring you to manage .NET updates, security patches, and troubleshooting.
  2. Performance:
    • Azure AI Language outperformed the others, but how critical is a 10-20ms difference to your application?
    • Surprisingly, NuGet solutions didn’t perform as well as expected, suggesting Microsoft’s AI services scale better than open-source alternatives.
  3. Cost and Support:
    • Azure AI offers a free tier for up to 5,000 text records/month with included support, making it ideal for small projects as well.
    • Open-source solutions may appear free but involve hosting and maintenance costs. Additionally, relying on GitHub repositories for support can be risky for production-critical systems.
  4. Bulk Processing Advantage:
    • Azure AI’s bulk detection feature wasn’t fully tested here but could further reduce response times for large workloads. Check out this blog post about Azure AI services in containers.

In conclusion, for most scenarios, Azure AI Language detection is a reliable choice due to its performance, ease of use, and scalability. NuGet solutions might still hold value for niche offline use cases, but careful consideration is needed before implementing them in production environments.