validating - Microbenchmarking

本文通过Java代码基准测试,对比了不同数组设置方法的运行效率。实验显示,在使用共享资源的情况下,并行处理并不总是更快。

Benchmarking means timing pieces of code or algorithms to see which runs faster.

// onjava/Timer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package onjava;

import static java.util.concurrent.TimeUnit.*;

public class Timer {
  private long start = System.nanoTime();

  public long duration() {
    return NANOSECONDS.toMillis(System.nanoTime() - start);
  }

  public static long duration(Runnable test) {
    Timer timer = new Timer();
    test.run();
    return timer.duration();
  }
}
// validating/BadMicroBenchmark.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {ExcludeFromTravisCI}

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark {
  static final int SIZE = 250_000_000; // 250 million

  public static void main(String[] args) {
    try { // For machines with insufficient memory
      long[] la = new long[SIZE];
      System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> n))); // Arrays.setAll since 1.8
      System.out.println(
          "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> n))); // Arrays.parallelSetAll since 1.8
    } catch (OutOfMemoryError e) {
      System.out.println("Insufficient memory");
      System.exit(0);
    }
  }
}
/* My Output:
setAll: 217
parallelSetAll: 204
*/

 when depends on a common resource, the parallel version can end up being much slower, as the separate contend for that resource

// validating/BadMicroBenchmark2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Relying on a common resource

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark2 {
  // SIZE reduced to make it run faster:
  static final int SIZE = 5_000_000;

  public static void main(String[] args) {
    long[] la = new long[SIZE];
    Random r = new Random();
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> r.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> r.nextLong())));
    SplittableRandom sr = new SplittableRandom(); // since 1.8, is designed for parallel algorithms
// This algorithm was inspired by the "DotMix" algorithm
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> sr.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> sr.nextLong())));
  }
}
/* My Output:
parallelSetAll: 946
setAll: 106
parallelSetAll: 73
setAll: 16
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/onjava/Timer.java

3. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/System.java

4. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/TimeUnit.java

5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark.java

6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark2.java

7. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/SplittableRandom.java

那你覺得我這七個設計可以嗎? ````yaml # repo-root/kustomization.yaml # 全域入口:聚合平台治理與命名空間層 apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - platform-governance - namespaces # 全域通用標籤,利於觀測/審計 commonLabels: root.io/repo: repo-root root.io/entry: global # 可選:全域 namePrefix/nameSuffix,預設不加以避免資源名漂移 # namePrefix: "" # nameSuffix: "" # 可選:全域生成 ConfigMap/Secret(目前無) # configMapGenerator: [] # secretGenerator: [] # 可選:全域 patches(目前無) # patches: [] ```` ````yaml # repo-root/platform-governance/kustomization.yaml # 平台治理頂層,聚合控制器、政策、觀測稽核 apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - 10-controllers - 20-clusterpolicies - 99-observability commonLabels: app.kubernetes.io/part-of: platform-governance result.io/owner: platform-team root.io/repo: repo-root # 可選:針對治理層單獨定義 namePrefix # namePrefix: "gov-" ```` ````yaml # repo-root/platform-governance/10-controllers/kustomization.yaml # 控制器層:Kyverno 或原生 VAP/VAC 相關控制面資源 # 預設部署至 kyverno 命名空間(Kyverno Chart 安裝時會建立) apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namespace: kyverno resources: # 若以 Manifest 方式安裝 Kyverno(或改為 HelmRelease 由 Flux/Helm Operator 管理) - kyverno/install.yaml # 原生 ValidatingAdmissionPolicy/PolicyBinding(K8s 1.30+ 穩定) - validating-admission-policy/prevent-foundation-namespace-deletion.yaml # 建議額外補強(選用,視您是否同時採用 VAP/VAC 與 Kyverno): # - validating-admission-policy/base-constraints.yaml # - validating-admission-policy/labels-required.yaml # - validating-admission-policy/psa-enforce.yaml # - validating-admission-policy/require-immutable-annotations.yaml # - validating-admission-policy/require-signed-images-binding.yaml # 以上檔案可先留空/分階段引入;若您確認要加,我可一併提供模板。 commonLabels: app.kubernetes.io/part-of: platform-governance app.kubernetes.io/component: controllers root.io/repo: repo-root # 可選:避免 CRD 尚未註冊導致順序問題,可將 CRD 類資源放在 install.yaml 內最前段 # patches: [] ```` ````yaml # repo-root/platform-governance/20-clusterpolicies/kustomization.yaml # 政策層(以 Kyverno 為例);若採 VAP/VAC,對應轉為 ValidatingAdmissionPolicy 套件 apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - immutable-namespace-meta.yaml - restrict-pod-security.yaml - require-signed-images.yaml commonLabels: app.kubernetes.io/part-of: platform-governance app.kubernetes.io/component: policies root.io/repo: repo-root # 可選:針對不同環境的覆寫(dev/stage/prod) # patches: [] ```` ````yaml # repo-root/platform-governance/99-observability/kustomization.yaml # 觀測稽核層:政策指標與審計日誌轉送 apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - policy-metrics-scrape.yaml - audit-log-forwarding.yaml commonLabels: app.kubernetes.io/part-of: platform-governance app.kubernetes.io/component: observability root.io/repo: repo-root # 若階段性關閉觀測,resources 可改為 [] ```` ````yaml # repo-root/namespaces/kustomization.yaml # 命名空間聚合入口;可加入其他 domain/team 的命名空間資料夾 apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - foundation commonLabels: app.kubernetes.io/part-of: result-namespaces root.io/repo: repo-root # 可選:針對所有 namespaces 子樹套用 namePrefix # namePrefix: "ns-" ```` ````yaml # repo-root/namespaces/foundation/kustomization.yaml # foundation 命名空間基線:Namespace/Quota/LimitRange/NetworkPolicy apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - 00-namespace.yaml - 10-resourcequota.yaml - 20-limitrange.yaml - 30-networkpolicy-deny-all.yaml - 31-networkpolicy-allow-internal.yaml commonLabels: app.kubernetes.io/name: result-foundation app.kubernetes.io/part-of: result-namespaces root.io/repo: repo-root # 可選:針對 foundation 命名空間單點補丁 # patches: [] ````
最新发布
10-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值