当前位置: 首页 > news >正文

网站快速备案安全如何把官网

网站快速备案安全,如何把官网,中国建造师人才网,成都生物城建设有限公司网站来源#xff1a;jitwxs.cn/5aa91d10.html一、前言代码耗时统计在日常开发中算是一个十分常见的需求#xff0c;特别是在需要找出代码性能瓶颈时。可能也是受限于 Java 的语言特性#xff0c;总觉得代码写起来不够优雅#xff0c;大量的耗时统计代码#xff0c;干扰了业务逻… 来源jitwxs.cn/5aa91d10.html一、前言代码耗时统计在日常开发中算是一个十分常见的需求特别是在需要找出代码性能瓶颈时。可能也是受限于 Java 的语言特性总觉得代码写起来不够优雅大量的耗时统计代码干扰了业务逻辑。特别是开发功能的时候有个感受就是刚刚开发完代码很清爽优雅结果加了一大堆辅助代码后整个代码就变得臃肿了自己看着都挺难受。因此总想着能不能把这块写的更优雅一点今天本文就尝试探讨下“代码耗时统计”这一块。在开始正文前先说下前提“代码耗时统计”的并不是某个方法的耗时而是任意代码段之间的耗时。这个代码段可能是一个方法中的几行代码也有可能是从这个方法的某一行到另一个被调用方法的某一行因此通过 AOP 方式是不能实现这个需求的。二、常规方法2.1 时间差统计这种方式是最简单的方法记录下开始时间再记录下结束时间计算时间差即可。public class TimeDiffTest {public static void main(String[] args) throws InterruptedException {final long startMs  TimeUtils.nowMs();TimeUnit.SECONDS.sleep(5); // 模拟业务代码System.out.println(timeCost:   TimeUtils.diffMs(startMs));} } /* output:  timeCost: 5005 public class TimeUtils {/*** return 当前毫秒数*/public static long nowMs() {return System.currentTimeMillis();}/*** 当前毫秒与起始毫秒差* param startMillis 开始纳秒数* return 时间差*/public static long diffMs(long startMillis) {return diffMs(startMillis, nowMs());} } 这种方式的优点是实现简单利于理解缺点就是对代码的侵入性较大看着很傻瓜不优雅。2.2 StopWatch第二种方式是参考 StopWatch StopWatch 通常被用作统计代码耗时各个框架和 Common 包都有自己的实现。public class TraceWatchTest {public static void main(String[] args) throws InterruptedException {TraceWatch traceWatch  new TraceWatch();traceWatch.start(function1);TimeUnit.SECONDS.sleep(1); // 模拟业务代码traceWatch.stop();traceWatch.start(function2);TimeUnit.SECONDS.sleep(1); // 模拟业务代码traceWatch.stop();traceWatch.record(function1, 1); // 直接记录耗时System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));} }/* output:  {function2:[{data:1000,taskName:function2}],function1:[{data:1000,taskName:function1},{data:1,taskName:function1}]} */ public class TraceWatch {/** Start time of the current task. */private long startMs;/** Name of the current task. */Nullableprivate String currentTaskName;Getterprivate final MapString, ListTaskInfo taskMap  new HashMap();/*** 开始时间差类型指标记录如果需要终止请调用 {link #stop()}** param taskName 指标名*/public void start(String taskName) throws IllegalStateException {if (this.currentTaskName ! null) {throw new IllegalStateException(Cant start TraceWatch: its already running);}this.currentTaskName  taskName;this.startMs  TimeUtils.nowMs();}/*** 终止时间差类型指标记录调用前请确保已经调用*/public void stop() throws IllegalStateException {if (this.currentTaskName  null) {throw new IllegalStateException(Cant stop TraceWatch: its not running);}long lastTime  TimeUtils.nowMs() - this.startMs;TaskInfo info  new TaskInfo(this.currentTaskName, lastTime);this.taskMap.computeIfAbsent(this.currentTaskName, e - new LinkedList()).add(info);this.currentTaskName  null;}/*** 直接记录指标数据不局限于时间差类型*  param taskName 指标名* param data 指标数据*/public void record(String taskName, Object data) {TaskInfo info  new TaskInfo(taskName, data);this.taskMap.computeIfAbsent(taskName, e - new LinkedList()).add(info);}GetterAllArgsConstructorpublic static final class TaskInfo {private final String taskName;private final Object data;} } 我是仿照 org.springframework.util.StopWatch 的实现写了 TraceWatch 类这个方法提供了两种耗时统计的方法1.通过调用 Start(name) 和 Stop() 方法进行耗时统计。2.通过调用 Record(name, timeCost)方法直接记录耗时信息。这种方式本质上和“时间差统计”是一致的只是抽取了一层稍微优雅了一点。注你可以根据自己的业务需要自行修改 TraceWatch 内部的数据结构我这里简单起见内部的数据结构只是随便举了个例子。三、高级方法第二节提到的两种方法用大白话来说都是“直来直去”的感觉我们还可以尝试把代码写的更简便一点。3.1 Function在 jdk 1.8 中引入了 java.util.function 包通过该类提供的接口能够实现在指定代码段的上下文执行额外代码的功能。public class TraceHolderTest {public static void main(String[] args) {TraceWatch traceWatch  new TraceWatch();TraceHolder.run(traceWatch, function1, i - {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码} catch (InterruptedException e) {e.printStackTrace();}});String result  TraceHolder.run(traceWatch, function2, () - {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码return YES;} catch (InterruptedException e) {e.printStackTrace();return NO;}});TraceHolder.run(traceWatch, function1, i - {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码} catch (InterruptedException e) {e.printStackTrace();}});System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));} }/* output:  {function2:[{data:1004,taskName:function2}],function1:[{data:1001,taskName:function1},{data:1002,taskName:function1}]} */ public class TraceHolder {/*** 有返回值调用*/public static T T run(TraceWatch traceWatch, String taskName, SupplierT supplier) {try {traceWatch.start(taskName);return supplier.get();} finally {traceWatch.stop();}}/*** 无返回值调用*/public static void run(TraceWatch traceWatch, String taskName, IntConsumer function) {try {traceWatch.start(taskName);function.accept(0);} finally {traceWatch.stop();}} } 这里我利用了 Supplier 和 IntConsumer 接口对外提供了有返回值和无返回值得调用在 TraceHolder 类中在核心代码块的前后分别调用了前文的 TraceWatch 的方法实现了耗时统计的功能。3.2 AutoCloseable除了利用 Function 的特性我们还可以使用 jdk 1.7 的 AutoCloseable 特性。说 AutoCloseable 可能有同学没听过但是给大家展示下以下代码就会立刻明白是什么东西了。// 未使用 AutoCloseable public static String readFirstLingFromFile(String path) throws IOException {BufferedReader br  null;try {br  new BufferedReader(new FileReader(path));return br.readLine();} catch (IOException e) {e.printStackTrace();} finally {if (br ! null) {br.close();}}return null; }// 使用 AutoCloseable public static String readFirstLineFromFile(String path) throws IOException {try (BufferedReader br  new BufferedReader(new FileReader(path))) {return br.readLine();} } 在 try 后方可以加载一个实现了 AutoCloseable 接口的对象该对象作用于整个 try 语句块中并且在执行完毕后回调 AutoCloseable#close() 方法。让我们对 TraceWatch 类进行改造1.实现 AutoCloseable 接口实现 close() 接口Override public void close() {this.stop(); } 修改 start() 方法使其支持链式调用public TraceWatch start(String taskName) throws IllegalStateException {if (this.currentTaskName ! null) {throw new IllegalStateException(Cant start TraceWatch: its already running);}this.currentTaskName  taskName;this.startMs  TimeUtils.nowMs();return this; } public class AutoCloseableTest {public static void main(String[] args) {TraceWatch traceWatch  new TraceWatch();try(TraceWatch ignored  traceWatch.start(function1)) {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码} catch (InterruptedException e) {e.printStackTrace();}}try(TraceWatch ignored  traceWatch.start(function2)) {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码} catch (InterruptedException e) {e.printStackTrace();}}try(TraceWatch ignored  traceWatch.start(function1)) {try {TimeUnit.SECONDS.sleep(1); // 模拟业务代码} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));} }/* output:  {function2:[{data:1001,taskName:function2}],function1:[{data:1002,taskName:function1},{data:1002,taskName:function1}]} */ 四、总结本文列举了四种统计代码耗时的方法时间差统计StopWatchFunctionAutoCloseable列举的方案是我目前能想到的方案。当然可能有更加优雅的方案希望有相关经验的同学能在评论区一起交流下~
http://www.yutouwan.com/news/320084/

