Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangfire big overhead / delay by scheduling jobs #2405

Open
goodstas opened this issue May 17, 2024 · 1 comment
Open

Hangfire big overhead / delay by scheduling jobs #2405

goodstas opened this issue May 17, 2024 · 1 comment

Comments

@goodstas
Copy link

Hi, i wrote very simple asp.net application to test the overhead of time of scheduling job with Hangfire.
I just used the ASP.NET Web core template and add some to code to do it.
I use InMemory storage for jobs if it's matters.
The code is below and the results are that there are SECONDS (2-10 seconds) of overhead time.
Is this normal? I think it's too big overhead.

`namespace WebHangfire.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {            
        MyTimer.Start();
        BackgroundJob.Schedule(() => MyTimer.Stop(), TimeSpan.FromSeconds(10));

        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }


}

public static class MyTimer
{
    private static Stopwatch stopwatch = new Stopwatch();

    public static void Start()
    {
        stopwatch.Start();
        //Console.WriteLine($"Current time : {DateTime.Now.ToString("HH:mm:ss.fff")}");
    }

    public static void Stop()
    {
        stopwatch.Stop();

        Console.WriteLine($"{TimeSpan.FromTicks(stopwatch.ElapsedTicks).TotalSeconds}");

        stopwatch.Reset();
    }
}

}`

@goodstas
Copy link
Author

I saw that i can play with the polling interval and this significantly improve the delay overhead for scheduled job.
What is a reasonable lowest polling interval to put? I implements some statistics server which gets thousands of items per second and needs to calculate the statistics based on these items.

If i put jobs not in the default queue, will it improve / change something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant