From 65b1b280c7871f8f9bdf10eab4d595d4bb69d689 Mon Sep 17 00:00:00 2001 From: huahuadan Date: Sun, 13 Oct 2024 11:46:29 +0800 Subject: [PATCH 1/2] refactor(log): standardize the printing of logs to avoid performance loss caused by string concatenation --- .../lgdcloudsim/datacenter/CollaborationManagerSimple.java | 4 ++-- .../org/lgdcloudsim/record/SqlRecordDetailScheduleTime.java | 4 ++-- src/main/java/org/lgdcloudsim/record/SqlRecordSimple.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/lgdcloudsim/datacenter/CollaborationManagerSimple.java b/src/main/java/org/lgdcloudsim/datacenter/CollaborationManagerSimple.java index 33b3b5e9..a2b8060b 100644 --- a/src/main/java/org/lgdcloudsim/datacenter/CollaborationManagerSimple.java +++ b/src/main/java/org/lgdcloudsim/datacenter/CollaborationManagerSimple.java @@ -106,7 +106,7 @@ private void datacenterAddCollaborationId(Datacenter datacenter, int collaborati if (datacenter == null) return; Set collaborationIds = datacenter.getCollaborationIds(); if (collaborationIds.contains(collaborationId)) { - LOGGER.warn("the datacenter(" + datacenter + ") already belongs to the collaboration " + collaborationId); + LOGGER.warn("the datacenter({}) already belongs to the collaboration {}", datacenter, collaborationId); } else { collaborationIds.add(collaborationId); } @@ -163,7 +163,7 @@ private void datacenterRemoveCollaborationId(Datacenter datacenter, int collabor if (datacenter == null) return; Set collaborationIds = datacenter.getCollaborationIds(); if (!collaborationIds.contains(collaborationId)) { - LOGGER.warn("the datacenter(" + datacenter + ") does not belong to the collaboration " + collaborationId + " to be removed"); + LOGGER.warn("the datacenter({})'s collaborationIds: {}", datacenter, collaborationIds); } else { collaborationIds.remove(collaborationId); } diff --git a/src/main/java/org/lgdcloudsim/record/SqlRecordDetailScheduleTime.java b/src/main/java/org/lgdcloudsim/record/SqlRecordDetailScheduleTime.java index 6a277010..04c95dbc 100644 --- a/src/main/java/org/lgdcloudsim/record/SqlRecordDetailScheduleTime.java +++ b/src/main/java/org/lgdcloudsim/record/SqlRecordDetailScheduleTime.java @@ -168,7 +168,7 @@ public SqlRecordDetailScheduleTime(String dbDir, String dbName, String userReque try { conn = DriverManager.getConnection("jdbc:sqlite:" + this.dbPath); conn.setAutoCommit(false); - LOGGER.info("Opened " + this.dbPath + " successfully"); + LOGGER.info("Opened {} successfully", this.dbPath); stmt = conn.createStatement(); createUserRequestTable(); @@ -371,7 +371,7 @@ public void recordInstanceGroupAllInfo(InstanceGroup instanceGroup) { stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); - LOGGER.error("The instance group " + instanceGroup.getId() + " recorded error {}", e.getMessage()); + LOGGER.error("The instance group {} recorded error {}", instanceGroup.getId(), e.getMessage()); try { sql = "SELECT * FROM " + this.instanceGroupTableName + " WHERE id = " + instanceGroup.getId() + ";"; ResultSet rs = stmt.executeQuery(sql); diff --git a/src/main/java/org/lgdcloudsim/record/SqlRecordSimple.java b/src/main/java/org/lgdcloudsim/record/SqlRecordSimple.java index 6e272e4d..10ba9d33 100644 --- a/src/main/java/org/lgdcloudsim/record/SqlRecordSimple.java +++ b/src/main/java/org/lgdcloudsim/record/SqlRecordSimple.java @@ -148,7 +148,7 @@ public SqlRecordSimple(String dbDir, String dbName, String userRequestTableName, try { conn = DriverManager.getConnection("jdbc:sqlite:" + this.dbPath); conn.setAutoCommit(false); - LOGGER.info("Opened " + this.dbPath + " successfully"); + LOGGER.info("Opened {} successfully", this.dbPath); stmt = conn.createStatement(); createUserRequestTable(); From a61f88bbae8f6459dc8385f12c6625c946428a79 Mon Sep 17 00:00:00 2001 From: huahuadan Date: Sun, 13 Oct 2024 11:54:54 +0800 Subject: [PATCH 2/2] refactor(cloudsim): add the clockStr variable to avoid regenerating the string every time the log is printed. --- src/main/java/org/lgdcloudsim/core/CloudSim.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/lgdcloudsim/core/CloudSim.java b/src/main/java/org/lgdcloudsim/core/CloudSim.java index 0a31e13e..37e6fb81 100644 --- a/src/main/java/org/lgdcloudsim/core/CloudSim.java +++ b/src/main/java/org/lgdcloudsim/core/CloudSim.java @@ -45,6 +45,11 @@ public class CloudSim implements Simulation { * The simulation time. */ double clock; + + /** + * The simulation time in string format. + */ + String clockStr; /** * A flag to indicate if the simulation is running or not. */ @@ -116,7 +121,7 @@ public class CloudSim implements Simulation { * Creates a new CloudSim instance. */ public CloudSim() { - clock = 0; + setClock(0); this.entityList = new ArrayList<>(); this.future = new FutureQueue(); this.deferred = new DeferredQueue(); @@ -132,12 +137,13 @@ public double clock() { @Override public String clockStr() { - return "%.2f ms".formatted(clock); + return clockStr; } @Override public Simulation setClock(double time) { this.clock = time; + this.clockStr = "%.2f ms".formatted(clock); return this; }