相关文章:

  • 非凡网站开发培训免费crm手机版
  • 如何免费自己做网站wordpress 添加友情
  • 怎么做下载类的网站吗dedecms 调用 另一个网站
  • 免费做图表的网站网站建设套餐报价方案
  • 本溪网站开发公司电话wordpress 本机安装
  • 微信开放平台的功能介绍上海网站建设seo推广
  • 陕西高速公路建设集团网站合肥网站建设团队
  • 网站无内容 备案公司网站维护内容
  • 小程序开发平台源代码下载公众号seo排名软件
  • 网站建设公司电话销售话术媒体资源
  • 简易的在线数据库网站模板下载建设网站的法律可行性
  • 百度推广要自己建站吗白云网站建设价格
  • 聊城网站建设品牌太原网站建设方案托管
  • 旅游公司网站设计wordpress能做手机站吗
  • 做网站收款支付宝接口app软件制作多少钱
  • 求西北地区网站建设专家 西安沉睡网络 官方网址?销售类网站开发架构
  • wordpress 竞拍天津百度seo排名优化
  • 手工制作会动的玩具模板网站怎么建设优化
  • 个人网站怎么自己备案免费做网站教程
  • 分享网站模板苏州吴中区seo关键词优化排名
  • 辽宁省城乡和住房建设厅网站网站如何seo
  • 建设厅官方网站北京企业vi设计公司企业vi设计欣赏
  • 文山州住房建设网站广州哪里可以做网站
  • 免费建自己域名的网站编程培训加盟
  • 漂亮网站聚美优品网站建设导向
  • 移商网站建设wordpress不能更新插件
  • 装饰设计网站新冠怎么突然不见了
  • 最好的网站模板网站黑马程序员培训机构
  • 镇江做网站哪家公司好wordpress user login
  • 提供微网站制作网络公司我做网站如何分流客户