forked from fafeidou/fast-cloud-nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qinfuxiang
committed
Dec 1, 2021
1 parent
65cccf0
commit 9c61d08
Showing
10 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>fast.cloud.nacos</groupId> | ||
<artifactId>fast-source-code-analysis</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<groupId>fast.cloud.nacos</groupId> | ||
<artifactId>code-metrics</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>code-metrics</name> | ||
<description>code-metrics</description> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.dropwizard.metrics</groupId> | ||
<artifactId>metrics-core</artifactId> | ||
<version>3.2.6</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
34 changes: 34 additions & 0 deletions
34
...-source-code-analysis/code-metrics/src/main/java/fast/cloud/nacos/metrics/CacheGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fast.cloud.nacos.metrics; | ||
|
||
import com.codahale.metrics.CachedGauge; | ||
import com.codahale.metrics.ConsoleReporter; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CacheGauge { | ||
private static final MetricRegistry registry = new MetricRegistry(); | ||
|
||
private static final ConsoleReporter reporter = ConsoleReporter.forRegistry(registry) | ||
.convertRatesTo(TimeUnit.SECONDS) | ||
.convertDurationsTo(TimeUnit.SECONDS).build(); | ||
|
||
|
||
public static void main(String[] args) throws InterruptedException { | ||
reporter.start(10,TimeUnit.SECONDS); | ||
|
||
registry.gauge("cache-db-size", () -> new CachedGauge(30,TimeUnit.SECONDS) { | ||
@Override | ||
protected Object loadValue() { | ||
return queryFromDbSize(); | ||
} | ||
}); | ||
|
||
Thread.currentThread().join(); | ||
} | ||
|
||
private static Object queryFromDbSize() { | ||
System.out.println("=====queryFromDbSize===="); | ||
return System.currentTimeMillis(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...nalysis/code-metrics/src/main/java/fast/cloud/nacos/metrics/JmxAttributeGaugeExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fast.cloud.nacos.metrics; | ||
|
||
import com.codahale.metrics.ConsoleReporter; | ||
import com.codahale.metrics.JmxAttributeGauge; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
import javax.management.MalformedObjectNameException; | ||
import javax.management.ObjectName; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class JmxAttributeGaugeExample { | ||
private static final MetricRegistry registry = new MetricRegistry(); | ||
|
||
private static final ConsoleReporter reporter = ConsoleReporter.forRegistry(registry) | ||
.convertRatesTo(TimeUnit.SECONDS) | ||
.convertDurationsTo(TimeUnit.SECONDS).build(); | ||
|
||
public static void main(String[] args) throws MalformedObjectNameException, InterruptedException { | ||
reporter.start(10,TimeUnit.SECONDS); | ||
registry.register(MetricRegistry.name(JmxAttributeGaugeExample.class, "HeapMemory"), new JmxAttributeGauge(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage")); | ||
registry.register(MetricRegistry.name(JmxAttributeGaugeExample.class, "NonHeapMemory"), new JmxAttributeGauge(new ObjectName("java.lang:type=Memory"), "NonHeapMemoryUsage")); | ||
Thread.currentThread().join(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ource-code-analysis/code-metrics/src/main/java/fast/cloud/nacos/metrics/MeterExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package fast.cloud.nacos.metrics; | ||
|
||
import com.codahale.metrics.ConsoleReporter; | ||
import com.codahale.metrics.Meter; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static java.util.concurrent.ThreadLocalRandom.current; | ||
|
||
public class MeterExample { | ||
private static final MetricRegistry registry = new MetricRegistry(); | ||
|
||
private final static Meter requestMeter = registry.meter("tqs"); | ||
|
||
private final static Meter sizeMeter = registry.meter("volume"); | ||
|
||
public static void main(String[] args) { | ||
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry) | ||
.convertRatesTo(TimeUnit.MINUTES) | ||
.convertDurationsTo(TimeUnit.MINUTES).build(); | ||
reporter.start(10, TimeUnit.SECONDS); | ||
for (; ; ) { | ||
upload(new byte[current().nextInt(1000)]); | ||
randomSleep(); | ||
} | ||
} | ||
|
||
private static void randomSleep() { | ||
try { | ||
TimeUnit.SECONDS.sleep(current().nextInt(10)); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static void upload(byte[] bytes) { | ||
requestMeter.mark(); | ||
sizeMeter.mark(bytes.length); | ||
} | ||
|
||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...-source-code-analysis/code-metrics/src/main/java/fast/cloud/nacos/metrics/RadioGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package fast.cloud.nacos.metrics; | ||
|
||
import com.codahale.metrics.ConsoleReporter; | ||
import com.codahale.metrics.Meter; | ||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.RatioGauge; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static java.util.concurrent.ThreadLocalRandom.current; | ||
|
||
public class RadioGauge { | ||
private static final MetricRegistry registry = new MetricRegistry(); | ||
|
||
private static final ConsoleReporter reporter = ConsoleReporter.forRegistry(registry) | ||
.convertRatesTo(TimeUnit.SECONDS) | ||
.convertDurationsTo(TimeUnit.SECONDS).build(); | ||
|
||
private final static Meter totalMeter = new Meter(); | ||
|
||
private final static Meter successMeter = new Meter(); | ||
|
||
public static void main(String[] args) { | ||
reporter.start(10,TimeUnit.SECONDS); | ||
|
||
registry.gauge("success-rate", () -> new RatioGauge() { | ||
@Override | ||
protected Ratio getRatio() { | ||
return Ratio.of(successMeter.getCount(), totalMeter.getCount()); | ||
} | ||
}); | ||
for (; ; ) { | ||
shortSleep(); | ||
business(); | ||
} | ||
} | ||
|
||
private static void business() { | ||
totalMeter.mark(); | ||
try { | ||
int x = 10 / current().nextInt(6); | ||
successMeter.mark(); | ||
} catch (Exception e) { | ||
System.out.println("ERROR"); | ||
} | ||
} | ||
|
||
private static void shortSleep() { | ||
try { | ||
TimeUnit.SECONDS.sleep(current().nextInt(6)); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...code-analysis/code-metrics/src/main/java/fast/cloud/nacos/metrics/SimpleGaugeExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package fast.cloud.nacos.metrics; | ||
|
||
import com.codahale.metrics.ConsoleReporter; | ||
import com.codahale.metrics.Gauge; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static java.util.concurrent.ThreadLocalRandom.current; | ||
|
||
public class SimpleGaugeExample { | ||
private static final MetricRegistry registry = new MetricRegistry(); | ||
|
||
private static final ConsoleReporter reporter = ConsoleReporter.forRegistry(registry) | ||
.convertRatesTo(TimeUnit.SECONDS) | ||
.convertDurationsTo(TimeUnit.SECONDS).build(); | ||
|
||
private static Queue<Long> queue = new LinkedBlockingDeque<>(); | ||
|
||
|
||
public static void main(String[] args) { | ||
registry.register(MetricRegistry.name(SimpleGaugeExample.class), (Gauge<Integer>)queue::size); | ||
reporter.start(1,TimeUnit.SECONDS); | ||
new Thread(() -> { | ||
for (; ; ) { | ||
randomSleep(); | ||
queue.add(System.nanoTime()); | ||
} | ||
}).start(); | ||
|
||
new Thread(() -> { | ||
for (; ; ) { | ||
randomSleep(); | ||
queue.poll(); | ||
} | ||
}).start(); | ||
} | ||
|
||
private static void randomSleep() { | ||
try { | ||
TimeUnit.SECONDS.sleep(current().nextInt(5)); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
fast-source-code-analysis/code-metrics/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters