我有一个Web服务,应该在不到10毫秒的时间内返回答案。我必须进行许多验证过程,包括手机号码验证。一切正常,但我的手机号码验证执行时间完全不同!!有时它会在不到一毫秒的时间内返回,有时大约是2秒!请查看我的日志文件的一部分:(不同时间的相同条目)
[INFO ];[2015-08-25 9:51:09,073];[11];[Intermediate_Mobile.MobileContentValidator];[1800.2534 ms]
[INFO ];[2015-08-25 10:47:53,636];[5];[Intermediate_Mobile.MobileContentValidator];[5.4598 ms]
[INFO ];[2015-08-25 10:51:09,073];[38];[Intermediate_Mobile.MobileContentValidator];[0.1379 ms]
[INFO ];[2015-08-25 11:12:09,073];[45];[Intermediate_Mobile.MobileContentValidator];[1780.4578 ms]
这是我的方法:
public bool MobileContentValidator()
{
Regex len = new Regex(@"^.{2,20}$"); //length at least 6 characters and maximum of 20
Regex number = new Regex(@"^[0-9]*$"); //a string consisting only of numbers
PhoneNumber mobile;
Stopwatch sw = new Stopwatch();
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
sw.Start();
try
{
if (!len.IsMatch(this.Number))
{
this.IsNumberValid = false;
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
}
else if (!number.IsMatch(this.Number))
{
this.IsNumberValid = false;
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
}
else if (this.Region != "IR")
{
this.IsNumberValid = false;
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MB0003");
this.LogReason = Tools_MessageAccess.GetBriefMessage("user", "MB0003");
}
else
{
mobile = phoneUtil.Parse(this.Number, this.Region);
if (phoneUtil.IsValidNumber(mobile) != true)
{
this.IsNumberValid = false;
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MC0001");
this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MC0001");
}
else if (phoneUtil.GetNumberType(mobile).ToString() != "MOBILE")
{
this.IsNumberValid = false;
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "MU0001");
this.LogReason = Tools_MessageAccess.GetBriefMessage("log", "MU0001");
}
else
this.IsNumberValid = true;
}
sw.Stop();
Tools_Log.GetLogger().Info(Tools_Log.MessageForLogFile("mobile content validation completed successfully. ", "", sw.Elapsed.TotalMilliseconds));
return true;
}
catch (Exception ex)
{
this.UserReason = Tools_MessageAccess.GetBriefMessage("user", "G00001");
this.LogReason = ex.ToString();
Tools_Log.GetLogger().Error(Tools_Log.MessageForLogFile("mobile content validation failed due to the exception in application. ", ex.ToString(), sw.Elapsed.TotalMilliseconds));
return false;
}
}
我知道Google的libphonenumber
图书馆有点沉重,但我不知道为什么有时花这么长时间才能返回答案。正如我所说的,我有很多验证过程,只是这一行为很奇怪。谢谢。
更新#1:我认为问题很清楚。我的错。对于那个很抱歉。问题是为什么执行时间完全不同,我该如何解决?我的意思是我在写方法上犯了一个错误吗?
更新#2:更多日志:
[INFO ];[2015-08-25 12:03:33,793];[14];[Intermediate_Mobile.ConvertNumber];[0.3712];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,794];[14];[Intermediate_Mobile.MobileContentValidator];[2100.12];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,797];[14];[Intermediate_Mobile.MobileContentValidator];[0.0761];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:33,797];[14];[Intermediate_Mobile.ConvertNumber];[0.3456];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,395];[14];[Intermediate_Mobile.ConvertNumber];[0.3452];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,395];[14];[Intermediate_Mobile.MobileContentValidator];[0.0496];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,398];[14];[Intermediate_Mobile.MobileContentValidator];[0.0752];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:37,399];[14];[Intermediate_Mobile.ConvertNumber];[0.3593];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.MobileContentValidator];[0.0768];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.ConvertNumber];[0.3776];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,518];[11];[Intermediate_Mobile.MobileContentValidator];[0.0492];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,521];[11];[Intermediate_Mobile.MobileContentValidator];[0.0966];[];[(Message: mobile content validation completed successfully. )(Exeption: )]
[INFO ];[2015-08-25 12:03:38,521];[11];[Intermediate_Mobile.ConvertNumber];[0.383];[];[(Message: converting mobile number completed successfully. )(Exeption: )]
更新后的日志显示,一次加载时间只有2秒。如果使用(较大的)外部库,这是(某种)正常现象,这在第一次调用时可能需要加载某些程序集。在具有相同输入的后续调用中,您应该不会看到这样的延迟(因此,通过库的控制流也相同)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句