Esercizio - Uso della funzione Concurrent per testare le prestazioni

Completato

Questo esercizio pratico illustra come usare la funzione Concurrent per migliorare le prestazioni.

  1. Accedere a Power Apps.

  2. Dal menu di spostamento a sinistra della schermata iniziale, selezionare + Crea > App vuota > Crea (dall'app canvas vuota).

  3. Assegnare all'app un titolo appropriato, quindi selezionare Crea.

  4. La prima operazione consiste nel creare due raccolte senza usare la funzione Concurrent. Inserire un controllo Pulsante, assegnare al pulsante il nome btnCollection, impostarne la proprietà Text su "Collection", quindi impostarne la proprietà OnSelect sulla seguente formula:

    Set(varStart,Now());
    ClearCollect(colFruit,
    {Name: "banana", Category: "fruit",AvgCost: .49},
    {Name: "peach", Category: "fruit",AvgCost: 1.12},
    {Name: "strawberry", Category: "fruit",AvgCost: 2.99},
    {Name: "apple", Category: "fruit",AvgCost: 0.98},
    {Name: "orange", Category: "fruit",AvgCost: 1.49},
    {Name: "pear", Category: "fruit",AvgCost: 0.97},
    {Name: "cantaloupe", Category: "fruit",AvgCost: 3.99},
    {Name: "pineapple", Category: "fruit",AvgCost: 2.49},
    {Name: "cherry", Category: "fruit",AvgCost: 4.99},
    {Name: "mango", Category: "fruit",AvgCost: .99}
    );
    ClearCollect(colVegetable,
    {Name: "carrot", Category: "vegetable",AvgCost: .95},
    {Name: "lettuce", Category: "vegetable",AvgCost: 1.69},
    {Name: "potato", Category: "vegetable",AvgCost: 5.14},
    {Name: "zuccini", Category: "vegetable",AvgCost: 1.99},
    {Name: "broccoli", Category: "vegetable",AvgCost: 1.49},
    {Name: "cabbage", Category: "vegetable",AvgCost: 2.48},
    {Name: "celery", Category: "vegetable",AvgCost: 1.65},
    {Name: "asparagus", Category: "vegetable",AvgCost: 2.99},
    {Name: "kale", Category: "vegetable",AvgCost: 1.99},
    {Name: "cauliflower", Category: "vegetable",AvgCost: 3.24});
    Set(varSpeedCollect, Text(DateDiff(varStart, Now(), TimeUnit.Milliseconds)));
    
  5. Selezionare Inserisci per inserire un'etichetta di testo, impostare la proprietà BorderThickness su 3, modificare il nome in lblSpeedCollect e impostarne la proprietà Text su:

    varSpeedCollect
    
  6. Tenere premuto il tasto ALT e selezionare il pulsante Raccolta. L'etichetta txtSpeedCollect visualizza la quantità di tempo in millisecondi necessaria per eseguire il processo.

    Screenshot dei risultati del pulsante Raccolta che mostrano il risultato varSpeedCollect pari a 2 nell'etichetta di testo.

  7. Ora si aggiungerà la funzionalità Concurrent all'app per confrontare le prestazioni. Inserire un altro controllo Pulsante, rinominarlo in btnConcurrent, impostarne la proprietà Text su "Concurrent" e la proprietà OnSelect sulla formula seguente:

    Set(varStart,Now());
    Concurrent(
    ClearCollect(collectFruit,
    {Name: "banana", Category: "fruit",AvgCost: .49},
    {Name: "peach", Category: "fruit",AvgCost: 1.12},
    {Name: "strawberry", Category: "fruit",AvgCost: 2.99},
    {Name: "apple", Category: "fruit",AvgCost: 0.98},
    {Name: "orange", Category: "fruit",AvgCost: 1.49},
    {Name: "pear", Category: "fruit",AvgCost: 0.97},
    {Name: "cantaloupe", Category: "fruit",AvgCost: 3.99},
    {Name: "pineapple", Category: "fruit",AvgCost: 2.49},
    {Name: "cherry", Category: "fruit",AvgCost: 4.99},
    {Name: "mango", Category: "fruit",AvgCost: .99}
    ),
    ClearCollect(collectVegetable,
    {Name: "carrot", Category: "vegetable",AvgCost: .95},
    {Name: "lettuce", Category: "vegetable",AvgCost: 1.69},
    {Name: "potato", Category: "vegetable",AvgCost: 5.14},
    {Name: "zucchini", Category: "vegetable",AvgCost: 1.99},
    {Name: "broccoli", Category: "vegetable",AvgCost: 1.49},
    {Name: "cabbage", Category: "vegetable",AvgCost: 2.48},
    {Name: "celery", Category: "vegetable",AvgCost: 1.65},
    {Name: "asparagus", Category: "vegetable",AvgCost: 2.99},
    {Name: "kale", Category: "vegetable",AvgCost: 1.99},
    {Name: "cauliflower", Category: "vegetable",AvgCost: 3.24}));
    Set(varSpeedConcurrent, Text(DateDiff(varStart, Now(), TimeUnit.Milliseconds)));
    
  8. Inserire un'altra Etichetta di testo, impostarne la proprietà BorderThickness su 3, rinominarla in lblSpeedConcurrent, quindi impostarne la proprietà Text su:

    varSpeedConcurrent
    
  9. Tenere premuto il tasto ALT e selezionare il pulsante Concurrent. In questo modo vengono create le stesse due raccolte e si dovrebbe notare che l'operazione richiede la metà del tempo.

    Screenshot dell'app con l'etichetta accanto a Concurrent che mostra la metà del tempo rispetto alla procedura precedente.

Come si può notare, l'aggiunta della funzione Concurrent ha migliorato le prestazioni della creazione delle raccolte. È possibile usare questa procedura per eseguire più processi contemporaneamente, tenendo presente che non è possibile prevedere l'ordine in cui iniziano e finiscono le formule all'interno della funzione Concurrent. Nella misura in cui i risultati non dipendono da altri risultati all'interno della funzione Concurrent, tutte le attività possono essere eseguite contemporaneamente. Questa tecnica può fornire un significativo incremento delle prestazioni dell'app.